简体   繁体   中英

C# Recognize keys pressed when not in focus

I'm making a program that will shutdown a .exe app when F1 is pressed! 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. Basically the key thing is to import native method SetWindowsHookEx and process key presses with your callback delegate. Last time I used it was on Windows 8.1, so I believe it's still actual for Windows 10.

For this case, you can use NotifyIcon component along with the form. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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