c# - 使用 SendKeys 发送 Windows key

标签 c# keyboard-shortcuts sendkeys

我正在研究 C# 中的快捷方式。我成功地使用 SendKeys 实现了 Ctrl、Alt 和 Shift。

像这样;

Ctrl + C:

System.Windows.Forms.SendKeys.SendWait("^c");

Alt + F4:

System.Windows.Forms.SendKeys.SendWait("%{F4}");

但我无法使用 SendKeys 发送“Windows 键”。我试过 ex: Win + E : .SendWait("#e") 但它不起作用。我应该用什么代替“#”?

谢谢。

最佳答案

好吧,原来你真正想要的是这个:http://inputsimulator.codeplex.com/

它完成了将 Win32 SendInput 方法公开给 C# 的所有艰苦工作。这允许您直接发送 Windows 键。这已经过测试并且有效:

InputSimulator.SimulateModifiedKeyStroke(VirtualKeyCode.LWIN, VirtualKeyCode.VK_E);

注意然而,在某些情况下您希望专门将 key 发送到应用程序(例如ALT+F4),在这种情况下使用Form 库方法。在其他情况下,您通常希望将其发送到操作系统,请使用上面的内容。


将此保留在此处以供引用,它不会在所有操作系统中都有效,并且不会始终按您想要的方式运行。请注意,您正在尝试将这些击键发送到应用程序,而操作系统通常会提前拦截它们。对于 Windows 7 和 Vista,为时过早(在发送 E 之前)。

SendWait("^({ESC}E)")Send("^({ESC}E)")

此处注释:http://msdn.microsoft.com/en-us/library/system.windows.forms.sendkeys.aspx

To specify that any combination of SHIFT, CTRL, and ALT should be held down while several other keys are pressed, enclose the code for those keys in parentheses. For example, to specify to hold down SHIFT while E and C are pressed, use "+(EC)". To specify to hold down SHIFT while E is pressed, followed by C without SHIFT, use "+EC".

请注意,由于您希望同时按下 ESC 和(比如)E,因此需要将它们括在括号中。

关于c# - 使用 SendKeys 发送 Windows key ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10366152/

相关文章:

c# - 如何在主 Silverlight 控件上隐藏和显示 silverlight 用户控件?

c# - 在 WindowsFormsHost 中插入表单时出现 ArgumentException

c# - 从 ISampleGrabber 获取字符串并以第一种形式更新文本框

python - send_keys 到隐藏元素

c# - 如何在 C# 中从指向内存流的 int 指针获取数据?

visual-studio - 在 Visual Studio 中禁用单行复制

intellij-idea - 跳转到 Intellij IDEA 中的编辑器快捷方式

Eclipse 帮助框

java - 单击 Web 应用程序中的另一个元素后,输入的值被删除

c# - Webdriver "sendkeys"方法没有正确输入密码