在Visual Basic中执行没有焦点的KeyDown函数

时间:2013-07-16 20:07:12

标签: visual-studio-2010 vb6 media-player onkeydown wh-keyboard-ll

真的很简单。我希望应用程序即使没有焦点也能继续监视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以寻求解决方案,但如果我是诚实的话,我并不太了解它。

更新:

建议的link不是很好,因为'声明'功能不支持'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强调的错误都是用粗体编写的 在'Any'悬停时,它会在“声明”声明中向我提供“'任何'不支持”的信息“

在“全局”悬停时,它会显示“语法错误”

1 个答案:

答案 0 :(得分:0)

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