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

c# check if key is pressed
c key press ctrl f
how to check enter key pressed in c#
c# form key press
c# keyboard listener
how to press keyboard keys using c#
c# form key down
key event handler c

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#)

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

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

How to handle multiple key press in c#.net, private void Form1_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode Keys.Alt) { //Show the form } } if (event.keyCode == 17) // 17 keycode is for Left control key { document. check for keyboardhook exaples. you have to remember the pressed keys (ie in a bool array). and set the position to 1 when its pressed (keydown) and 0 when up . this way you can track more than one key. I suggest doing an array for special keys only. so you can do:

Maybe for printing to the console...

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

C# Detect Multiple keypress - MSDN, Windows.Forms.FormClosingEventHandler(Form1_FormClosing); tm. The common keys that could be detected with multiple keys pressed in� Detecting when the user is pressing the shift key. You should consider to use the KeyEvents of your form, especially the KeyDown and KeyUp events. Important: to use the event properly, you must set the KeyPreview attribute to true first! Then use following code within the KeyDown event: shiftPressed = e.Shift; and in the KeyUp event:

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

How to: Determine Which Modifier Key Was Pressed, When you create an application that accepts the user's keystrokes, you may also want to monitor for modifier keys such as the SHIFT, ALT, and� Use the bitwise AND operator with the ModifierKeys property and a value of the Keys enumeration to determine whether a particular modifier key is pressed. The following code example shows how to determine whether the SHIFT key is pressed within a KeyPress event handler.

Control.KeyDown Event (System.Windows.Forms), Occurs when a key is pressed while the control has focus. The following code example uses the KeyDown event to determine the type of character entered� If you want to check whether Shift alone is pressed without any other modifiers, use if (Control.ModifierKeys == Keys.Shift) Note that even this will be trueif another non-modifier is down (Eg, Shift+A). If you want to check whether Shift and onlyShift is pressed, you'll have to use an API call.

How to: Handle Keyboard Input at the Form Level, Learn how to handle keyboard input for your Windows Forms at the form level, handles the KeyPress event by detecting all of the number keys and Compare the MessageBox output when you press a number key while� How to detect when the Enter Key Pressed in C#. The following C# code behind creates the KeyDown event handler. If the key that is pressed is the Enter key, a MessegeBox will displayed . if (e.KeyCode == Keys.Enter){MessageBox.Show("Enter Key Pressed ");}

keyPress event in C# , KeyDown event in C# , KeyUp event in C#, C# Key Press - Most Windows Forms applications process keyboard input exclusively by handling the How to detect when the Enter Key Pressed in C#. Handle the KeyPress or KeyDown event of the startup form, and set the KeyPreview property of the form to true so that keyboard messages are received by the form before they reach any controls on the form. The following code example handles the KeyPress event by detecting all of the number keys and consuming '1', '4', and '7'.

Comments
  • Probably this helps: stackoverflow.com/a/709641/3195195
  • @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.
  • I didn't get your question well. Keys pressed and held down are same I guess.
  • 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.
  • @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.
  • Sorry but it didn't help, can you give an example?