471,066 Members | 2,099 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,066 software developers and data experts.

How to Read a keyboard input on an application?

I currently have a gui app I programmed with visual c++ .net version. Is there any way I can read any character from the keyboard as soon as any key is pressed while the application is running? My intention is that as soon as a specific key is pressed, the app is suppose to take the appropriate action. All the functions work, so all I would have to do is call the function as soon as a key is pressed from the keyboard? I am not asking the user for an input, it's just if the user does hit a keyboard key it will take the key as an input and call the function.

Thanks in advance.
Jul 20 '07 #1
5 2853
Atran
319 100+
I currently have a gui app I programmed with visual c++ .net version. Is there any way I can read any character from the keyboard as soon as any key is pressed while the application is running? My intention is that as soon as a specific key is pressed, the app is suppose to take the appropriate action. All the functions work, so all I would have to do is call the function as soon as a key is pressed from the keyboard? I am not asking the user for an input, it's just if the user does hit a keyboard key it will take the key as an input and call the function.

Thanks in advance.
Hello:
Try this code:
Expand|Select|Wrap|Line Numbers
  1. private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) {
  2.         //Start from Here:
  3.         Control ^ Ctrl = gcnew Control();
  4.         Ctrl->KeyDown += gcnew System::Windows::Forms::KeyEventHandler(this, &Form1::Ctrl_KeyDown);
  5.         Controls->Add(Ctrl);
  6.     }
  7. private: System::Void Ctrl_KeyDown(Object^ sender, System::Windows::Forms::KeyEventArgs^  e) {
  8.             if(e->KeyData == Keys::Space)
  9.             {
  10.                  ShowMessageBox("You pressed Space key.");
  11.             }
  12.         if(e->KeyCode == Keys::Enter)
  13.         {
  14.             ShowMessageBox("You pressed Enter key.");
  15.         }
  16.     }
  17.  
  18. private: System::Void ShowMessageBox(System::String^ msg) {
  19.         MessageBox::Show(msg, "Key Pressed");
  20.     }
  21. };
  22.  
Hope I helped you.
Jul 21 '07 #2
Thanks for the response, I'm working with an older version of .net, I believe its 2003. I modified the code so that I won't get any errors, nothing major, just had to change ^ to * and gcnew to new. I'm going to continue to work on it to get it to work. Thanks again.
Jul 23 '07 #3
Plater
7,872 Expert 4TB
You can register a global hook for your program, it pretty much will allow you to have a function like:
Expand|Select|Wrap|Line Numbers
  1. void globalkeypressed(object ContainsKeyData)
  2. {
  3.    if (ContainsKeyData.Key==Keys.A)
  4.    {
  5.       //do something
  6.    }
  7. }
  8.  
I have only been finding ways to stick global hooks into C#/VB.NET as they're much more native to c++. But you should be able to use it.

http://msdn2.microsoft.com/en-us/library/ms644959.aspx

The important function to get at is: (this is C#, but you can see where it's comming from)
Expand|Select|Wrap|Line Numbers
  1. //Use this function to install a thread-specific hook.
  2. [DllImport("user32.dll",CharSet=CharSet.Auto,
  3.  CallingConvention=CallingConvention.StdCall)]
  4. public static extern int SetWindowsHookEx(int idHook, HookProc lpfn, 
  5. IntPtr hInstance, int threadId);
  6.  
(http://support.microsoft.com/kb/318804 )
Jul 23 '07 #4
Thanks Atran and Plater for the help. You both gave me good ideas. I just found this site which was very helpful. I modified it a bit so that it won't give me messages instead it just calls the function I want. Works Great. Haven't found any bugs yet, although I still have a lot more testing to do.

http://support.microsoft.com/kb/839201
Jul 23 '07 #5
Plater
7,872 Expert 4TB
Hehe, oh yeah, you said you only wanted to capture when your program was active, my bad.
Thanks Atran and Plater for the help. You both gave me good ideas. I just found this site which was very helpful. I modified it a bit so that it won't give me messages instead it just calls the function I want. Works Great. Haven't found any bugs yet, although I still have a lot more testing to do.

http://support.microsoft.com/kb/839201
Jul 23 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

13 posts views Thread by j. del | last post: by
4 posts views Thread by Ralf Toender | last post: by
7 posts views Thread by Don Riesbeck Jr. | last post: by
7 posts views Thread by jpierson | last post: by
1 post views Thread by Louis Cypher | last post: by
1 post views Thread by Louis Cypher | last post: by
1 post views Thread by pinkfloydhomer | last post: by
8 posts views Thread by BD | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.