Wednesday, July 20, 2005

keyboard shortcut assignment in textbox

keyboard shortcut assignment in textbox
All 2 messages in topic - view as tree


I'm building a form to allow the user to assign a keyboard shortcut.
Basically I have a textbox that I want to work like the "Press shortcut
key(s)" in the Visual Studio keyboard options, the user does the key
combination and it shows up in the textbox. It looks like it works on the
KeyDown event. The KeyDown event has an argument of KeyEventArgs. The docs
say "You can use constants from Keys to extract information from the KeyData
property. Use the bitwise AND operator to compare data returned by KeyData
with constants in Keys to obtain information about which keys the user
pressed. To determine whether a specific modifier key was pressed, use the
Control, Shift, and Alt properties." Unfortunately there is no example and
I don't know how to do bitwise compares in c#. Can someone help me out
here?

thanks
Paul




first, set Form1.KeyPreview value to "true"

then, on Form's KeyDown Event

private void Form1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs
e)
{
//Note: "T" is a sample designated shortcut key for textbox
if (e.Modifiers.ToString().ToUpper() == "ALT" &
e.KeyCode.ToString().ToUpper() == "T")
this.textBox1.Select();

}

gani

No comments: