Link to home
Start Free TrialLog in
Avatar of MurrayHillIT
MurrayHillIT

asked on

Implementing Keyboard Hook in VB.NET

Hello,

I have a VB.NET program that I need to execute code if a key combination is pressed (Ctrl+Shift+Alt+S). This code needs to be executed whether the program is in focus or not.
[Note: If anyone has used Deep Freeze, this is just exactly how that program functions - i.e. after you press a specific key combination the program's menu appears even if the program was not in focus]

In the course of my search for a solution I came across Windows Keyboard Hooks as the most likely candidate. My issue is that I can't get anything I try to work and I just really need help implementing this solution: what code should I use, advice about where to implement it in my program, etc.

So, Specifically, I have created a "Settings Page" in my program that, among other things, has a Checkbox for executing a forced shutdown command if Ctrl+Shift+Alt+S is pressed. [Note: The settings on the Form are saved in a Txt file and loaded both on Form Load and when the "Ok" or "Apply" buttons are pressed on that Settings Page.] I'd like the Ctrl+Alt+Shift+S key combination to become active only when the Checkbox for the feature is marked, and will initiate the forced shutdown regardless of whether my program is in focus or not.

Thank you in advance
ASKER CERTIFIED SOLUTION
Avatar of HaiFai
HaiFai
Flag of Finland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I forgot to add windows key its
MOD_WIN
0x0008
so if you want to use that just add &H8 to this line
Public Const MOD_ALT As Integer = &H1 + &H2 + &H4

Open in new window


you can allso remove any combo of those keys

and remember to unregister keys after program close

UnregisterHotKey(Me.Handle, 100)
UnregisterHotKey(Me.Handle, 200)
'..etc...

Open in new window

Avatar of MurrayHillIT
MurrayHillIT

ASKER

Thanks! Just one more thing. How can I call this from another form? I'm having a little difficulty calling it from my Settings form (where the checkboxes for the specific settings are).
Hi

Hotkey should work anywhere no matter what form is currently active, or what do you mean?
oh did you mean like this

add this to main form
   Public checkedsetting As Boolean = False

Open in new window


change this
 Case "500"
                    If checkedsetting = True Then
                        MsgBox("cheked")
                    Else
                        MsgBox("not cheked")
                    End If

Open in new window


and in settings page you need to save state of checkbox to checkedsetting so add this settings form
  Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged
        If CheckBox1.Checked = True Then
            Form1.checkedsetting = True
        Else
            Form1.checkedsetting = False
        End If
    End Sub

Open in new window


in settings page code you need to change "Form1" to point your main form
Why can't I just do this in my settings form:

If ShoprtcutCheckBox.Checked Then
     'Initiate Keyboard Hook
     Call Form1.ResgisterHotKey(Me.Handle, 100, Form1.MOD_ALT, Keys.S)
Else
     'Unregister Keyboard Hook
     Call Form1.UnregisterHotKey(Me.Handle, 100)
End If