Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# KeyDown Event multiple Keys plus ControlKey

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?

like image 700
Bador Avatar asked Jun 14 '13 12:06

Bador


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.


2 Answers

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
    }
}
like image 130
HostMAX Avatar answered Sep 18 '22 02:09

HostMAX


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)) {
      ...
    }
like image 28
Dmitry Bychenko Avatar answered Sep 18 '22 02:09

Dmitry Bychenko