Proper way of creating a background hotkey with WPF C#? (preferably without using legacy code)

北城以北 提交于 2019-12-23 23:19:15

问题


I need to be able to set up a background hotkey event for a WPF application. (by background I mean that the application in question might not necessary be in focus).

I know that with WinForms the proper way of doing it would be to import user32.dll, and use RegisterHotKey, and the catch the appropriate WM_POST message. I'm convinced that there has to be a better solution with WPF, then using user32.dll, but I have been unable to find anything. Any suggestions? Thanks


回答1:


I don't think WPF has any built in support for hotkeys, at least I couldn't find any and I used RegisterHotKey directly.




回答2:


This has an excellent example (see 5.2 "Registering the System Hotkey"):

http://blog.280z28.org/archives/2007/03/15/

It uses the Managed Windows API, which is a great wrapper around a lot of the legacy api calls.




回答3:


Why not using Commanding infrastructure provided by WPF? You could try assigning some command bindings for you main window and provide some key gestures for it...




回答4:


You either have to use WinForm like you said or Win32.

I have made a solution in a class (Win32) that you can use directly in a wpf solution. Just put the class in a separate DLL and any calls to win32 will be transparent to you (and will let you continue to use edit and continue properly). Hope it helps.

enter link description here



来源:https://stackoverflow.com/questions/633362/proper-way-of-creating-a-background-hotkey-with-wpf-c-preferably-without-usin

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!