• Top Members
    Reps
    Posts
  • 834 Replies
    2585 Replies
  • 716 Replies
    2025 Replies
  • 331 Replies
    1928 Replies

System Wide KeyBoard Hooks

Please LOCK your topics once you have found the solution to your question so we know you no longer require help with your query.
User avatar
M1z23R
VIP - Donator
Posts: 622

Re: System Wide KeyBoard Hooks

Mon Oct 24, 2011 11:53 am

Thanks, i saw it just now :) And worked a little with it, i added items to ListMod1 and ListMod2:

Code: Select all

None
Alt
Ctrl
Shift
And it works perfect :)

User avatar
troop
Dedicated Member
Posts: 75

Re: System Wide KeyBoard Hooks

Mon Jan 27, 2014 12:58 pm

mandai wrote: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.
#mandai I think this is what i need, but i get errors from the DLLimport and Im confused on what all those listboxes do, please if u can explain or give me a source code
also where do I change it to be SHIFT F1 and SHIFT F2 ?

ty

User avatar
mandai
Coding God
Posts: 2585

Re: System Wide KeyBoard Hooks

Tue Jan 28, 2014 12:14 am

The listboxes are used to set the keys that it looks for.
For example if you need to check for Shift+F1 then ListMod1 would be at index 3 and listKey would match the index for F1. After btnSet is clicked it will listen, then when the hotkey is pressed it will run the code in WndProc.
This is all the source code that you should need, though there is a Microsoft article about this function here.

What error does the DLLImport give?

User avatar
troop
Dedicated Member
Posts: 75

Re: System Wide KeyBoard Hooks

Tue Jan 28, 2014 6:11 pm

mandai wrote:The listboxes are used to set the keys that it looks for.
For example if you need to check for Shift+F1 then ListMod1 would be at index 3 and listKey would match the index for F1. After btnSet is clicked it will listen, then when the hotkey is pressed it will run the code in WndProc.
This is all the source code that you should need, though there is a Microsoft article about this function here.

What error does the DLLImport give?
sorry for the late reply, i seem to be very confused, thats fine i wont use this as nobody seem to know how to explain it well, thanks mandai for this btw :D loove;

Post Reply

Return to “Tutorial Requests”