Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.net difference between right shift and left shift keys

Tags:

c#

.net

keyboard

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?

like image 429
Mr AH Avatar asked Apr 02 '10 21:04

Mr AH


People also ask

Is there a difference between left and right Shift?

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".

What does the right Shift key look like?

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.

Why is there a right Shift key?

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.

What are the types of key Shift?

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.


3 Answers

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");
        }
    }
}
like image 83
JYelton Avatar answered Sep 29 '22 06:09

JYelton


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));
}
like image 42
adrianbanks Avatar answered Sep 29 '22 06:09

adrianbanks


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....
}
like image 35
Mr AH Avatar answered Sep 29 '22 05:09

Mr AH