Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detecting key combinations

I want to detect when a combination like Ctrl-C is pressed in a WPF application. What I've read online says to use something like the following in the KeyDown (or KeyUp) Event:

if ((Keyboard.Modifiers == ModifierKeys.Control) && (e.Key == Key.S))
{
    MessageBox.Show("Save!");
}

I'm just trying to understand how this works. As I understand it, e.Key contains the key that was pressed that triggered the event and Keyboard.Modifiers contains the information about the state of the Control key right now. Is it safe to assume that the Control key will still be down by the time the KeyDown event gets handled?

For example, I restart Firefox and it grinds away loading a bunch of tabs, and in the meantime I hit Ctrl-S in my application. There is a delay in getting to KeyDown, and the application thinks just S has been pressed.

Thanks

like image 533
user584974 Avatar asked Apr 30 '12 17:04

user584974


People also ask

What is a keystroke combination?

A key combination is the use of two or more keys on a keyboard to generate a specific result. These keys are pressed at the same time, or one after the other while holding down each of the previous keys.

How many combination key are there in the keyboard?

There's no single standard dictating the number of keys, buttons, or characters on a keyboard; most companies use the PC keyboard with a total of 104 alphanumeric keys as a de facto standard. There have been many different manufacturers over the years, so the number of keys varies from model to model.

Which two keys work in combination with keys?

Control keys. These keys are used alone or in combination with other keys to perform certain actions. The most frequently used control keys are Ctrl, Alt, the Windows logo key , and Esc.


1 Answers

You could use KeyBindings instead, they define full gestures without such a separation.

like image 138
H.B. Avatar answered Oct 12 '22 06:10

H.B.