I have an application which uses hotkeys which are to be defined by the user. It is a combination of 2 keys, either CTRL, ALT, SHIFT as one hotkey and the F* Function keys as the other (e.g. CTRL + F1, ALT + F2 or SHIFT + F8) As stated these are then chosen by the user before a global keyhook is put in place to listen for them and trigger an event. In my app I have all my function keys as follows:
Public Const VK_F1 = &H70
Public Const VK_F2 = &H71
Public Const VK_F3 = &H72
Public Const VK_F4 = &H73
etc etc…
Here is the code I am using the catch the key combinations:
If (Hookstruct.vkCode = VK_F5) And _
CBool(Hookstruct.flags And _
LLKHF_ALTDOWN) Then
'TriggerEventHere
'Return True
End If
I also have Two combo boxes for the user to select ALT, CTRL, SHIFT in one combobox and the Function keys in the other. I then have a button which activates the hook.
My question is this; how can I use the user information from the comboboxes to set the relevant key combinations in my code when the app is running?
Thanks for taking the time to read this.
Set the form’s
KeyPreviewproperty totrue(this will capture all the key strokes at the form level). E.g.:In the
Form1_KeyDownevent you can look up the Key combinations defined by the user, i.e. these could be stored in aList(Of KeyPresses)whereKeyPressesis a class (tip: use theKeyEventArgsclass as inspiration) that has the user-defined key combinations.At run-time, after users save key press combinations, when they do key presses, it will fire the
KeyDownevent, and you’ll check theList(Of KeyPresses)and trigger an event: