3

Mostly every program that uses hotkeys allows the user to see which keys are actually being pressed, but I'm not sure how it is accomplished. For reference, see the following screenshot of a random program that captures what the button combination is:

enter image description here

How can I implement something like this? The event I'm reading from is of Type System.Windows.Forms.KeyEventArgs. There are many keys which have no description of its actual value and several others which have inaccurate values. Is there no solution outside of finding out the code for each key and displaying them manually?

Edit: For clarification, I want to get a user friendly string representation of the keys that were pressed as shown in the above screenshot.

Lunyx
  • 3,164
  • 6
  • 30
  • 46

4 Answers4

1

The event has bool properties for Alt, Control and Shift.

KeyCode's type is System.Windows.Forms.Keys. It has a [Flags] attribute, but that only counts for Alt, Control and Shift. All other keys have a regular value.

private void showKeys(object sender, KeyEventArgs e)
{
    List<string> keys = new List<string>();

    if (e.Control == true)
    {
        keys.Add("CTRL");
    }

    if (e.Alt == true)
    {
        keys.Add("ALT");
    }

    if (e.Shift == true)
    {
        keys.Add("SHIFT");
    }

    switch (e.KeyCode)
    {
        case Keys.ControlKey:
        case Keys.Alt:
        case Keys.ShiftKey:
        case Keys.Menu:
            break;
        default:
            keys.Add(e.KeyCode.ToString()
                .Replace("Oem", string.Empty)
                );
            break;
    }

    e.Handled = true;

    textBox1.Text = string.Join(" + ", keys);
}
user247702
  • 23,641
  • 15
  • 110
  • 157
  • The issue is that I want to print a user friendly string that indicates which keys were pressed. Have you taken a look at the example in the question? I don't need to enumerate through multiple keys since I will only be allowing modifier(s) and one other key. – Lunyx Feb 13 '13 at 16:00
  • Well, you have all required data available. You still need to do some of the work. I've edited the answer to include sample code. In the case of `KeyDown` and `KeyUp` the `KeyCode` property will only hold one value, so depending on the way you handle the event it could need some modification. – user247702 Feb 13 '13 at 18:29
  • There may be some edge cases you need to handle, for example the *Oem...* keys. Go over the `Keys` enum to see if there are any additional cases you wish to handle. – user247702 Feb 13 '13 at 18:37
  • Something seems off. This is what I'm getting when I press "5" (NOT numpad) http://i.imgur.com/qm2729X.png I've tried pressing regular characters, but none of them actually translate what the key I pressed is in any of the fields, which is why I asked this question. – Lunyx Feb 13 '13 at 21:59
  • I had a look at the Enum and it turns out this is a bit of a weird one. http://stackoverflow.com/questions/10162110/system-windows-forms-keys-hasflag-behaving-weirdly explains it. Summary: Alt, Control and Shift behave as flag values, all other keys should be seen as single values. `D5 = 53` or `110101` is the expected result, however, due to the `[Flags]` attribute, you see `LButton = 1, MButton = 4, ShiftKey = 16, Space = 32` or `110101`. http://i.imgur.com/x0c3bO5.png – user247702 Feb 13 '13 at 23:02
  • Thanks for finding an explanation on that. Let me look into the other keys when I have some time and I'll get back to you. Have an upvote for now. – Lunyx Feb 14 '13 at 04:08
  • Ok, so I tried actually printing the value and it shows up properly. I was a bit thrown off by the values shown by the debugger. I'll accept your answer since it explained why I wasn't seeing the actual representation. – Lunyx Feb 14 '13 at 17:40
0

KeyEventArgs.KeyCode Property....

if u can use javascript u will have a code for each key

Keycodes in javascript

and i found this quite interesting for Keyeventargs.Keycode http://www.asquare.net/javascript/tests/KeyCode.html

Community
  • 1
  • 1
0

You can get witch key pressed by this code(for single key or multiple key pressed,set form KeyPreview to true )

    private void Form1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
    {
        label1.Text = null;
        label1.Text=e.Modifiers.ToString();
    }
KF2
  • 9,887
  • 8
  • 44
  • 77
0

For a win-form, you need firstly to set property KeyPreview to true. That way, the pressed key should be detected, even if one of the form's childs has the focus and form itself doesn't have it.

Then,override one of the following methods: OnKeyUp, OnKeyDown, and OnKeyPress.

protected override void OnKeyUp(KeyEventArgs e)
{
  switch (e.KeyCode)
  {
      case Keys.F1:
         showHelpTopics();
         break;
  }
}

If you suffer no description of some keys, just perform a msdn search and you will find its details.

  • I am globally detecting keystrokes so I don't think the KeyPreview property is relevant. Perhaps I should've mentioned that in the question. In any case, I want to be able to handle all of the keys possible, so it would be inefficient to do something like a switch statement with all the possible scenarios. – Lunyx Feb 13 '13 at 15:51