使用按键处理表单上的按键 [英] Using Keypress to Process Keypress on form

查看:134
本文介绍了使用按键处理表单上的按键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在frmMain中,我有:

In frmMain I have:

AddHandler Me.PreviewKeyDown, AddressOf Me.TrapKeyPreview
AddHandler Me.KeyDown, AddressOf Me.TrapKeyDown
Me.KeyPreview = True



然后有两个附加子:



There are then two additional subs:

Private Sub TrapKeyPreview(ByVal sender As Object, _
                       ByVal e As PreviewKeyDownEventArgs)
    Select Case (e.KeyCode)
        Case Keys.Down, Keys.Up
            e.IsInputKey = True
    End Select
End Sub

Private Sub TrapKeydown(ByVal sender As Object, _
                        ByVal e As KeyEventArgs)
    Select Case (e.KeyCode)
        Case Keys.Up
            'Process up arrow key
        Case Keys.Down
            'Process down arrow key
    End Select
    e.Handled = True
End Sub



当我将其放入一个简单的应用程序时,它会按预期执行.您可以具有其他文本框等,可以在它们之间进行制表,但是向上箭头和向下箭头由TrapKeyDown处理.

当我将此代码放到带有许多控件的更大的应用程序中时,它在向上箭头方向上的行为不一致.当我按下下一个向上箭头时,向下箭头的击键似乎已被缓冲,然后被应用.

任何想法将不胜感激.

Carl



When I put this into a simple app, it performs as intended. You can have other textboxes, etc. that you can TAB between but the up arrow and down arrow are handled by TrapKeyDown.

When I put this code into a larger app with many controls it behaves inconsistently with regard to arrow up. Arrow down key strokes appear to be buffered and then applied when I hit the next Up Arrow.

Any ideas would be appreciated.

Carl

推荐答案

我遇到一个问题,需要捕获Page Up,Page Down,Home和End(用于导航快捷键);但是,标准事件并未捕获它们.不确定这对您的情况是否有帮助;但是,也许值得一看.

I ran into a problem where I needed to capture Page Up, Page Down, Home and End (to use for quick keys for navigation); however, the standard events were not capturing them. Not sure if this helps you in your situation; but, maybe worth a look into.

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.PageDown, Keys.PageUp, Keys.End, Keys.Home
                   RaiseEvent CommandKeyPress(keyData)
           End Select
           MyBase.ProcessCmdKey(msg, keyData)
       End Function


这篇关于使用按键处理表单上的按键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆