简体   繁体   English

如何在WinForms应用程序中拦截捕获TAB键?

[英]How to intercept capture TAB key in WinForms application?

I'm trying to capture the Tab key in a Windows Forms application and do a custom action when it is pressed. 我正在尝试捕获Windows窗体应用程序中的Tab键,并在按下时执行自定义操作。

I have a Form with several listViews and buttons, I've set the Form's KeyPreview property to true and when I press any other key than tab, my KeyDown event handler does get called. 我有一个带有几个listViews和按钮的Form,我已经将Form的KeyPreview属性设置为true,当我按Tab键之外的任何其他键时,我的KeyDown事件处理程序会被调用。

But that's not true with the Tab key - I don't receive WM_KEYDOWN message even in WndProc. 但是Tab键不是这样 - 即使在WndProc中我也没有收到WM_KEYDOWN消息。

Do I need to set each control inside my form - its TabStop property - to false? 我是否需要将我的表单中的每个控件(它的TabStop属性)设置为false? There must be a more elegant way than that. 必须有一种比这更优雅的方式。

Thanks. 谢谢。

This is the C# code similar to the VB code given in the answer above... 这是与上面答案中给出的VB代码类似的C#代码...

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
    {
        if (keyData == Keys.Tab)
        {
            //your code
        }
        return base.ProcessCmdKey(ref msg, keyData);
    }

Hope this helps... 希望这可以帮助...

will this help you? 对你有帮助吗?

Protected Overrides Function ProcessCmdKey(ByRef msg As Message, ByVal keyData As Keys) As Boolean
  Dim keyPressed As Keys = CType(msg.WParam.ToInt32(), Keys)

  Select Case keyPressed
    Case Keys.Right msgbox("Right Arrow Key Caught")
    Case Keys.Left msgbox("LeftArrow Key Caught")
    Case Keys.Up msgbox("Up Arrow Key Caught")
    Case Keys.Down msgbox("Down Arrow Key Caught")
    Case Else Return MyBase.ProcessCmdKey(msg, keyData)
  End Select
End Function 

您可以使用“PreviewKeyDown”事件

Private Sub form1_KeyDown(.... ) Handles Me.KeyDown
    If e.KeyCode = Keys.Enter Then
        SendKeys.Send("{tab}")
    End If
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM