简体   繁体   English

如何检测 C# 表单中是否按下了多个键

[英]How to detect if multiple keys are pressed in C# forms

I have looked for a solution to detect if multiple keys are pressed in C# (I couldn't find a solution!).我一直在寻找一种解决方案来检测是否在C#中按下了多个键(我找不到解决方案!)。 I have this game with two players and I need to detect if a key is pressed to move them around.我有这个游戏有两个玩家,我需要检测是否按下键来移动他们。 (I'm Using C# Forms) I've only found answers to detect if two keys are held down at the same time and they didn't help. (我正在使用 C# 表单)我只找到了检测是否同时按住两个键并且它们没有帮助的答案。

--EDIT-- - 编辑 -

How to detect if a key is pressed using KeyPressed (C#)如何使用 KeyPressed (C#) 检测是否按下了某个键

The source of the problems is that this simple request is simply not supported under Winforms.问题的根源在于 Winforms 根本不支持这个简单的请求。

Sounds weird?听起来怪怪的? It is.这是。 Winforms is often painfully close to the hardware layers and while this may be quite interesting at times, it also may be just a pita.. Winforms 通常非常接近硬件层,虽然有时这可能很有趣,但它也可能只是一个皮塔......

((One hidden hint by Hans should be noted: In the KeyUp you get told which key was released so you can keep track of as many keys as you want..)) ((应该注意 Hans 的一个隐藏提示:在KeyUp中,您会被告知释放了哪个键,以便您可以跟踪任意数量的键..))

But now for a direct solution that desn't require you to keep a bunch of bools around and manage them in the KeyDown and KeyUp events..: Enter Keyboard.IsKeyDown .但是现在对于不需要您保留一堆KeyDown bools KeyUp KeyUp中管理它们的直接解决方案..:输入Keyboard.IsKeyDown

With it code like this works:有了它,这样的代码可以工作:

Uses the Keyboard.IsKeyDown to determine if a key is down.使用Keyboard.IsKeyDown来确定某个键是否按下。 e is an instance of KeyEventArgs . eKeyEventArgs的一个实例。

 if (Keyboard.IsKeyDown(Key.Return)) { btnIsDown.Background = Brushes.Red; } ...

Of course you can ceck for multiple key as well:当然,您也可以检查多个键:

if (Keyboard.IsKeyDown(Key.A) && Keyboard.IsKeyDown(Key.W)) ...

The only requirement is that you borrow this from WPF :唯一的要求是你从WPF借用这个:

First: Add the namespace:首先:添加命名空间:

using System.Windows.Input;

Then: Add two references to the project:然后:在项目中添加两个引用:

PresentationCore
WindowsBase

在此处输入图像描述

Now you can test the keyboard at any time, including the KeyPress event..现在您可以随时测试键盘,包括KeyPress事件。

也许用于打印到控制台...

if (Input.GetKeyDown(KeyCode.G))     {print ("G key pressed");}

The easiest way to do this is:最简单的方法是:

  1. Create a KeyDown event for the form为表单创建 KeyDown 事件

  2. Write down the DllImports below in the class:在类中写下下面的 DllImports:

     [DllImport("user32")] public static extern int GetAsyncKeyState(int vKey); [DllImport("user32.dll")] public static extern int GetKeyboardState(byte[] keystate);
  3. Write down the code below in the KeyDown Event:在 KeyDown 事件中写下以下代码:

     byte[] keys = new byte[256]; GetKeyboardState(keys); if ((keys[(int)Keys.ControlKey] & keys[(int)Keys.S] & 128) == 128) // change the keys if you want to { // it worked }
  4. Paste the code this.KeyPreview = true;粘贴代码this.KeyPreview = true; in the Form_Load event and boom, there you go.在 Form_Load 事件和繁荣中,你去。

This is currently the best way to handle multiple Key events as it's a simple and straight up way for WinForms.这是目前处理多个 Key 事件的最佳方式,因为它是 WinForms 的一种简单直接的方式。

You can achieve it using program level keyboard hook.您可以使用程序级键盘挂钩来实现它。 You can use Win-API functions like SetWindowsHookEx in user32.dll.您可以在 user32.dll 中使用 Win-API 函数,例如SetWindowsHookEx

Here are some examples of it :以下是它的一些例子:

A Simple C# Global Keyboard Hook 一个简单的 C# 全局键盘钩子
Global keyboard capture in C# application C# 应用程序中的全局键盘捕获

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM