I am currently working on an application which requires different behaviour based on whether the user presses the right or left shift key (RShiftKey, LShiftKey), however when either of these keys is pressed I only see ShiftKey | Shift.
Is there something wrong with my keyboard? (laptop) do I need a new keyboard driver/keyboard in order to send the different key commands maybe...
This is a pretty massive problem at the moment, as there is no way of testing that the code works (apart from unit tests). Anyone had any experience of the different shift/alt/ctrl keys?
The left and right Shift on a computer keyboard perform the same function. When pressed and held down, it changes the case of the letter to uppercase, or use the alternate character on any other key. For example, pressing and holding down Shift while pressing the letter "a" makes a capital "A".
The 'shift' keys are on the left and right of the keyboard, with the arrow pointing upwards. For capital letters, hold down the 'shift' key and hold and type the letter. For symbols at the top of a number key, press down the symbol key and then type the symbol.
The Shift key on the right is a little wider on most keyboards. Why are there two Shift keys? People who type fluently with all ten fingers use the right Shift key for letters typed with the left hand and the left Shift key for letters typed with the right hand.
The Shift key ⇧ Shift is a modifier key on a keyboard, used to type capital letters and other alternate "upper" characters. There are typically two shift keys, on the left and right sides of the row below the home row.
I don't know if this post will help you or not, but it looks like you may have to mess with InteropServices and Diagnostics:
MSDN Forum: How to send Left/Right shift key
Edit: I finally figured out how to make GetAsyncKeyState() work, as adrianbanks revealed.
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern short GetAsyncKeyState(Keys vKey);
private void theForm_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.ShiftKey)
{
if (Convert.ToBoolean(GetAsyncKeyState(Keys.LShiftKey)))
{
Console.WriteLine("Left");
}
if (Convert.ToBoolean(GetAsyncKeyState(Keys.RShiftKey)))
{
Console.WriteLine("Right");
}
}
}
Take a look at the GetAsyncKeyState Win32 method. You can add a pInvoke call to it using:
[DllImport("user32.dll")]
private static extern short GetAsyncKeyState(Keys key);
and then handle the KeyDown
event on your form:
private void MyForm_KeyDown(object sender, KeyEventArgs e)
{
Console.WriteLine("Left Shift : " + (GetAsyncKeyState(Keys.LShiftKey) < 0));
Console.WriteLine("Right Shift: " + (GetAsyncKeyState(Keys.RShiftKey) < 0));
}
Thanks guys, good solution there. In the mean time here's my own "hacky" way of doing it from the override of ProcessCmdKey:
public override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (msg.LParam.ToInt32() == 0x2a0001)
LastShiftKey = ShiftKeys.NumericShift;
else if (msg.LParam.ToInt32() == 0x360001)
LastShiftKey = ShiftKeys.AlphaShift;
etc....
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With