我可以使用RegisterHotkey和UnregisterHotkey()创建和使用系统范围的热键,如下所示:
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
但有什么可以创建更多可自定义的系统级热键,如 Space + S , NumLock + Control + P 或 Tab + CapsLock + F5 ?我的电脑用完了。
有关这些热键的更多信息:Another post about hotkeys
如果没有其他程序,也需要this post 。
编辑:我更喜欢上述链接。
答案 0 :(得分:1)
如果您坚持不使用AutoHotkey等程序,我会看到两种可能的方法来实现您的目标:
Timer
,然后检查所需的按键是否按每{100}左右GetAsycKeyState按下。