This is a mirror of official site: http://jasper-net.blogspot.com/

#627 – Detecting Whether The Ctrl Key Is Pressed In a KeyDown Event Handler

| Wednesday, August 22, 2012
When you press a Ctrl-key combination (e.g. Ctrl+G) and you are monitoring KeyDown (or KeyUp) events, you’ll see two separate KeyDown events–one for the Ctrl key and one for the key pressed with it.
If you want to respond to a Ctrl-key combination within a KeyDown event handler, you can do the following :
Use the KeyEventArgs.Key property to see whether the event is being fired because the combination key (e.g. ‘G’) is being pressed
Use the Keyboard.IsKeyDown method to check whether the Ctrl key is also currently down

private void TextBox_KeyDown(object sender, KeyEventArgs e)
{
    if ((e.Key == Key.G) &&
        (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl)))
             MessageBox.Show("You pressed Ctrl+G !");
}

QR: Inline image 1

Posted via email from Jasper-net

0 comments: