受保护的覆盖无效OnKeyUp未被调用 [英] protected override void OnKeyUp not being called

查看:114
本文介绍了受保护的覆盖无效OnKeyUp未被调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试捕获键入事件.由于某些原因,在C#中无法使用常规的KeyUp事件处理程序.我要执行的操作是在抬起Alt键时运行一些代码.我尝试使用此:

I am attempting to catch a keyup event. For some reason using the regular KeyUp event handler is not working in C#. What I am wanting to do is run some code when the Alt key is lifted. I tried using this:

protected override void OnKeyUp(KeyEventArgs e)<br />
{<br />
    base.OnKeyUp(e);<br />
}<br />


并在base.OnKeyUp(e)的行上放置一个断点,无论按下什么,调试器都不会停在那里.我还尝试对我可以找到的每个关键事件使用替代.唯一成功的方法是ProcessDialogKey,但在keyData下给出的唯一信息是RButton |. Shift键| Alt并没有指示它是KeyDown还是KeyUp,而且我甚至不确定它是否适用于KeyUp.有什么建议吗?

我用以下方法在ProcessDialogKey上做了进一步的测试:


and placed a break point on the line of base.OnKeyUp(e) and no matter what was pressed the debugger wouldn''t stop there. I have also attempted using an override for about every key event I could find. The only one that had any success was ProcessDialogKey but the only information it gave under keyData was RButton | Shiftkey | Alt which gives no indication whether it was a KeyDown or KeyUp, and I''m not even sure it would work for KeyUp. Any suggestions?

I did further testing on ProcessDialogKey with this:

int x = 0;<br />
protected override bool ProcessDialogKey(Keys keyData)<br />
{<br />
    if (keyData.ToString().Contains("Alt") == true && x == 0)<br />
        x += 1;<br />
    else<br />
x = 2;<br />
    return base.ProcessDialogKey(keyData);<br />
}<br />


并将断点放在x = 2的行上,以尝试查看这是否适用于KeyUp和KeyDown.调试器没有在KeyUp事件的这一行停止,而是在我第二次按Alt键时停止了.


and placed the break point on the line of x = 2 to attempt to see if this worked for KeyUp and KeyDown. The debugger did not stop at this line for the KeyUp event but did stop the second time I pushed the Alt key.

推荐答案

您是否尝试添加myForm.KeyPreview = true;


由于Alt用于某些与系统相关的事情,因此有时会出现问题,并且其处理方式与普通键有所不同. 请参阅此处 [
Since Alt is used for some system related things, it sometimes gives problems and has to be handled somewhat differently than normal keys. See here[^]


检查键事件是否似乎被路由到另一个控件.
Check if the key event appears to be getting routed to another control.


这篇关于受保护的覆盖无效OnKeyUp未被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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