简体   繁体   English

C#识别焦点不清晰时按下的键

[英]C# Recognize keys pressed when not in focus

I'm making a program that will shutdown a .exe app when F1 is pressed! 我正在制作一个程序,当按F1键时将关闭.exe应用程序! I did it, but I'm trying to do it without having to focus on the form! 我做到了,但是我试图做到这一点而不必关注表单! How could I do that? 我该怎么办?

You can set up low level keyboard hook, it will notify your app with key press events. 您可以设置低级键盘挂钩,它将通过按键事件通知您的应用。 Check this article , it's almost 10 years old, but should give you a hint. 查看这篇文章 ,它已有10年的历史了,但应该给您提示。 Basically the key thing is to import native method SetWindowsHookEx and process key presses with your callback delegate. 基本上,关键是导入本地方法SetWindowsHookEx并使用您的回调委托处理按键。 Last time I used it was on Windows 8.1, so I believe it's still actual for Windows 10. 我上次使用它是在Windows 8.1上,因此我认为Windows 10仍然适用。

For this case, you can use NotifyIcon component along with the form. 对于这种情况,可以将NotifyIcon组件与表单一起使用。 Add a context menu with a shortcut key (F1 is not supported I think, you need modifiers) which then finds the PID and terminates that process. 添加带有快捷键的上下文菜单(我认为不支持F1,您需要使用修饰符),然后找到PID并终止该过程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM