Question [VB.2010] Problem with Key-Hook

nlh

New member
Joined
Apr 25, 2012
Messages
2
Programming Experience
Beginner
Hello,
I have a little problem with this source code.
It should send "bla" when Ctrl + 1 is pressed

VB.NET:
Public Class frmKeytest
    Private Declare Function RegisterHotKey Lib "user32" (ByVal hWnd As IntPtr, ByVal id As Integer, ByVal fsModifier As Integer, ByVal vk As Integer) As Integer
    Private Declare Sub UnregisterHotKey Lib "user32" (ByVal hWnd As IntPtr, ByVal id As Integer)
    Private Const Key_NONE As Integer = &H0
    Private Const WM_HOTKEY As Integer = &H312
    Private Const MOD_ALT As Integer = &H1
    Private Const MOD_CONTROL As Integer = &H2
    Private Const MOD_SHIFT As Integer = &H4
    Private Const MOD_WIN As Integer = &H8

    Protected Overrides Sub WndProc(ByRef m As Message)
        'die messages auswerten
        If m.Msg = WM_HOTKEY Then
            Select Case m.WParam
                Case 1
                    MessageBox.Show("you pressed Tab")
                Case 2
                    SendKeys.Send("bla")
            End Select
        End If
        MyBase.WndProc(m)
    End Sub

    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        UnregisterHotKey(Me.Handle, 1)
        UnregisterHotKey(Me.Handle, 2)
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        RegisterHotKey(Me.Handle, 1, Key_NONE, Keys.Tab)
        RegisterHotKey(Me.Handle, 2, MOD_CONTROL, Keys.D1)
    End Sub
End Class
 
Top Bottom