How can I see if LeftShift or RightShift was pressed?
-
I'm developing a C# application where I need to find out if the left shift key has been pressed down or the right shift key. I've tried to use the KeyDown event with the following code: private void KeyDown(object sender, System.Windows.Forms.KeyEventArgs e) { if(e.Control && (e.KeyCode == Keys.D1) && (e.KeyCode == Keys.LShiftKey)) { //Do stuff here } } I can read the keypresses but I can not see if the left shift key is pressed, only that a shift key was pressed. The e.KeyCode == Keys.LShiftKey doesn't work since it seems that KeyCode don't collect info about which shift that was pressed. Any help appreciated! Thaks! Andrew
-
I'm developing a C# application where I need to find out if the left shift key has been pressed down or the right shift key. I've tried to use the KeyDown event with the following code: private void KeyDown(object sender, System.Windows.Forms.KeyEventArgs e) { if(e.Control && (e.KeyCode == Keys.D1) && (e.KeyCode == Keys.LShiftKey)) { //Do stuff here } } I can read the keypresses but I can not see if the left shift key is pressed, only that a shift key was pressed. The e.KeyCode == Keys.LShiftKey doesn't work since it seems that KeyCode don't collect info about which shift that was pressed. Any help appreciated! Thaks! Andrew
Declare the function somewhere inside your class:
[DllImport("user32")] public static extern short GetKeyState (Keys VirtKey);
On your KeyDown event, you can check which shift key is pressed in this way:bool lshiftpressed = ((GetKeyState(Keys.LShiftKey) & 256)==256); bool rshiftpressed = ((GetKeyState(Keys.RShiftKey) & 256)==256);
DevIntelligence.com - My blog for .Net Developers -
Declare the function somewhere inside your class:
[DllImport("user32")] public static extern short GetKeyState (Keys VirtKey);
On your KeyDown event, you can check which shift key is pressed in this way:bool lshiftpressed = ((GetKeyState(Keys.LShiftKey) & 256)==256); bool rshiftpressed = ((GetKeyState(Keys.RShiftKey) & 256)==256);
DevIntelligence.com - My blog for .Net Developers