如何捕获用户按下修饰键的时间?

发布于 2024-12-22 17:22:29 字数 794 浏览 0 评论 0原文

即使我的控件没有焦点,我也想捕获按下 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不顾 2024-12-29 17:22:29

只有具有焦点的控件才会接收键盘事件。因此,没有方法可以覆盖自定义控件,也没有可以处理事件的方法,可以让您在自定义控件当前没有焦点时检测按键操作。

无论如何,添加到您的问题中的新信息表明这是无关紧要的。如果您只想在适当的时间绘制键盘加速器,那么有一个更简单的解决方案。

在自定义控件的 Paint 事件处理程序(在其中绘制控件的文本)中,您应该检查 Control.ShowKeyboardCues 属性。如果值为true,那么您应该使键盘加速器可见;否则,您应该省略绘制它们。

同样,您还应该检查 的值Control.ShowFocusCues 属性。这告诉您是否在控件周围绘制焦点矩形。
使用 ControlPaint.DrawFocusRectangle 方法 绘制所述焦点矩形。

类似于:
(我面前没有.NET编译器,所以代码可能有错误......)

// Draw the text
using (StringFormat sf = new StringFormat())
{
    sf.Alignment = StringAlignment.Center;
    sf.LineAlignment = StringAlignment.Center;
    sf.HotkeyPrefix = this.ShowKeyboardCues ? HotkeyPrefix.Show : HotKeyPrefix.Hide;

    if (this.Enabled)
    {
        using (Brush br = new SolidBrush(this.ForeColor))
        {
            g.DrawString(this.Text, this.Font, br, this.ClientRectangle, sf);
        }
    }
    else
    {
        SizeF sz = g.MeasureString(this.Text, this.Font, Point.Empty, sf);
        RectangleF rc = new RectangleF(Point.Empty, sz);
        ControlPaint.DrawStringDisabled(g, this.Text, this.Font, this.BackColor, rc, sf);
    }
 }

 // Draw the focus rectangle
 if (this.ShowFocusCues && this.ContainsFocus)
 {
     ControlPaint.DrawFocusRectangle(g, this.ClientRectangle);         
 }

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 the Control.ShowKeyboardCues property. If the value is true, 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...)

// Draw the text
using (StringFormat sf = new StringFormat())
{
    sf.Alignment = StringAlignment.Center;
    sf.LineAlignment = StringAlignment.Center;
    sf.HotkeyPrefix = this.ShowKeyboardCues ? HotkeyPrefix.Show : HotKeyPrefix.Hide;

    if (this.Enabled)
    {
        using (Brush br = new SolidBrush(this.ForeColor))
        {
            g.DrawString(this.Text, this.Font, br, this.ClientRectangle, sf);
        }
    }
    else
    {
        SizeF sz = g.MeasureString(this.Text, this.Font, Point.Empty, sf);
        RectangleF rc = new RectangleF(Point.Empty, sz);
        ControlPaint.DrawStringDisabled(g, this.Text, this.Font, this.BackColor, rc, sf);
    }
 }

 // Draw the focus rectangle
 if (this.ShowFocusCues && this.ContainsFocus)
 {
     ControlPaint.DrawFocusRectangle(g, this.ClientRectangle);         
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文