在C#中通过钩子重定向keydown?
我想编写一个像游戏热键助手这样的应用程序,例如,在魔兽争霸3中,我出于某种原因使用小键盘键。是否可以通过按“X”键向操作系统发送“Numpad 1 key pressed”消息?我一直在尝试使用钩子来做到这一点,但我无法弄清楚。我希望我说清楚了。
我已将此类用于挂钩:.NET 中的全局系统挂钩。
简单来说我的代码是这样的:
globalKeyboardHook hook = new globalKeyboardHook(); // A Global Hook
private void btnHook_Click(object sender, EventArgs e)
{
hook.hook(); // Set the hook
hook.HookedKeys.Add(Keys.X); // Hook the X key
// i removed other hooks purify the code, six hooks total.
hook.KeyDown += new KeyEventHandler(hook_KeyDown); // add eventhandler
Program.hookActive = true; // inform the program that hook is active.
Guncelle(); // update form components (buttons enabling/disabling according to hook status etc.)
}
private void hook_KeyDown(object sender, KeyEventArgs e)
{
KeyEventArgs k = new KeyEventArgs(Keys.NoName);
switch (e.KeyCode)
{
case Keys.X:
k = new KeyEventArgs(Keys.NumPad1);
OnKeyDown(k);
break;
// i removed other cases to purify the code, six cases total.
}
}
I want to write an application like hotkey helpers for games, for example, in Warcraft 3, I use numpad keys for some reason. Is it possible to send a "Numpad 1 key pressed" message to the OS by pressing the "X" key? I've been trying to do this using hooks but I couldn't figure out. I hope I was clear.
I've used this class for hooks: Global System Hooks in .NET.
Simply my code is like this:
globalKeyboardHook hook = new globalKeyboardHook(); // A Global Hook
private void btnHook_Click(object sender, EventArgs e)
{
hook.hook(); // Set the hook
hook.HookedKeys.Add(Keys.X); // Hook the X key
// i removed other hooks purify the code, six hooks total.
hook.KeyDown += new KeyEventHandler(hook_KeyDown); // add eventhandler
Program.hookActive = true; // inform the program that hook is active.
Guncelle(); // update form components (buttons enabling/disabling according to hook status etc.)
}
private void hook_KeyDown(object sender, KeyEventArgs e)
{
KeyEventArgs k = new KeyEventArgs(Keys.NoName);
switch (e.KeyCode)
{
case Keys.X:
k = new KeyEventArgs(Keys.NumPad1);
OnKeyDown(k);
break;
// i removed other cases to purify the code, six cases total.
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,您可以使用 WinAPI
SendInput()
或keyb_event()
调用。例如:Yes, you can use the WinAPI
SendInput()
orkeyb_event()
calls. For example: