2

I want to make a new Application which I can control via keys from outside GUI. If the key was pressed there should be a text field changing at first. At last I want to make a timer start on keyPress. I googled and visit Stack Overflow but found nothing that could help me really to solve my problem.

I found a post on Stack Overflow and tried the code. But I just get the key in a console and don't know how to request it on a GUI which is out of focus.

Any help would be highly appreciable.

Community
  • 1
  • 1
Gero K
  • 61
  • 1
  • 11

1 Answers1

2

While this is just a link, I successfully used the library from the Code Project article "Processing Global Mouse and Keyboard Hooks in C#" in the past.

You can register your event sinks to be notified about several events like e.g.:

var actHook = new UserActivityHook();

// hang on events

actHook.OnMouseActivity += new MouseEventHandler(MouseMoved);
actHook.KeyDown += new KeyEventHandler(MyKeyDown);
actHook.KeyPress += new KeyPressEventHandler(MyKeyPress);
actHook.KeyUp += new KeyEventHandler(MyKeyUp);

Rather easy to use in my opinion.

Update:

The project is also available in a newer version from the same author, over at CodePlex.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
  • 1
    Thank you, i looks promising. I'll take a look now. – Gero K Mar 24 '13 at 11:48
  • I cut it down to just the KeyDown Listener like [this](http://grabilla.com/03318-c15b6222-e4c6-4961-bef4-013562b484ae.html) but it gave me an error. 'An unhandled exception of type 'System.ComponentModel.Win32Exception' – Gero K Mar 24 '13 at 11:59
  • @GeroK Try to inspect the full exception (including stack trace, error message and inner exeption). – Uwe Keim Mar 24 '13 at 13:03
  • One thing that sucks badly about codeproject.com is the complete lack of review. This project is especially bad, way too many ways for anybody to shoot their leg off. Please do not recommend it. – Hans Passant Mar 24 '13 at 13:20
  • What in particular is bad? I really used it successfully. – Uwe Keim Mar 24 '13 at 13:37
  • @UweKeim What do you mean exactly? I get the error an if I press Break it highlight the last bracket of the Start() method in UserActivityHook.cs -> [Error](http://grabilla.com/03318-59bc6092-9223-4247-ac59-66e175eeee12.html) – Gero K Mar 24 '13 at 14:26