开发者

Capture function keys F1..F12 in VB.NET [closed]

开发者 https://www.devze.com 2023-02-03 07:37 出处:网络
开发者_运维知识库 This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time,or an extraordinarily narrow situation that is not
开发者_运维知识库 This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. Closed 9 years ago.

I cannot capture the function keys F1..F12 for my application. I am able to capture regular keys and modifiers such as shift, ctrl, alt, etc..

This question recommends KeyPreview = True, however that does not seem to work for my application. What am I doing wrong?

Private Sub Main_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Me.KeyPreview = True
    AddHandler Me.KeyDown, AddressOf KeyDownsHandler
    AddHandler Me.KeyUp, AddressOf KeyUpHandler
End Sub

Private Sub KeyUpHandler(ByVal o As Object, ByVal e As KeyEventArgs)
    e.SuppressKeyPress = True
    If e.KeyCode = Keys.F1 Then
        txtMain.AppendText("F1 was pressed!" & Environment.NewLine)
    End If
    txtMain.AppendText( _
        String.Format("'{0}' '{1}' '{2}' '{3}' {4}", e.Modifiers, e.KeyValue, e.KeyData, e.KeyCode, Environment.NewLine))
End Sub

Private Sub KeyDownHandler(ByVal o As Object, ByVal e As KeyEventArgs)
    e.SuppressKeyPress = True
    txtMain.AppendText( _
        String.Format("'{0}' '{1}' '{2}' '{3}' {4}", e.Modifiers, e.KeyValue, e.KeyData, e.KeyCode, Environment.NewLine))
End Sub


For capturing keys (including function keys) I've started to use this pattern, which works quite well:

    Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, ByVal keyData As System.Windows.Forms.Keys) As Boolean
        Select Case keyData
            Case Keys.F2
                ' Do something 

            Case Keys.F3
                ' Do more

            Case Keys.Escape
                ' Crap

            Case Else
                Return MyBase.ProcessCmdKey(msg, keyData)

        End Select

        Return True
    End Function

This will automatically suppress every key you handle in the Select statement. If you want to use combinations with Shift, Alt or Ctrl you'll just have to Or them. Of course this works on a very low Form level which makes it independent from any Control you have on that form. It will also trigger weird behavior if you do not know that, like focus-jumps or badly behaving Controls.


Your code works for me with the exception that you have a typo in your EventHandler declaration. Change:

AddHandler Me.KeyDown, AddressOf KeyDownsHandler

to

AddHandler Me.KeyDown, AddressOf KeyDownHandler

Capture function keys F1..F12 in VB.NET [closed]

0

精彩评论

暂无评论...
验证码 换一张
取 消