ZeroEffect Posted January 30, 2007 Posted January 30, 2007 I was able to find this thread to help me out awhile ago. http://www.xtremedotnettalk.com/showthread.php?t=69918&highlight=modifierkeys Ok So I can do this with all my projects except a MDI project where I want the Main MDI container to respond to the keyboard strokes. Baically I want to use Shift & Blah & blah to make some "hidden" functions visible. Just thising the normal user doesn't need to mess with. Is this possible? Or would I have to add the code to all the forms in the MDI container to do this. Thanks ZeroEffect Quote If you can't find it, Build It. There is no place Like 127.0.0.1 also don't forget 1 + 1 = 10
*Experts* Nerseus Posted January 30, 2007 *Experts* Posted January 30, 2007 You can set the MDI form's KeyPreview property to true, then handle the KeyDown event. For example: private void MDIForm_KeyDown(object sender, KeyEventArgs e) { string s = string.Empty; if (e.Shift) s+= " SHIFT "; if (e.Control) s += " CONTROL "; if (e.Alt) s += " ALT "; s += e.KeyCode; Debug.WriteLine(s); } -ner Quote "I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
ZeroEffect Posted January 30, 2007 Author Posted January 30, 2007 (edited) Thanks for the reply. I didn't know about the Key Preview property. This is how I am detecting multipule key presses but it doesn't seem to work and stepping through is a bit difficult. If (ModifierKeys = Keys.Shift) And (ModifierKeys = Keys.R) And (ModifierKeys = Keys.C) Then MenuItem22.Visible = True End If Thanks again for the help and example. ZeroEffect Edited January 30, 2007 by ZeroEffect Quote If you can't find it, Build It. There is no place Like 127.0.0.1 also don't forget 1 + 1 = 10
MrPaul Posted January 30, 2007 Posted January 30, 2007 Key combinations Your conditional statement will never succeed since it is impossible for a variable to take 3 values at once. If it is a combination of values, then you have to test for specific parts using bitmasking (the And operator). The ModifierKeys member of the KeyEventArgs only represents combinations of Ctrl, Shift, and Alt. The KeyCode member specifies the keycode (eg R or C). Note that this can only ever contain one key - it is impossible to represent R and C simultaneously. This means that to check for a combination such as Shift+R+C, you would need to either query the keyboard state or use a state variable, since the combination would likely cause two events to fire - Shift+R and Shift+C. To test for Shift+C, you might do: If (e.Shift) And (e.KeyCode = Keys.C) Then 'Shift+C End If To capture Shift+R+C, you would need to capture Shift+R and Shift+C independently and check that they have both occured before continuing. Done properly this will require use of the KeyDown and the KeyUp events. However, combinations using only one character key and any combination of Ctrl, Shift, and Alt can be captured much more easily, using the above method. Good luck :cool: Quote Never trouble another for what you can do for yourself.
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.