Global Keyhook in C#

泪湿孤枕 提交于 2019-12-12 21:54:30

问题


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.



来源:https://stackoverflow.com/questions/15597890/global-keyhook-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!