0

这真的很简单。我希望应用程序即使没有焦点也能继续监视 KeyDown 事件。

Private Sub Form1_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
    Select Case e.KeyData
        Case Keys.MediaStop
            PictureBox2_Click(sender, e)
        Case Keys.MediaPlayPause
            PauseToolStripMenuItem_Click(sender, e)
        Case Keys.MediaNextTrack
            SkipTrackToolStripMenuItem_Click(sender, e)
        Case Keys.MediaPreviousTrack
            PreviousTrackToolStripMenuItem_Click(sender, e)
    End Select
End Sub

上面的代码用于音乐播放器。按下媒体键时调用函数('Fn'+ 'Home'、'Fn' + 'Pg Up'...等)

在之前的评论中,有人建议研究 WH_KEYBOARD_LL 以寻求解决方案,但老实说,我并不太了解它。

更新:

建议的链接不是很好,因为“Declare”函数不支持“As Any”关键字。

这就是我已经走了多远......

Public Declare Function CallNextHookEx Lib "user32" (ByVal hHook As Long, ByVal ncode As Long, ByVal wParam As Long, lParam As **Any**) As Long

Public Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Long) As Long

Public Declare Function SetWindowsHook Lib "user32" Alias "SetWindowsHookA" (ByVal nFilterType As Long, ByVal pfnFilterProc As Long) As Long

Public Declare Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" (ByVal idHook As Long, ByVal lpfn As Long, ByVal hmod As Long, ByVal dwThreadId As Long) As Long

Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As **Any**, Source As **Any**, ByVal Length As Long)

**Global** Const WH_KEYBOARD_LL = 13
Public Const HC_ACTION = 0

Structure HookStruct
    Dim vkCode As Long
    Dim scancode As Long
    Dim flags As Long
    Dim time As Long
    Dim dwExtraInfo As Long
End Structure

Visual Studio 下划线的错误都以粗体书写

在“全局”悬停时,它会给出“语法错误”

4

1 回答 1

0

使用 Windows API 实现键盘挂钩是解决方案。请检查:http ://www.planetsourcecode.com/vb/scripts/ShowCode.asp?txtCodeId=13506&lngWId=1

于 2013-07-17T08:37:56.417 回答