[英]how to detect key pressed in game with C#
How can I detect if a key was pressed in a game (league of legends) using C#?如何使用 C# 检测是否在游戏(英雄联盟)中按下了键?
I used to do that with AHK with ease but I want to move to c# since I m studying it these days.我过去很容易用 AHK 做到这一点,但我想搬到 c#,因为这些天我正在学习它。
Do you guys have any suggestions?你们有什么建议吗?
My friend implemented a thread level mouse/keyboard hook.我的朋友实现了一个线程级鼠标/键盘挂钩。
Just install the Nuget package and give me some feedback.只需安装 Nuget package 并给我一些反馈。
Install-Package -IncludePrerelease Winook
Example:例子:
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}");
}
I would suggest you should always search in the documentations and read them carefully.我建议您始终在文档中搜索并仔细阅读。
Bellow is an example of how to do it based on Microsoft documentation 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;
}
Don't forget to import 'System.Windows.Input' namespace.不要忘记导入“System.Windows.Input”命名空间。
Follow the link 1 and Link 2 for more info.按照链接 1和链接 2了解更多信息。
Edit: This question is answered in this link: Trying to detect keypress编辑:这个问题在这个链接中得到了回答: 试图检测按键
take a look at SetWindowsHookExA it will let you hook to mouse and kb events and look into pinvoke.net to learn how to do it.看看SetWindowsHookExA它将让您挂钩鼠标和 kb 事件并查看pinvoke.net以了解如何操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.