Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get all keys that are pressed

What do I have to use to be able to get "all keys" that are pressed on the keyboard at the moment? Since Form.KeyPress += new EventHandler() doesn't do much at all when it's filled of controls. It doesn't call it no matter what I do, neither KeyDown, KeyUp or anything else... and yeah, I know how to use them.

So, if there's any function in the system that can check for pressed keys, that returns an array of the keys used or something - I would be grateful to be pointed in the right direction.

like image 457
Deukalion Avatar asked May 07 '12 14:05

Deukalion


People also ask

How can I see what keys I'm pressing?

The Windows on-screen keyboard is a program included in Windows that shows an on-screen keyboard to test modifier keys and other special keys. For example, when pressing the Alt , Ctrl , or Shift key, the On-Screen Keyboard highlights the keys as pressed.

How do you show what keys are being pressed on-screen Windows 10?

Go to Start , then select Settings > Accessibility > Keyboard, and turn on the On-Screen Keyboard toggle. A keyboard that can be used to move around the screen and enter text will appear on the screen. The keyboard will remain on the screen until you close it.

How do I get a keypress event?

The keypress event is fired when a key that produces a character value is pressed down. Examples of keys that produce a character value are alphabetic, numeric, and punctuation keys. Examples of keys that don't produce a character value are modifier keys such as Alt , Shift , Ctrl , or Meta .

How do you see what key is being pressed Javascript?

Attach an event to the input box. like onkeypress event. Call a function when that event happens and pass the event parameter in it. In the called function, identify the key pressed.


2 Answers

It sounds like you want to query the state of all keys in the keyboard. The best function for that is the Win32 API call GetKeyboardState

  • GetKeyboardState

I don't believe there is a good managed equivalent of that function. The PInvoke code for it is fairly straight forward

[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool GetKeyboardState(byte [] lpKeyState);

var array = new byte[256];
GetKeyboardState(array);

This will populate the byte[] with the up / down state of every virtual key in the system. If the high bit is set then the virtual key is currently pressed. Mapping a Key to a virtual key code is done by only considering the byte portion of the numeric Key value.

public static byte GetVirtualKeyCode(Key key) {
  int value = (int)key;
  return (byte)(value & 0xFF);
}

The above works for most of the Key values but you need to be careful with modifier keys. The values Keys.Alt, Keys.Control and Keys.Shift won't work here because they're technically modifiers and not key values. To use modifiers you need to use the actual associated key values Keys.ControlKey, Keys.LShiftKey, etc ... (really anything that ends with Key)

So checking if a particular key is set can be done as follows

var code = GetVirtualKeyCode(Key.A);
if ((array[code] & 0x80) != 0) {
  // It's pressed
} else { 
  // It's not pressed
}
like image 61
JaredPar Avatar answered Sep 29 '22 12:09

JaredPar


I've expanded on JaredPar's answer a little bit. The following method returns the collection of all keys that are currently being pressed:

using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Windows.Input;

private static readonly byte[] DistinctVirtualKeys = Enumerable
    .Range(0, 256)
    .Select(KeyInterop.KeyFromVirtualKey)
    .Where(item => item != Key.None)
    .Distinct()
    .Select(item => (byte)KeyInterop.VirtualKeyFromKey(item))
    .ToArray();

/// <summary>
/// Gets all keys that are currently in the down state.
/// </summary>
/// <returns>
/// A collection of all keys that are currently in the down state.
/// </returns>
public IEnumerable<Key> GetDownKeys()
{
    var keyboardState = new byte[256];
    GetKeyboardState(keyboardState);

    var downKeys = new List<Key>();
    for (var index = 0; index < DistinctVirtualKeys.Length; index++)
    {
        var virtualKey = DistinctVirtualKeys[index];
        if ((keyboardState[virtualKey] & 0x80) != 0)
        {
            downKeys.Add(KeyInterop.KeyFromVirtualKey(virtualKey));
        }
    }

    return downKeys;
}

[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool GetKeyboardState(byte[] keyState);
like image 32
satnhak Avatar answered Sep 29 '22 12:09

satnhak