System Wide KeyBoard Hooks

Do you need something made? then ask in here.
Forum rules
Please LOCK your topics once you have found the solution to your question so we know you no longer require help with your query.
14 posts Page 1 of 2
Contributors
User avatar
M1z23R
VIP - Donator
VIP - Donator
Posts: 622
Joined: Tue Sep 28, 2010 4:55 pm

System Wide KeyBoard Hooks
M1z23R
I would like someone to post any code, library, anything that can help get a simple way of getting this
Code: Select all
Private Sub GetKeys_Down (ByVal Sender as Object, ByVal e As EventsArgs) Handles MyHooks.KeyDown
MsgBox(Key.ToString)
End Sub
Nothing Else goofy;
GoodGuy17
Coding God
Coding God
Posts: 1610
Joined: Mon Sep 07, 2009 12:25 am

Re: System Wide KeyBoard Hooks
GoodGuy17
Try:
Code: Select all
Private Sub GetKeys_Down (ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles MyHooks.KeyPress
MsgBox(e.KeyChar.ToString)
End Sub
User avatar
M1z23R
VIP - Donator
VIP - Donator
Posts: 622
Joined: Tue Sep 28, 2010 4:55 pm

Re: System Wide KeyBoard Hooks
M1z23R
I need the hole API handling code '-.- That was just an example how i'd like to use it, in a very simple way - Handles Something.KeyDown
User avatar
mandai
Coding God
Coding God
Posts: 2585
Joined: Mon Apr 26, 2010 6:51 pm

Re: System Wide KeyBoard Hooks
mandai
You could do the same with GetAsyncKeyState, see viewtopic.php?f=32&t=4958&p=37340#p37340
User avatar
M1z23R
VIP - Donator
VIP - Donator
Posts: 622
Joined: Tue Sep 28, 2010 4:55 pm

Re: System Wide KeyBoard Hooks
M1z23R
I know about hot keys, but i don't want to use the timer, sometimes it registers 10 of my keys, sometimes none :/ It is all in Timer Interval :/ What interval does windows use if you can understand my question ? I am not that good at explaining things.
I want this to be something like key down event for textbox, but i want to use it Global, i'll send my application to system tray, it is for a game, some in game hot keys are numpad numbers, and others are on Q/W/E/A/S/D/Z/X/C, so i can't get them all with one hand. I want to be able to, for example press Shift + Q, and Handle (Q), so it doesn't send it, and send numpad number 1 for example.
So something like this
Code: Select all
Private Sub ()

If e.Modifiers = Keys.Shift and e.Key = Keys.Q Then
e.Handled = Keys.Q
SendKeys.Send("NUMPAD1")
End if

End Sub
Btw i know i can't send numpad numbers in "SendKeys", i must simulate NumPad1.Down, and NumPad1.Up using API.
User avatar
mandai
Coding God
Coding God
Posts: 2585
Joined: Mon Apr 26, 2010 6:51 pm

Re: System Wide KeyBoard Hooks
mandai
The timer interval is not the only important part, it is all about how you record which keys are up/down when the function runs.

Alternatively, you could use RegisterHotKey and the system will manage the key states and timers for you.
User avatar
M1z23R
VIP - Donator
VIP - Donator
Posts: 622
Joined: Tue Sep 28, 2010 4:55 pm

Re: System Wide KeyBoard Hooks
M1z23R
I think that would be what i want. But how do i register multiple keys ? I found a way to register 1, but if i register 1 more, it doesn't work :/
User avatar
mandai
Coding God
Coding God
Posts: 2585
Joined: Mon Apr 26, 2010 6:51 pm

Re: System Wide KeyBoard Hooks
mandai
With RegisterHotKey/UnregisterHotKey you can specify the ID parameter for multiple hotkeys.
User avatar
M1z23R
VIP - Donator
VIP - Donator
Posts: 622
Joined: Tue Sep 28, 2010 4:55 pm

Re: System Wide KeyBoard Hooks
M1z23R
Can you give me any examples ? I am not that good at using APIs :/
User avatar
mandai
Coding God
Coding God
Posts: 2585
Joined: Mon Apr 26, 2010 6:51 pm

Re: System Wide KeyBoard Hooks
mandai
Here is an example where you can register and unregister hotkeys by using form controls:
Code: Select all
    Dim keyNames As String()
    Dim keyValues As Array

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        keyNames = [Enum].GetNames(GetType(Keys))
        For i As Integer = 0 To keyNames.Length - 1
            listKey.Items.Add(keyNames(i))
        Next
        If listKey.Items.Count > 0 Then listKey.SelectedIndex = 0
        keyValues = [Enum].GetValues(GetType(Keys))

    End Sub

    <DllImport("user32.dll")> Shared Function RegisterHotKey(ByVal hWnd As IntPtr, ByVal id As Integer, ByVal fsModifiers As UInteger, ByVal vk As UInteger) As Boolean
    End Function

    Dim lastID As Integer = 0
    Dim IDs As List(Of Integer) = New List(Of Integer)

    Private Sub btnSet_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSet.Click
        Dim modifiers As UInteger = 0

        Select Case listMod1.SelectedIndex
            Case 1
                modifiers += 1 'alt
            Case 2
                modifiers += 2 'ctrl
            Case 3
                modifiers += 4 'shift
            Case 4
                modifiers += 8 'win
        End Select

        Select Case listMod2.SelectedIndex
            Case 1
                modifiers += 1
            Case 2
                modifiers += 2
            Case 3
                modifiers += 4
            Case 4
                modifiers += 8
        End Select

        listHotkeys.Items.Add("ID: " & lastID & ", Modifiers: " & modifiers & ", " & keyNames(listKey.SelectedIndex))

        IDs.Add(lastID)
        RegisterHotKey(Me.Handle, lastID, modifiers, keyValues(listKey.SelectedIndex))
        lastID += 1
    End Sub

    <DllImport("user32.dll")> Shared Function UnregisterHotKey(ByVal hWnd As IntPtr, ByVal id As Integer) As Boolean
    End Function

    Private Sub btnRemove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRemove.Click

        If listHotkeys.SelectedIndex > -1 Then

            MsgBox("Unregistering hotkey " & listHotkeys.SelectedItem)

            UnregisterHotKey(Me.Handle, IDs(listHotkeys.SelectedIndex))

            IDs.RemoveAt(listHotkeys.SelectedIndex)

            listHotkeys.Items.RemoveAt(listHotkeys.SelectedIndex)

        End If

    End Sub

    Const WM_HOTKEY As Integer = &H312

    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
        If m.Msg = WM_HOTKEY Then
            MsgBox("hotkey with ID " & m.WParam.ToInt32() & " pressed")
        End If
        MyBase.WndProc(m)
    End Sub
Where listHotkeys, listMod1, listMod2 and listKey are ListBox controls.
14 posts Page 1 of 2
Return to “Tutorial Requests”