如何检测 WPF 中的修饰键状态?

发布于 2024-11-02 19:13:18 字数 127 浏览 3 评论 0原文

是否有一些全局构造可供我在需要访问 Control、Shift、Alt 按钮是否按下时使用?例如,在 TreeViewMouseDown 事件中。

如果是这样怎么办?

Is there some global constructs that I can use whenever I need to access whether the Control, Shift, Alt buttons are down? For instance inside MouseDown event of a TreeView.

If so how?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(6

纸短情长 2024-11-09 19:13:18

使用类 键盘。使用 Keyboard.IsKeyDown 您可以检查 Control、Shift、Alt 现在是否已按下。

对于 Shift:

if (Keyboard.IsKeyDown(Key.LeftShift) || Keyboard.IsKeyDown(Key.RightShift))
{ /* Your code */ }

对于 Control:

if (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl))
{ /* Your code */ }

对于 Alt:

if (Keyboard.IsKeyDown(Key.LeftAlt) || Keyboard.IsKeyDown(Key.RightAlt))
{ /* Your code */ }

Use class Keyboard. Using Keyboard.IsKeyDown you can check if Control, Shift, Alt is down now.

For Shift:

if (Keyboard.IsKeyDown(Key.LeftShift) || Keyboard.IsKeyDown(Key.RightShift))
{ /* Your code */ }

For Control:

if (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl))
{ /* Your code */ }

For Alt:

if (Keyboard.IsKeyDown(Key.LeftAlt) || Keyboard.IsKeyDown(Key.RightAlt))
{ /* Your code */ }
上课铃就是安魂曲 2024-11-09 19:13:18

还有:

// Have to get this value before opening a dialog, or user will have released the control key
if ((Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control)
{

}

There's also:

// Have to get this value before opening a dialog, or user will have released the control key
if ((Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control)
{

}
ヅ她的身影、若隐若现 2024-11-09 19:13:18
    private bool IsShiftKey { get; set; }

    private void OnPreviewKeyDown(object sender, KeyEventArgs e)
    {
        IsShiftKey = Keyboard.Modifiers == ModifierKeys.Shift ? true : false;

        if ((Key.Oem3 == e.Key || ((IsShiftKey && Key.Oem4 == e.Key) || (IsShiftKey && Key.Oem6 == e.Key) || (IsShiftKey && Key.Oem5 == e.Key)) && (validatorDefn as FormatValidatorDefinition).format == "packedascii"))
        {
           e.Handled = true;
        }
    }
    private bool IsShiftKey { get; set; }

    private void OnPreviewKeyDown(object sender, KeyEventArgs e)
    {
        IsShiftKey = Keyboard.Modifiers == ModifierKeys.Shift ? true : false;

        if ((Key.Oem3 == e.Key || ((IsShiftKey && Key.Oem4 == e.Key) || (IsShiftKey && Key.Oem6 == e.Key) || (IsShiftKey && Key.Oem5 == e.Key)) && (validatorDefn as FormatValidatorDefinition).format == "packedascii"))
        {
           e.Handled = true;
        }
    }
韶华倾负 2024-11-09 19:13:18

这就是我处理它的方式(使用 PreviewKeyDown),假设我们正在寻找 Alt + R...

private void OnPreviewKeyDown(object sender, KeyEventArgs e)
{
    if ((Keyboard.IsKeyDown(Key.LeftAlt) || Keyboard.IsKeyDown(Key.RightAlt)
       && e.SystemKey == Key.R)
    {
       //do whatever
    }
}

也许有人可以澄清为什么我必须使用 e.SystemKey 而不仅仅是 e.Key,也许是由于修饰符?但这在搜索修饰符+键时对我来说完美无缺。

This is how I handle it (using PreviewKeyDown), let's say we are looking for Alt + R...

private void OnPreviewKeyDown(object sender, KeyEventArgs e)
{
    if ((Keyboard.IsKeyDown(Key.LeftAlt) || Keyboard.IsKeyDown(Key.RightAlt)
       && e.SystemKey == Key.R)
    {
       //do whatever
    }
}

Maybe someone can clear up why I had to use e.SystemKey and not just e.Key, maybe due to the modifier? but this has worked flawlessly for me when searching for modifier+key.

一瞬间的火花 2024-11-09 19:13:18

部分借用@Josh,有点类似于@Krushik,还引用了有关 KeyEventArgs.systemKey 和 KeyEventArgs.Key 之间的区别(回答为什么 Josh 必须使用 SystemKey);其中,使用修饰键(例如 Alt)时,e.Key 返回 Key.System,因此“真实”键位于 e.SystemKey 内。

解决这个问题的方法是首先获取“真实”密钥,然后执行条件:

private void OnPreviewKeyDown(object sender, KeyEventArgs e)
{
    // Fetch the real key.
    var key = e.Key == Key.System ? e.SystemKey : e.Key;

    if ((Keyboard.IsKeyDown(Key.LeftAlt) || Keyboard.IsKeyDown(Key.RightAlt))
        && key == Key.Return)
    {
        // Execute your code.
    }
}

Partly borrowing from @Josh, and somewhat similar to @Krushik, and also referencing a question about the Difference between KeyEventArgs.systemKey and KeyEventArgs.Key (answering why Josh has to use SystemKey); wherein, with modifier keys (such as Alt), e.Key returns Key.System, and thus the 'real' key is within e.SystemKey.

A way around this, is to first fetch the 'real' key, and then do your conditional:

private void OnPreviewKeyDown(object sender, KeyEventArgs e)
{
    // Fetch the real key.
    var key = e.Key == Key.System ? e.SystemKey : e.Key;

    if ((Keyboard.IsKeyDown(Key.LeftAlt) || Keyboard.IsKeyDown(Key.RightAlt))
        && key == Key.Return)
    {
        // Execute your code.
    }
}
芯好空 2024-11-09 19:13:18

还有:

如果 My.Computer.Keyboard.ShiftKeyDown 那么 ...

My.Computer.Keyboard.CtrlKeyDown

My.Computer.Keyboard.AltKeyDown

and as well:

if My.Computer.Keyboard.ShiftKeyDown then ...

My.Computer.Keyboard.CtrlKeyDown

My.Computer.Keyboard.AltKeyDown

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文