VB.NET中捕获功能键F1..F12

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4630825/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 15:06:10  来源:igfitidea点击:

Capture function keys F1..F12 in VB.NET

.netvb.netwinforms.net-2.0keypress

提问by Bobby

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

我无法为我的应用程序捕获功能键F1..。F12我能捕捉到正规键以及功能键,如shiftctrlalt,等。

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

这个问题推荐KeyPreview = True,但是这似乎不适用于我的应用程序。我究竟做错了什么?

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

采纳答案by theChrisKent

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

您的代码对我有用,但您的 EventHandler 声明中有错字。改变:

AddHandler Me.KeyDown, AddressOf KeyDownsHandler

to

AddHandler Me.KeyDown, AddressOf KeyDownHandler

alt text

替代文字

回答by Bobby

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 Selectstatement. If you want to use combinations with Shift, Alt or Ctrl you'll just have to Orthem. Of course this works on a very low Formlevel 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.

这将自动抑制您在Select语句中处理的每个键。如果您想使用 Shift、Alt 或 Ctrl 的组合,您只需要使用Or它们。当然,这适用于非常低的Form级别,这使其独立于您在该表单上拥有的任何控件。如果您不知道,它也会触发奇怪的行为,例如焦点跳跃或行为不良的控件。