您可以使用 Keys.MediaXXX 常量捕获多媒体键。这是以标准形式捕获这些键的示例:
Public Class Form1
Protected Overrides Function ProcessKeyMessage(ByRef m As System.Windows.Forms.Message) As Boolean
Const WM_KEYDOWN As Integer = &H100
If m.Msg = WM_KEYDOWN Then
Select Case m.WParam.ToInt32
Case Keys.MediaStop
Debug.Print("Stop")
Case Keys.MediaPlayPause
Debug.Print("PlayPause")
Case Keys.MediaNextTrack
Debug.Print("Next")
Case Keys.MediaPreviousTrack
Debug.Print("Previous")
End Select
End If
Return MyBase.ProcessKeyMessage(m)
End Function
End Class
*这也可以在表单的 KeyDown() 事件中使用e.KeyData
.
编辑:这是使用表单的 KeyDown() 事件的示例。请注意,我启用了 KeyPreview(); 如果表单上有任何可以接收焦点的控件,则需要这样做:
Public Class Form1
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Me.KeyPreview = True
End Sub
Private Sub Form1_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
Select Case e.KeyData
Case Keys.MediaStop
Debug.Print("Stop")
Case Keys.MediaPlayPause
Debug.Print("PlayPause")
Case Keys.MediaNextTrack
Debug.Print("Next")
Case Keys.MediaPreviousTrack
Debug.Print("Previous")
End Select
End Sub
End Class