问题描述
我开始制作一个简短的应用程序来跟踪我在游戏中运行的圈数。我想要完成的是每次我在游戏中完成一圈同时仍然将焦点设置到游戏时使用全局热键来说按Ctrl + A将一圈添加到计数器而不必将焦点更改为计数器。
到目前为止这是我所拥有的
Hi,
I started working on a short application to keep track of laps I run around a course in a game I play. What I am trying to accomplish is that every time I complete a lap in the game while still having the focus set to the game use a global hotkey to say press Ctrl + A to add a lap to the counter without having to change focus to the lap counter.
So far this is what I have
<pre lang="c#">public partial class Form1 : Form
{
KeyboardHook hook = new KeyboardHook();
int count = 0;
public Form1()
{
InitializeComponent();
}
private void AddButton_Click(object sender, EventArgs e)
{
count++;
CounterLabel.Text = count.ToString();
}
我想知道是否有人能够给关于全局关键钩的任何提示。
我花了很多时间浏览msdn和其他一些网站,但仍然无法弄明白到底是什么做。
谢谢,
JosephCj
I was wondering if anyone would be able to give me any tips on the global keyhook.
I've spent quite a bit of time looking through msdn and some other sites but still can't figure out exactly what to do.
Thank you,
JosephCj
推荐答案
private int count = 0;
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
// look for Control A
if (keyData == (Keys.Control| Keys.A))
{
count++;
return true;
}
return base.ProcessCmdKey(ref msg, keyData);
}
请记住,许多操作系统定义的键盘快捷键被许多Windows用户视为理所当然:如果你覆盖像Control-A这样的标准Win快捷键,你可能会让人感到困惑使用您的应用程序。
Do keep in mind that many OS defined keyboard shortcuts are taken-for-granted by many Windows users: if you over-ride a standard Win shortcut like Control-A you may confuse people using your Application.
这篇关于如何编写全局热键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!