Capture Hot Key Combination with vb.net

Good Morning! Well if you have been following along, I have made a full recovery.  I got a rental car which I look really funny in. (It is a compact and I have long legs you see.) My daughter was taken into custody but they are probably not going to revoke her. My wife and I made the decision though that she can go elsewhere until she turns 18 once she gets out. It’s just time. We are tired of being stolen from and disrespected.

So today’s topic is how to capture a hot Hot Key Combination with vb.net. In this case it will be Ctrl/Shift/F12. Make it a great day!


Join me on Facebook

Public Class Form1
    Inherits System.Windows.Forms.Form
    Private WithEvents llkb As New LowLevelKeyBoardhook
    Private Sub llkb_CtrlShiftF12() Handles llkb.CtrlShiftF12
        MsgBox("Ctrl-Shift-F12 Pressed ")
    End Sub
End Class
‘ ————————————————–
‘  Class LowLevelKeyBoardhook
‘ ————————————————–
Imports System.Runtime.InteropServices
Public Class LowLevelKeyBoardhook
    Private Const HC_ACTION As Integer = 0
    Private Const WH_KEYBOARD_LL As Integer = 13
    Private Const WM_KEYDOWN As Integer = &H100
    Private Const WM_SYSKEYDOWN As Integer = &H104
    Public Structure KBDLLHOOKSTRUCT
        Public vkCode As Integer
        Public scancode As Integer
        Public flags As Integer
        Public time As Integer
        Public dwExtraInfo As Integer
    End Structure
    Private Declare Function SetWindowsHookEx Lib "user32" _
        Alias "SetWindowsHookExA" ( _
        ByVal idHook As Integer, _
        ByVal lpfn As LowLevelKeyboardProcDelegate, _
        ByVal hmod As Integer, _
        ByVal dwThreadId As Integer) As Integer
    Private Declare Function CallNextHookEx Lib "user32" ( _
        ByVal hHook As Integer, _
        ByVal nCode As Integer, _
        ByVal wParam As Integer, _
        ByVal lParam As KBDLLHOOKSTRUCT) As Integer
    Private Declare Function UnhookWindowsHookEx Lib "user32" ( _
        ByVal hHook As Integer) As Integer
    Private Delegate Function LowLevelKeyboardProcDelegate( _
        ByVal nCode As Integer, _
        ByVal wParam As Integer, _
        ByRef lParam As KBDLLHOOKSTRUCT) As Integer
    Private Declare Function GetAsyncKeyState Lib "user32" _
        (ByVal vKey As Integer) As Integer
    Public Event CtrlShiftF12()
    Private hhkLowLevelKeyboard As Integer
    Private keyboardDelegate As LowLevelKeyboardProcDelegate
    Public Sub New()
        keyboardDelegate = New LowLevelKeyboardProcDelegate(AddressOf Me.LowLevelKeyboardProc)
        hhkLowLevelKeyboard = SetWindowsHookEx(WH_KEYBOARD_LL, keyboardDelegate, _
            Marshal.GetHINSTANCE(System.Reflection.Assembly.GetExecutingAssembly.GetModules()(0)).ToInt32, 0)
    End Sub
    Private Function LowLevelKeyboardProc( _
        ByVal nCode As Integer, _
        ByVal wParam As Integer, _
        ByRef lParam As KBDLLHOOKSTRUCT) As Integer
        If (nCode = HC_ACTION) Then
            Select Case wParam
                Case WM_KEYDOWN, WM_SYSKEYDOWN
                    If GetAsyncKeyState(Keys.ControlKey) AndAlso _
                            GetAsyncKeyState(Keys.ShiftKey) AndAlso _
                            (lParam.vkCode = Keys.F12) Then
                        RaiseEvent CtrlShiftF12()
                    End If
            End Select
        End If
        Return CallNextHookEx(hhkLowLevelKeyboard, nCode, wParam, lParam)
    End Function
    Protected Overrides Sub Finalize()
        UnhookWindowsHookEx(hhkLowLevelKeyboard)
        MyBase.Finalize()
    End Sub
End Class

 

Technorati Tags: ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,

Advertisements
  1. #1 by Rob on April 28, 2009 - 9:04 pm

    Hi Kelly…This code snippet was exactly what I was looking for to use in a new project. Unfortunately, I can\’t get it to actually capture the key combination and display the message. The form pretty much just stays there on the screen not doing anything regardless of me hitting the CTRL+SHIFT+F12 keys.Anything you can do to point me in the right direction?Regards…Rob

  2. #2 by Rob on April 29, 2009 - 10:48 am

    Kelly…Disregard my post from last night. I didn\’t realize being in "debug" mode was causing my problem. Once I ran the code in normal mode everything worked perfectly. Thanks for posting this as it saved me quite a bit of time.Regards…Rob

  3. #3 by Kelly on April 29, 2009 - 6:35 pm

    no problem. apologize for not responding last night.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: