6

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?

Cœur
  • 34,719
  • 24
  • 185
  • 251
Bador
  • 263
  • 2
  • 5
  • 13
  • This question may be of some help http://stackoverflow.com/questions/400113/best-way-to-implement-keyboard-shortcuts-in-a-windows-forms-application – unlimit Jun 14 '13 at 12:38

2 Answers2

9

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
    }
}
naed21
  • 149
  • 4
  • 9
HostMAX
  • 177
  • 1
  • 5
3
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)) {
      ...
    }
Dmitry Bychenko
  • 165,109
  • 17
  • 150
  • 199