6
votes

i want to recognize keystrokes to my Control. For this i use the KeyDown Event. The Kind of keystrokes I want to detect are something like CTRL + A or CTRL + C and so on. (So combinations of multiple keys)

Now I have revied the KeyEventArgs and found the Keys enum. (Everything works perfect just use | and & to Combine and find the correct keys) An Example could be Shift + A then the Value of the KeyData Enum is: ShiftKey | Shift | A

BUT

When i try it with the Control Key pressed (so Control + A) i got 131137 as Response? And I do not know exactly why I do not get something like ControlKey | Control | A (or something like this)

I have recogniced if I try it with A ist 131137 with B ist 131138 with C ist 131139 and so on ... So I think it is possible to calculate the key but I think there should be a better solution then just so something like this?

131137 - 131072 = 65 (for A)

Am I right, or is this the prevered solution, or do I misunderstand some Basic?

2
This question may be of some help stackoverflow.com/questions/400113/…unlimit

2 Answers

8
votes

You can get Ctrl, Shift etc... using properties in KeyEventArgs object

http://msdn.microsoft.com/en-us/library/system.windows.forms.keyeventargs_properties(v=vs.90).aspx

void Control_KeyDown(object sender, KeyEventArgs e)
{
    if (e.Control && e.KeyCode == Keys.F4)
    {
        // Be happy
    }
}
3
votes
131072 == (int) Keys.Control 

so

131137 (100000000001000001 binary) == (int) (Keys.Control | Keys.A)

and you can put something like that

  private void myControl_KeyDown(object sender, KeyEventArgs e) {
    if (e.KeyData == (Keys.A | Keys.Control)) {
      ...
    }