【发布时间】:2020-05-13 04:01:35
【问题描述】:
如何使用 C# 检测游戏(英雄联盟)中是否按下了某个键?
我以前很容易用 AHK 做到这一点,但我想转向 c#,因为我最近正在学习它。
大家有什么建议吗?
【问题讨论】:
标签: c#
如何使用 C# 检测游戏(英雄联盟)中是否按下了某个键?
我以前很容易用 AHK 做到这一点,但我想转向 c#,因为我最近正在学习它。
大家有什么建议吗?
【问题讨论】:
标签: c#
我的朋友实现了线程级鼠标/键盘挂钩。
只需安装 Nuget 包并给我一些反馈。
Install-Package -IncludePrerelease Winook
例子:
var processes = Process.GetProcessesByName("LeagueOfLegendProcessName");
_process = processes.FirstOrDefault();
if (_process == null)
{
return;
}
_keyboardHook = new KeyboardHook(_process.Id);
_keyboardHook.MessageReceived += KeyboardHook_MessageReceived;
...
private void KeyboardHook_MessageReceived(object sender, KeyboardMessageEventArgs e)
{
Debug.WriteLine($"Keyboard Virtual Key Code: {e.VirtualKeyCode}; Flags: {e.Flags:x}");
}
【讨论】:
我建议您始终在文档中搜索并仔细阅读。
Bellow 是一个基于 Microsoft 文档的示例
// Uses the Keyboard.IsKeyDown to determine if a key is down.
// e is an instance of KeyEventArgs.
if (Keyboard.IsKeyDown(Key.Return))
{
btnIsDown.Background = Brushes.Red;
}
else
{
btnIsDown.Background = Brushes.AliceBlue;
}
不要忘记导入“System.Windows.Input”命名空间。
编辑: 此问题在此链接中得到解答:Trying to detect keypress
【讨论】:
看看SetWindowsHookExA,它会让你挂钩鼠标和kb事件并查看pinvoke.net以了解如何做。
【讨论】: