1

I can create and use system-wide hotkeys by using RegisterHotkey and UnregisterHotkey(), as follows:

Enum HotkeyModifier As UShort
    None = &H0
    Alt = &H1 'Alt key
    Control = &H2
    Shift = &H4
    Windows = &H8
    WM_HOTKEY = &H312
    Norepeat = &H4000
End Enum
'


<Runtime.InteropServices.DllImport("User32.dll")> _
Public Shared Function RegisterHotKey(ByVal hwnd As IntPtr, ByVal id As Integer, _
                                ByVal fsModifiers As Integer, ByVal vk As Integer) As Integer
End Function

<Runtime.InteropServices.DllImport("User32.dll")> _
Public Shared Function UnregisterHotKey(ByVal hwnd As IntPtr, ByVal id As Integer) As Integer
End Function
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
    If m.Msg = HotkeyModifier.WM_HOTKEY Then
        Dim id As IntPtr = m.WParam
        Select Case (id.ToInt32)
            Case 100
                MessageBox.Show("You pressed ALT+D key combination")
            Case 300
                MessageBox.Show("You pressed CTRL+SHIFT+F12 key combination")
            Case 399
                SwitchTopMost()
        End Select
    End If
    MyBase.WndProc(m)
End Sub
Friend Sub SwitchTopMost()
    Me.TopMost = Not Me.TopMost
    If Me.TopMost Then
        Me.Show()
        Me.ClientSize = New Size(Me.RestoreBounds.X, Me.RestoreBounds.Y)
        Me.ClientSize = RestoreSize
    Else : Me.Hide()
    End If
End Sub
Private Sub Unregister(ByVal sender As System.Object, ByVal e As  _
                            System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
    UnregisterHotKey(Me.Handle, 100)
    UnregisterHotKey(Me.Handle, 300)
    UnregisterHotKey(Me.Handle, 399)
End Sub


Private Sub Register(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    RegisterHotKey(Me.Handle, 100, HotkeyModifier.Alt, Keys.D)
    RegisterHotKey(Me.Handle, 300, HotkeyModifier.Control + HotkeyModifier.Shift, Keys.F12)
    RegisterHotKey(Me.Handle, 399, HotkeyModifier.Alt, Keys.None)
End Sub

But is there anything that can create more customizable system-wide hotkeys like Space+S, NumLock+Control+P or Tab+CapsLock+F5? I am running out of hotkeys.

More info about these hotkeys: Another post about hotkeys

Also would like something like this post without other programs.

Edit: I would prefer a way like the above link the most.

Community
  • 1
  • 1
Happypig375
  • 1,022
  • 1
  • 12
  • 32
  • No. You can combine keys with a modifier (Ctrl or Alt), but not just multiple keys like Space+S (which are just two characters). You can use things like Ctrl+P and then in the code that handles that you can check for the state of NumLock, but not as a single combination (same goes for CapsLock). – Ken White Jan 01 '16 at 04:39
  • @KenWhite Will the check block the Ctrl+P combination? – Happypig375 Jan 01 '16 at 04:51
  • No, the check would be done *after* the Ctrl+P occurs, so it can't block it. You would detect Ctrl+P, and then within the handler for that see if NumLock is on or not and react accordingly. Think of the logic: *if the key combination is Ctr+P then (if the NumLock is on then do this otherwise do that)*. – Ken White Jan 01 '16 at 04:57

1 Answers1

1

If you insist on not using programs such as AutoHotkey, I see two possible ways to achieve what you want:

  1. First you can make a Timer and just check if the keys you want are pressed with GetAsycKeyState every say 100ms or so.
  2. The more elegant way would be to use a low-level keyboard hook. I've used this simple one in the past as a starting point for my code (it's written in C#, but converting that to VB.NET shouldn't be hard).
Royce
  • 143
  • 1
  • 10
  • I do not want to use things like AutoHotKey because there are no way to turn that into Visual Basic language. I do not want to have too many executables. – Happypig375 Jan 03 '16 at 13:01
  • I think AutoIt has a dll that you can use in VB. AHK runs very efficiently though, so I wouldn't worry too much about using it. – Royce Jan 03 '16 at 20:55