In my Windows Forms application I would like one special button to run a test everytime I press it. There are dozens of controls so implementing it in each one takes too much time.
Is there a way I can set a hotkey so, no matter what I am doing in the application, I can press the key, and it will fire off my event?
You can override a form's ProcessCmdKey() method, it runs before any control on the form will see the key stroke. If you really need this to be at the application level (all forms) then you should have your main form implement the IMessageFilter interface. For example:
If you have just one form. You can set the Form.KeyPreview=true and then add code to the form's key event.
You can override
ProcessCmdKey
and handle your hotkeys there, either in a control or a form.From MSDN:
For example: