【问题标题】:how to detect key pressed in game with C#如何使用C#检测游戏中按下的键
【发布时间】:2020-05-13 04:01:35
【问题描述】:

如何使用 C# 检测游戏(英雄联盟)中是否按下了某个键?

我以前很容易用 AHK 做到这一点,但我想转向 c#,因为我最近正在学习它。

大家有什么建议吗?

【问题讨论】:

    标签: c#


    【解决方案1】:

    我的朋友实现了线程级鼠标/键盘挂钩。

    只需安装 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}");
    }
    

    【讨论】:

      【解决方案2】:

      我建议您始终在文档中搜索并仔细阅读。

      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”命名空间。

      关注link 1Link 2 了解更多信息。

      编辑: 此问题在此链接中得到解答:Trying to detect keypress

      【讨论】:

      • 哦,谢谢你的回答,但只有当我在表单上时才有效,但如果我在玩游戏呢?
      • 我发布的代码 sn-p 与您在哪里使用它无关。它不是为了形式。可以在任何地方运行此代码,它应该可以工作。
      【解决方案3】:

      看看SetWindowsHookExA,它会让你挂钩鼠标和kb事件并查看pinvoke.net以了解如何做。

      【讨论】:

      • 任何要遵循的教程因为我想我迷路了 ngl
      猜你喜欢
      • 2011-04-11
      • 1970-01-01
      • 2011-02-26
      • 1970-01-01
      • 2023-04-09
      • 1970-01-01
      • 1970-01-01
      • 2014-11-14
      • 2016-11-05
      相关资源
      最近更新 更多