C# - 检测打开上下文菜单时是否按住 SHIFT 键
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/973721/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# - Detecting if the SHIFT key is held when opening a context menu
提问by Chris Thompson
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.
在我的 C# 应用程序中,我想显示一个上下文菜单,但如果在打开上下文菜单时按住 SHIFT 键,我想向菜单添加特殊选项。
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.
我目前正在使用GetKeyState
API 来检查 SHIFT 键。它在我的电脑上运行良好,但使用非英语 Windows 的用户说它对他们根本不起作用。
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.
我还在 Win32 API 中读到,当打开上下文菜单时,有一个标志指示菜单中应显示EXTENDEDVERBS
. 在 C# 中EventArgs
,Opening
事件不包含(据我所知)指示EXTENDEDVERBS
或是否按下任何修饰键的属性。
Here's the code I'm using now inside the "Opening
" event:
这是我现在在“ 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);
}
If GetKeyState is the right way of doing it, is my code properly detecting the SHIFT key being pressed?
如果 GetKeyState 是正确的做法,我的代码是否正确检测到按下的 SHIFT 键?
采纳答案by JaredPar
You can use the ModifierKeys static propertyon control to determine if the shift key is being held.
您可以使用控件上的ModifierKeys 静态属性来确定是否按住了 shift 键。
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.
另请注意,这将检查在您检查值时是否按住 Shift 键。不是启动菜单打开的那一刻。对于您的应用程序来说,这可能没有显着差异,但值得注意。
回答by bobbyalex
Use this to detect if the shift key is pressed:
使用它来检测是否按下了 shift 键:
if ((Control.ModifierKeys & Keys.Shift) == Keys.Shift)
回答by Héctor Espí Hernández
In silverlight, at least in latest versions, you must use:
在 Silverlight 中,至少在最新版本中,您必须使用:
if(Keyboard.Modifiers == ModifierKeys.Shift) {
...
}
回答by Stephen Punak
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
只要确保您的项目引用 PresentationCore 和 WindowsBase
回答by FastHyman
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.
Keyboard.Modifiers
也适用于实际的 WPF 项目!
另外我建议它使用 overKeyboard.GetKeyStates
因为后者使用触发并且可能无法反映真实的关键状态。
Also be aware that this will trigger ONLY if the shiftmodifier key is down and nothing else:
还要注意,这只会在shift修饰键按下时触发,没有别的:
if(Keyboard.Modifiers == ModifierKeys.Shift)
{ ... }
If you just want to detect if the shiftkey is down, whether another modifier key is pressed or not, use this one:
如果您只想检测shift按键是否按下,是否按下了另一个修饰键,请使用以下方法:
if((Keyboard.Modifiers & ModifierKeys.Shift) == ModifierKeys.Shift)
{ ... }