C# - 检测打开上下文菜单时是否按住 SHIFT 键

发布于 2024-07-24 03:45:47 字数 853 浏览 2 评论 0原文

在我的 C# 应用程序中,我想显示上下文菜单,但如果在打开上下文菜单时按住 SHIFT 键,我想向菜单添加特殊选项。

我当前正在使用 GetKeyState API 来检查 SHIFT 键。 它在我的计算机上运行良好,但使用非英语 Windows 的用户表示它根本不适合他们。

我还读到,在 Win32 API 中,当打开上下文菜单时,有一个标志指示菜单中应显示 EXTENDEDVERBS。 在 C# 中,Opening 事件的 EventArgs 不包含(据我所知)指示 EXTENDEDVERBS 的属性,或者是否有任何修饰键按下。

这是我现在在“Opening”事件中使用的代码:

// SHIFT KEY is being held down
if (Convert.ToBoolean(GetKeyState(0x10) & 0x1000))
{
     _menuStrip.Items.Add(new ToolStripSeparator());

     ToolStripMenuItem log = new ToolStripMenuItem("Enable Debug Logging");
     log.Click += new EventHandler(log_Click);
     log.Checked = Settings.Setting.EnableDebugLogging;
     _menuStrip.Items.Add(log);
 }

如果 GetKeyState 是正确的方法,那么我的代码是否可以正确检测按下的 SHIFT 键?

In my C# application I want to display a context menu, but I want to add special options to the menu if the SHIFT key is being held down when the context menu is opened.

I'm currently using the GetKeyState API to check for the SHIFT key. It works fine on my computer but users with non-English Windows say it doesn't work at all for them.

I also read that in the Win32 API when a context menu is opened there's a flag that indicates in the menu should show EXTENDEDVERBS. In C# the EventArgs for the Opening event doesn't contain (from what I can tell) a property indicating EXTENDEDVERBS or if any modifier keys are pressed.

Here's the code I'm using now inside the "Opening" event:

// SHIFT KEY is being held down
if (Convert.ToBoolean(GetKeyState(0x10) & 0x1000))
{
     _menuStrip.Items.Add(new ToolStripSeparator());

     ToolStripMenuItem log = new ToolStripMenuItem("Enable Debug Logging");
     log.Click += new EventHandler(log_Click);
     log.Checked = Settings.Setting.EnableDebugLogging;
     _menuStrip.Items.Add(log);
 }

If GetKeyState is the right way of doing it, is my code properly detecting the SHIFT key being pressed?

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

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

发布评论

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

评论(5

你穿错了嫁妆 2024-07-31 03:45:47

您可以使用 ModifierKeys 静态属性控制以确定是否按住 Shift 键。

if (Control.ModifierKeys == Keys.Shift ) { 
  ...
}

这是一个标志样式的枚举,因此根据您的情况,您可能需要进行更严格的测试。

另请注意,这将检查您检查值时是否按住 Shift 键。 不是菜单打开的那一刻。 对于您的应用程序来说,这可能不是显着差异,但值得注意。

You can use the ModifierKeys static property on control to determine if the shift key is being held.

if (Control.ModifierKeys == Keys.Shift ) { 
  ...
}

This is a flag style enum though so depending on your situation you may want to do more rigorous testing.

Also note that this will check to see if the Shift key is held at the moment you check the value. Not the moment when the menu open was initiated. That may not be a significant difference for your application but it's worth noting.

无戏配角 2024-07-31 03:45:47

使用它来检测是否按下了 Shift 键:

if ((Control.ModifierKeys & Keys.Shift) == Keys.Shift) 

Use this to detect if the shift key is pressed:

if ((Control.ModifierKeys & Keys.Shift) == Keys.Shift) 
初心 2024-07-31 03:45:47

它实际上比任何一个都简单得​​多

            if( Keyboard.IsKeyDown(Key.LeftCtrl) || 
                Keyboard.IsKeyDown(Key.RightCtrl) ||
                Keyboard.IsKeyDown(Key.LeftAlt) ||
                Keyboard.IsKeyDown(Key.RightAlt) ||
                Keyboard.IsKeyDown(Key.LeftShift) ||
                Keyboard.IsKeyDown(Key.RightShift))
            {
                /** do something */
            }

只需确保您的项目引用PresentationCore和WindowsBase

It's actually much simpler than any of that

            if( Keyboard.IsKeyDown(Key.LeftCtrl) || 
                Keyboard.IsKeyDown(Key.RightCtrl) ||
                Keyboard.IsKeyDown(Key.LeftAlt) ||
                Keyboard.IsKeyDown(Key.RightAlt) ||
                Keyboard.IsKeyDown(Key.LeftShift) ||
                Keyboard.IsKeyDown(Key.RightShift))
            {
                /** do something */
            }

Just make sure your project references PresentationCore and WindowsBase

ま柒月 2024-07-31 03:45:47

Keyboard.Modifiers 也适用于实际的 WPF 项目!
另外,我建议使用它而不是 Keyboard.GetKeyStates 因为后者使用触发并且可能无法反映真实的按键状态。

另请注意,仅当 shift 修饰键按下且没有其他任何操作时才会触发:

if(Keyboard.Modifiers == ModifierKeys.Shift)
{ ... }

如果您只想检测 shift 键是否按下,是否有另一个修饰键按下或不按下,使用这个:

if((Keyboard.Modifiers & ModifierKeys.Shift) == ModifierKeys.Shift)
{ ... }

Keyboard.Modifiers also works with actual WPF projects!
Also I would recommend it's use over Keyboard.GetKeyStates because the latter uses triggering and may not reflect the real key state.

Also be aware that this will trigger ONLY if the shift modifier key is down and nothing else:

if(Keyboard.Modifiers == ModifierKeys.Shift)
{ ... }

If you just want to detect if the shift key is down, whether another modifier key is pressed or not, use this one:

if((Keyboard.Modifiers & ModifierKeys.Shift) == ModifierKeys.Shift)
{ ... }
清秋悲枫 2024-07-31 03:45:47

在 silverlight 中,至少在最新版本中,您必须使用:

if(Keyboard.Modifiers == ModifierKeys.Shift) {
    ...
}

In silverlight, at least in latest versions, you must use:

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