如何捕获用户按下修饰键的时间?
即使我的控件没有焦点,我也想捕获按下 ALT 键的情况。
System.Windows.Forms.Control
中是否有类似的内容?
public void OnModifierKeyPressed(KeyEventArgs e);
或者也许处理任何 WndProc 消息?
提前致谢。
编辑:
当用户按下 ALT 键以在控件中绘制加速键下划线时(就像按钮一样),我需要捕获。我确信当用户按下 ALT 并且控件没有焦点时,以下消息会发送到控件:
WndProcMessage as integer:
296
8235
15
133
20
EDIT2:
最后,我找到了涉及的消息, 这里:
Msg: WM_UPDATEUISTATE 0x0128
WParam: UISF_HIDEACCEL 0x2
但是,作为Cody Gray说,这不是必需的,你可以使用Control.ShowKeyboardCues
属性。
I want to capture when the ALT key has been pressed even if my control hasn't the focus.
Is there something similar to this in System.Windows.Forms.Control
?
public void OnModifierKeyPressed(KeyEventArgs e);
Or maybe processing any WndProc message?
Thanks in advance.
EDIT:
I need to capture when the user pressed the ALT key to paint the key accelerator underline in my control (as a button does). I'm sure that the following messages are sent to the control when the user press ALT and the control doesn't have the focus:
WndProcMessage as integer:
296
8235
15
133
20
EDIT2:
Finally, I found the message that is involved, here:
Msg: WM_UPDATEUISTATE 0x0128
WParam: UISF_HIDEACCEL 0x2
But, as Cody Gray said, this is not needed, you can use Control.ShowKeyboardCues
property.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只有具有焦点的控件才会接收键盘事件。因此,没有方法可以覆盖自定义控件,也没有可以处理事件的方法,可以让您在自定义控件当前没有焦点时检测按键操作。
无论如何,添加到您的问题中的新信息表明这是无关紧要的。如果您只想在适当的时间绘制键盘加速器,那么有一个更简单的解决方案。
在自定义控件的
Paint
事件处理程序(在其中绘制控件的文本)中,您应该检查Control.ShowKeyboardCues
属性。如果值为true
,那么您应该使键盘加速器可见;否则,您应该省略绘制它们。同样,您还应该检查 的值
Control.ShowFocusCues
属性。这告诉您是否在控件周围绘制焦点矩形。使用
ControlPaint.DrawFocusRectangle
方法 绘制所述焦点矩形。类似于:
(我面前没有.NET编译器,所以代码可能有错误......)
Only the control with the focus will receive keyboard events. So there is no method to override or event to handle on your custom control that will allow you to detect key presses when your custom control does not currently have the focus.
At any rate, the new information added to your question indicates that this is irrelevant. If all you want to do is draw the keyboard accelerators at the appropriate time, there's a much simpler solution.
In the
Paint
event handler for your custom control (where you draw the control's text), you should check the value of theControl.ShowKeyboardCues
property. If the value istrue
, then you should make keyboard accelerators visible; otherwise, you should omit drawing them.Similarly, you should also be checking the value of the
Control.ShowFocusCues
property. That tells you whether to draw the focus rectangle around the control.Use the
ControlPaint.DrawFocusRectangle
method to draw said focus rectangle.Something like:
(I don't have a .NET compiler in front of me, so the code might have errors...)