Ryan
Ryan

Reputation: 135

How to fire both Shortcut Keys and KeyDown event? (C# Windows Form)

I have a TextBox's KeyDown event that start a timer when it is called. I also have a Paste MenuStripItem and I set the ShorcutKeys property in Design View to Ctrl + V.

I notice that when user press the shortcut keys (Ctrl + V), the KeyDown event will not be called. And when I set the ShortcutKeys property to None, KeyDown is normally called. Is there a way to fire both event when I press the Ctrl + V?

I has tried with other MenuStripItem and the result are the same.

EDIT: Thank you very much, @Hans. When I move my code from KeyDown event to ProcessCmdKey, it works really well. (So I suppose the ProcessCmdKey is the method which is called everytime a key is pressed nomatter what it is). Because you deleted your previous anwser so I can't mark it as answer now.

Upvotes: 0

Views: 652

Answers (1)

John Smith
John Smith

Reputation: 495

private void somethind_KeyDown(object sender, KeyEventArgs e)
{
    if (KeyCodes.Contains(e.KeyValue) || (e.KeyCode==Keys.V && e.Control))
        e.SuppressKeyPress = false;
    else 
        e.SuppressKeyPress=true;
}

See this list of keycodes: http://www.cambiaresearch.com/articles/15/javascript-char-codes-key-codes

I hope this helps!

Upvotes: 0

Related Questions