C# - 检测如果打开一个上下文菜单时SHIFT键举行 [英] C# - Detecting if the SHIFT key is held when opening a context menu

查看:190
本文介绍了C# - 检测如果打开一个上下文菜单时SHIFT键举行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的C#应用​​程序,我想显示上下文菜单,但我想,如果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.

我目前使用的函数GetKeyState API来检查SHIFT键。它工作正常,在我的电脑上,但用户使用非英文Windows说它不会为他们在所有的工作。

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.

我也看了,在Win32 API的当上下文打开菜单有指示菜单应显示 EXTENDEDVERBS 的标志。在C#中的 EventArgs的打开事件不包含(从我可以告诉)属性说明 EXTENDEDVERBS 或任何修饰键被按下。

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.

下面是现在,我使用了<$ C $里面的代码C>打开事件:

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);
 }

如果函数GetKeyState是这样做的正确的方式,是我的代码正确检测到按下SHIFT键?

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

推荐答案

您可以使用的上控制ModifierKeys静态属性,以确定是否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.

另外请注意,这将检查是否Shift键的时刻举行您检查值。不是一时当菜单打开已启动。这可能不是您的应用显著差异,但值得注意。

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.

这篇关于C# - 检测如果打开一个上下文菜单时SHIFT键举行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆