How do I make hotkeys for buttons, which will activate it, like I pressed the button?
Keep in mind I’m new at programming.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You can use the GetKeyState function in Windows. Windows functions must be referenced by APIs, since they are not by default included in the .NET Framework as such.
Assuming you only need to detect one keystroke, GetKeyState or GetAsyncKeyState would probably be best for that. GetKeyState will wait until a specific key is pressed and then get the state of that key, and GetAsyncKeyState will get the state of a key at the time the function is called.
The declaration of GetKeyState is as follows:
Put that inside the class where you wish to use the function.
The virtualKeyCode argument of the function is the keycode of the key you are trying to get the state of. You can see a list of all the virtual keycodes here: http://msdn.microsoft.com/en-us/library/ms927178.aspx
The GetAsyncKeyState declaration is almost the same as the GetKeyState declaration.
Using both the GetKeyState and GetAsyncKeyState is pretty simple From Microsoft’s own documentation (http://msdn.microsoft.com/en-us/library/ms646293(v=vs.85).aspx) it says:
Given the above information, here’s an example of GetAsyncKeyState usage:
If you need to get notified when the user presses any kind of key, a keyboard hook would be more appropriate.
Lastly, you may also use the RegisterHotkey API to get notified by Windows when specific hotkeys are pressed.