LShiftKey doesn't work

Hello,

Does anybody know why the following code doesn't work

Keys.LShiftKey exsists but you cannot use it, isn't that strange

Regards

Arjen

private void Form1_KeyDown(object sender, KeyEventArgs e)

{

if (e.KeyCode == Keys.LShiftKey)

{

MessageBox.Show("Left Shift pressed");

}

}



Answer this question

LShiftKey doesn't work

  • Saviourmachine

    You're trying to get a KeyDown for your main Form. I don't think this will work because the current control that has focus is not your Form1, but another control.

    This will work if your Form1 has explicit focus and no other controls are present on the form.

    Try assigning KeyDown to other controls on your form that have focus or use a Win32 API call for GetKeys ( I'm not sure exactly what this is called) to get the key strokes from the keyboard.



  • Andrey Basko

    In this situation it's not a matter that the key is or is not being pressed its that the KeyCode/KeyData/KeyValue/Modifiers are the same for both left and right shift. You may need to enable KeyPreview on your form to always get it. But that has nothing to do with your stated problem. THe fact is that
    if (e.KeyCode==Keys.LShiftKey) will never evaluate to true
    while this wil. if (e.KeyCode==Keys.ShiftKey)

    Hope this helps.


  • Jossef Goldberg

    Try to look at this thread, I have a working VB example of using GetKeyState to determine which shift key is pressed:
    http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=204734&SiteID=1

    Regards,

    -chris

  • Arnoldio

    Thanks for the tip

    Although you have to invoke some unsafe code, which I think is a pitty

    Arjen


  • fafan_iran

    If your really need to know the state of an individual key then use the Win32 GetKeyState call using platform invoke. Pass in VK_LSHIFT which is 0xA0.

    [DllImport("User32.dll", CharSet=CharSet.Auto)]
    public static extern ushort GetKeyState(int virtKey);

    Phil Wright
    http://www.componentfactory.com
    Free user interface controls for .NET2


  • suamikim

    hmmm...

    Indeed, I noticed that if (e.KeyCode == Keys.LShiftKey) never became True.

    And I also know that I could use an API call to test whether the Left or the Right Shift key has been pressed, but I am reluctant to use the API call, because I like to keep my code safe.

    Why is there a constant voor the Left Shift Key if I cannot use it


  • LShiftKey doesn't work