基本 WinForm KeyDown 事件处理
我正在使用 WinForms。我为主窗体的 KeyDown
事件创建了一个事件处理程序,从而调用按钮的 Click
事件处理程序。
调用的 Click
事件处理程序取决于按下的特定键。如果用户单击该按钮而不是使用该键,然后随后尝试使用该键,则该键(例如向下箭头)将充当制表符循环,在窗体上的每个按钮控件之间更改焦点(而不是执行Keydown 处理程序)。
有什么想法吗?
I'm using WinForms. I've created an event handler for the KeyDown
event of the main form, thereby invoking a button's Click
event handler.
The Click
event handler called is dependent upon the specific key pressed. If a user clicks the button rather than using the key, and then subsequently tries to use the key thereafter, the key (down arrow for example) acts as a tab-cycle, changing focus between each button control on the form (rather than executing the Keydown handler).
Any ideas ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
问题是,按钮在被单击时具有焦点,因此后续的按键不会被表单本身捕获,而是被按钮捕获。在按钮的单击事件处理程序中,将窗体聚焦:
这样,焦点将恢复到窗体,以便窗体将侦听按键事件。
编辑
正如您所发现的,真正的问题是箭头键不被视为输入键。要解决此问题,您需要创建一个新类来继承您要使用的任何控件。然后,您重写
IsInputKey
方法以将箭头键视为输入键。检查此链接:http://bytes.com/topic /c-sharp/answers/517530-trapping-arrow-keys-usercontrol。本文也很有用: http://msdn .microsoft.com/en-us/library/system.windows.forms.control.isinputkey.aspx。The problem is, the button has the focus when it is clicked, so subsequent key presses are not caught by the form itself, but by the buttons instead. In the click event handler for the buttons, focus the form:
That way, focus is restored to the form so the form will listen for the keypress events.
Edit
The real problem, as you have discovered, is that arrow keys are not treated as input keys. To fix this, you need to create a new class that inherits whatever control you want to use. Then, you override the
IsInputKey
method to treat arrow keys as input keys. Check this link: http://bytes.com/topic/c-sharp/answers/517530-trapping-arrow-keys-usercontrol. This article is also useful: http://msdn.microsoft.com/en-us/library/system.windows.forms.control.isinputkey.aspx.根据 SimpleCoder,我必须重写 Button 类的
IsInputKey
成员。然后我需要使用这个新类实例化我的按钮对象(在设计器类中),如下所示:
接下来,我为每个新按钮对象注册了
OnClick
处理程序,如下所示:(等等)
并注册主窗体的
KeyDown
处理程序:将主窗体属性
KeyPreview
设置为 true,并且看起来好像我已经覆盖了向上、向下、向左和右键,按钮控件不再循环焦点,而是返回 true,将控制权转移回主窗体。从这里开始,如果按下后续键(上、下、左或右),表单将执行适当的处理程序。As per SimpleCoder, I had to override the
IsInputKey
member for the Button class.Then I needed to instantiate my button objects (in the designer class) using this new class, like so:
Next I registered
OnClick
handlers for each of the new button objects like so:(etc.)
And registered a
KeyDown
handler for the main form:Having set the main form property
KeyPreview
to true, and seeing as though I had overridden the default behaviour of the Up, Down, Left and Right keys, the button controls no longer cycle focus, but rather return true, transferring control back to the main form. From here, if subsequent keys (up, down, left or right) are pressed, the form actions the appropriate handler.使用 Control.GetNextControl 并设置焦点它返回什么。
Use Control.GetNextControl and Set Focus to what it returns.
查看 KeyPreview 属性。 http://msdn.microsoft.com/en -us/library/system.windows.forms.form.keypreview.aspx
将其设置为
true
。或者您可能想要覆盖表单的
ProcessKeyPreview
。Look at KeyPreview property. http://msdn.microsoft.com/en-us/library/system.windows.forms.form.keypreview.aspx
Set it to
true
.Or alternatively you may want to override
ProcessKeyPreview
of the form.