2

I have looked for a solution to detect if multiple keys are pressed in C# (I couldn't find a solution!). 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.

--EDIT--

How to detect if a key is pressed using KeyPressed (C#)

Zak The Hat
  • 317
  • 1
  • 5
  • 15
  • 1
    Probably this helps: http://stackoverflow.com/a/709641/3195195 – jeffry copps Jun 17 '16 at 12:03
  • @jeffrycopps Sorry but that didn't help, the answers only tell you how to detect if all the selected keys are pressed, not if any key is pressed. – Zak The Hat Jun 17 '16 at 12:05
  • 1
    I didn't get your question well. Keys pressed and held down are same I guess. – jeffry copps Jun 17 '16 at 12:07
  • 1
    Crystal ball says that the real problem is that key repeat no longer works. Something you should *never* rely on in a game since the repeat rate and cadence is a user preference. Nothing that two *bool* variables cannot do. Set them to *true* with the KeyDown event and back to false with the KeyUp event. Use those bools in your game loop to determine where the game objects should move. It completely does not matter that more than one player presses keys at the same time, you get the events one after another. – Hans Passant Jun 17 '16 at 12:13
  • @jeffrycopps I don't have a skill at making myself clear, so I'll try to make it better this time.The link you gave me, the answer is coded to check if all the keys are pressed at the **the same time**, I need to check if multiple keys are pressed e.g. UP, DOWN, LEFT, RIGHT, W, A, S, D. I **don't** want it to check if they're pressed **all** at the same time. – Zak The Hat Jun 17 '16 at 12:13
  • @HansPassant Could you please explain yourself more? – Zak The Hat Jun 17 '16 at 12:15
  • Google "windows forms game loop" to get somewhere. – Hans Passant Jun 17 '16 at 12:17
  • @HansPassant It didn't help. – Zak The Hat Jun 17 '16 at 12:19
  • Are you meaning to say, you want a common action listener which will be able to handle any key press event? – jeffry copps Jun 17 '16 at 12:24
  • It would help if you stated what data you want when keys are pressed in various sequences. For example, what information do you need for the following key press release sequence (D suffix means key down, U suffix means key up): `WD, WU, WD, AD, WU, AU, AD, WD, SD, WU, DD, WD, WU, AU, SU, DU` – Matthew Watson Jun 17 '16 at 13:21

4 Answers4

6

The source of the problems is that this simple request is simply not supported under 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..

((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..))

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.

With it code like this works:

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; }
...

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:

First: Add the namespace:

using System.Windows.Input;

Then: Add two references to the project:

PresentationCore
WindowsBase

enter image description here

Now you can test the keyboard at any time, including the KeyPress event..

TaW
  • 53,122
  • 8
  • 69
  • 111
0

Maybe for printing to the console...

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

The easiest way to do this is:

  1. Create a KeyDown event for the form

  2. Write down the DllImports below in the class:

    [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:

            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; in the Form_Load event and boom, there you go.

This is currently the best way to handle multiple Key events as it's a simple and straight up way for WinForms.

Pronner
  • 25
  • 1
  • 9
-1

You can achieve it using program level keyboard hook. You can use Win-API functions like SetWindowsHookEx in user32.dll.

Here are some examples of it :

A Simple C# Global Keyboard Hook
Global keyboard capture in C# application

Community
  • 1
  • 1
Er Mayank
  • 1,017
  • 2
  • 16
  • 39