Page 2 of 2

Re: System Wide KeyBoard Hooks

Posted: Mon Oct 24, 2011 10:53 am
by M1z23R
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 :)

Re: System Wide KeyBoard Hooks

Posted: Mon Jan 27, 2014 11:58 am
by troop
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

Re: System Wide KeyBoard Hooks

Posted: Mon Jan 27, 2014 11:14 pm
by mandai
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?

Re: System Wide KeyBoard Hooks

Posted: Tue Jan 28, 2014 5:11 pm
by troop
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;