views:

321

answers:

2

In AutoIt v3 there's a function called: HotKeySet. It sets a hotkey that calls a user function when pressed. It's system wide hotkey meaning that the key when hotkey is set can't be used for anything else.

Basically I would like to catch ESC or any other key like $ ` etc and when user presses it anywhere even outside of the app it should let me know.

For example i would do like HotKeySet({ESC}) inside loop and when it's done program would wait for that key to be pressed before.

 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 Hotkey Here
                SetHotkey(`);
                //Wait for hotkey to press when it's pressed... Execute some stuff 

                Thread.Sleep(5000); // simulate work...
                Program.windowStateChange("Hide");
            } else {
                wh.WaitOne(); // No more tasks - wait for a signal
            }
        }
    }
A: 

I think you need to setup a system hook...

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

m0sa
I've seen that link. It doesn't seem to block the input but just logs what you type. So If you lets say have app that requires you to act on pressing 'y' and you are in Word typing, then when you press y it will actually be put in Word document. I would like to have system wide HotKey that takes away the option to use that key for anything else (for the time it's set of course).
MadBoy
+1  A: 

You need to take a look at RegisterHotKey in user32.dll. Here's a good example at pinvoke.net: http://www.pinvoke.net/default.aspx/user32/RegisterHotKey.html

JohnForDummies
Seems like it's exactly what i need :-) Thanks a bunch!
MadBoy