Global Keyhook in C#

2019-07-21 03:51发布

问题:

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.

回答1:

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.