本文介绍了C#中的全局按键钩的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个新的应用程序,可以通过外部GUI的键进行控制.如果按下该键,则应该首先更改一个文本字段.最后,我想使计时器在keyPress上启动.我用Google搜索并访问了Stack Overflow,但发现没有任何东西可以真正帮助我解决问题.

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.

我在Stack Overflow上找到了帖子,并尝试了代码.但是我只是在控制台中得到了密钥,却不知道如何在焦点不集中的GUI上请求它.

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.

推荐答案

虽然这只是一个链接,但我成功地使用了Code Project文章"在C#中处理全局鼠标和键盘挂钩".

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);

我认为这很容易使用.

更新:

该项目也可以在 CodePlex 来自同一作者的新版本中找到. /a>.

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

这篇关于C#中的全局按键钩的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!