How to install HotKey on a system in C #

AutoIt v3 has a HotKeySet feature. It sets a hotkey that calls the user function when pressed. This is a system hotkey, meaning that the key cannot be used for anything else when the hotkey is installed.

Basically, I would like to catch Esc or any other key, for example $ , ` , etc., and when the user presses it anywhere, even outside the application, he should inform me.

For example, I would do like HotKeySet ({ESC}) inside a loop, and when this is done, the program will wait until this key is pressed earlier.

 public static void work() { while (true) { string task = null; lock (locker) if (tasks.Count > 0) { task = tasks.Dequeue(); if (task == null) { return; } } if (task != null) { //MessageBox.Show("Performing task: " + task); Program.mainAnnounceWindow.setLogTextBox(task); Program.mainAnnounceWindow.setLogTrayTip(task); Program.windowStateChange("Show"); // Set the hotkey here SetHotkey(`); //Wait for hotkey to press when it pressed... Execute some stuff Thread.Sleep(5000); // Simulate work... Program.windowStateChange("Hide"); } else { wh.WaitOne(); // No more tasks - wait for a signal } } } 
+2
c # windows
source share
2 answers

You need to take a look at RegisterHotKey in user32. dll Here is a good example on pinvoke.net, RegisterHotKey (user32) .

+3
source share

I think you need to set up a system hook ...

Here is an example: http://www.axino.net/tutorial/2009/02/keylogger-in-c-introduction

0
source share

All Articles