Best way to tackle global hotkey processing in c#?

2019-01-03 09:51发布

Possible Duplicate:
How can I register a global hot key to say CTRL+SHIFT+(LETTER) using WPF and .NET 3.5?

I'd like to have multiple global hotkeys in my new app (to control the app from anywhere in windows), and all of the given sources/solutions I found on the web seem to provide with a sort of a limping solution (either solutions only for one g.hotkey, or solutions that while running create annoying mouse delays on the screen).

Does anyone here know of a resource that can help me achive this, that I can learn from? Anything?

Thanks ! :)

3条回答
够拽才男人
2楼-- · 2019-01-03 10:26
一夜七次
3楼-- · 2019-01-03 10:27

I would handle this by using P/Invoke to call RegisterHotKey() for each hotkey, and then use NativeForm (assuming you are using WinForms) to be notified of the WM_HOTKEY message. This should keep most of your hotkey code in one place.

查看更多
Lonely孤独者°
4楼-- · 2019-01-03 10:32

The nicest solution I've found is http://bloggablea.wordpress.com/2007/05/01/global-hotkeys-with-net/

Hotkey hk = new Hotkey();

hk.KeyCode = Keys.1;
hk.Windows = true;
hk.Pressed += delegate { Console.WriteLine("Windows+1 pressed!"); };

hk.Register(myForm); 

Note how you can set different lambdas to different hotkeys

查看更多
登录 后发表回答