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
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.
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.
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.
You could use KeyBindings
instead, they define full gestures without such a separation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With