LWJGL Forum

Archive => Resolved Bugs/RFE => Topic started by: avm1979 on October 19, 2013, 17:31:17

Title: [FIXED] Right shift key gets stuck being reported as "down"
Post by: avm1979 on October 19, 2013, 17:31:17
If you press one of the shift keys (left or right, doesn't matter), hold it down, then press the other shift key so that both are down at the same time, and then release them in any order, Keyboard.isKeyDown(Keyboard.KEY_RSHIFT) will continue to return true. A "key up" event will also not be generated for the right shift key.

It can be "unstuck" by pressing and releasing the right shift key again.


This is on Windows 7 with LWJGL version 2.8.4.


Minimal test case:
Code: [Select]
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.PixelFormat;

public class ShiftTest {

public static final int SCREEN_WIDTH = 640;
public static final int SCREEN_HEIGHT = 480;

public static void main(String[] args) throws Exception {
setDisplayMode();
Display.setTitle("Shift Test");
Display.setFullscreen(false);
Display.setVSyncEnabled(true);
Display.create(new PixelFormat(32, 0, 24, 8, 0));
Mouse.setGrabbed(false);

while (true) {

boolean lShiftDown = Keyboard.isKeyDown(Keyboard.KEY_LSHIFT);
boolean rShiftDown = Keyboard.isKeyDown(Keyboard.KEY_RSHIFT);

System.out.println(String.format("LShift: %b\tRShift: %b", lShiftDown, rShiftDown));

if (Display.isCloseRequested() || Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)) {
break;
}
Display.update();

}
}

private static void setDisplayMode() throws Exception
{
DisplayMode[] dm = org.lwjgl.util.Display.getAvailableDisplayModes(SCREEN_WIDTH, SCREEN_HEIGHT, -1, -1, -1, -1, 60, 60);
org.lwjgl.util.Display.setDisplayMode(dm, new String[] {
"width=" + SCREEN_WIDTH,
"height=" + SCREEN_HEIGHT,
"freq=" + 60,
"bpp=" + org.lwjgl.opengl.Display.getDisplayMode().getBitsPerPixel()
         });
}
}
Title: Re: [BUG] Right shift key gets stuck being reported as "down"
Post by: spasi on October 19, 2013, 20:35:21
Have you tried the latest LWJGL version?
Title: Re: [BUG] Right shift key gets stuck being reported as "down"
Post by: avm1979 on October 20, 2013, 00:52:19
Just tried it with 2.9.0; same behavior.
Title: Re: [BUG] Right shift key gets stuck being reported as "down"
Post by: spasi on October 20, 2013, 12:33:45
Thanks for the bug report, this should be fixed in the next build.
Title: Re: [BUG] Right shift key gets stuck being reported as "down"
Post by: avm1979 on October 20, 2013, 19:36:37
Cool, thank you!
Title: Re: [FIXED] Right shift key gets stuck being reported as "down"
Post by: FlukeFan on January 05, 2015, 16:19:07
Hi,

This appears to still be happening with the 2.9.2 release.  I don't see a mention of this in the github issues - was an issue raised?

Cheers,
    Richard
Title: Re: [FIXED] Right shift key gets stuck being reported as "down"
Post by: spasi on January 05, 2015, 16:30:43
I'm not seeing any problem with the OP's code. What behavior are you seeing exactly?
Title: Re: [FIXED] Right shift key gets stuck being reported as "down"
Post by: FlukeFan on January 05, 2015, 16:33:56
I'm seeing that when both shift keys are pressed, and released at the same time, isKeyPressed still returns true for one of them (the right shift).

I'm also not seeing the issue that was created to track this, so I'm not sure how to track down what the fix was.

Thanks.
Title: Re: [FIXED] Right shift key gets stuck being reported as "down"
Post by: spasi on January 05, 2015, 17:00:26
isKeyPressed still returns true for one of them (the right shift).

There is no isKeyPressed method, do you mean Keyboard.isKeyDown?

I'm also not seeing the issue that was created to track this, so I'm not sure how to track down what the fix was.

There was no issue iirc. There were two commits that fixed the problem: first (https://github.com/LWJGL/lwjgl/commit/7cb0207eeffca892c34ff0515b2f0822f2839c6e), second (https://github.com/LWJGL/lwjgl/commit/849511784b7651bd30ab35e0ff10c023f28cca9a).
Title: Re: [FIXED] Right shift key gets stuck being reported as "down"
Post by: spasi on January 05, 2015, 17:04:21
If you're sure this is still happening on 2.9.2, could you please share some code that reproduces the problem? Also details on your system might be helpful (OS and Java version, etc)
Title: Re: [FIXED] Right shift key gets stuck being reported as "down"
Post by: FlukeFan on January 05, 2015, 17:51:44
Thanks for pointing me to the commits related to this - it looks like the underlying OS isn't being very helpful here.

I've put together a sample (based on the one above) using lwjgl 2.9.2:  https://github.com/FlukeFan/LwjglShiftTest/blob/master/LwjglShiftTest/src/LwjglShiftTest.java (https://github.com/FlukeFan/LwjglShiftTest/blob/master/LwjglShiftTest/src/LwjglShiftTest.java)

I'm using Windows 7 64-bit.
I'm using Eclipse 4.4.1 and Java 1.7.

The example code displays some text corresponding to whether each of the shift keys is up/down.  If I try pressing/releasing both shift keys together (it sometimes takes a few goes) then the Keyboard class is reporting the right-shift is down when both shift keys are up.
Title: Re: [FIXED] Right shift key gets stuck being reported as "down"
Post by: spasi on January 05, 2015, 18:19:47
Thank you FlukeFan, I have reverted the second commit and it should be fine now. It was a minor optimization that made the workaround fail when the keyboard wasn't polled fast enough.
Title: Re: [FIXED] Right shift key gets stuck being reported as "down"
Post by: FlukeFan on January 05, 2015, 19:26:52
Hi,

Thanks for such a quick update.

I tried recompiling LWJGL (but without the natives).  I dropped that into my test harness, but I'm still seeing the same behaviour.

Do I have to recompile the natives too?

Cheers,
    Richard
Title: Re: [FIXED] Right shift key gets stuck being reported as "down"
Post by: spasi on January 05, 2015, 19:54:10
I tried recompiling LWJGL (but without the natives).  I dropped that into my test harness, but I'm still seeing the same behaviour.

Do I have to recompile the natives too?

No, only Java code changed. Could you please make sure that your test uses the latest commit? I cannot reproduce the issue anymore, with either the original test I was using or your code above.
Title: Re: [FIXED] Right shift key gets stuck being reported as "down"
Post by: FlukeFan on January 05, 2015, 20:50:59
Hi,

Thanks for looking again.

I've checked and re-checked.  I added the following code at line 77 of WindowsKeyboard.java to check it was definitely going through that code, and my test harness promptly blew up when I pressed both shift keys:

Code: [Select]
        if (isKeyDown(Keyboard.KEY_LSHIFT) && isKeyDown(Keyboard.KEY_RSHIFT))
          throw new RuntimeException("blew up here!");

I took the code out again, recreated the jars (using 'ant jars'), and my test harness goes back to displaying the problem.

Note, I do have to try quite a few times to reproduce the problem - I think the 'release' of the two shift keys needs to be almost simultaneous.

Let me know if there's anything else I can do to try and help.

Cheers,
    Richard
Title: Re: [FIXED] Right shift key gets stuck being reported as "down"
Post by: spasi on January 05, 2015, 21:10:31
I think the 'release' of the two shift keys needs to be almost simultaneous.

That's a pretty good hint actually. Could you change the code to:

Code: [Select]
if ( isKeyDown(Keyboard.KEY_LSHIFT) && !isKeyPressedAsync(WindowsKeycodes.VK_LSHIFT) )
handleKey(WindowsKeycodes.VK_SHIFT, Keyboard.KEY_LSHIFT, false, (byte)0, 0L, false);
else if ( isKeyDown(Keyboard.KEY_RSHIFT) && !isKeyPressedAsync(WindowsKeycodes.VK_RSHIFT) )
handleKey(WindowsKeycodes.VK_SHIFT, Keyboard.KEY_RSHIFT, false, (byte)0, 0L, false);
and try again?
Title: Re: [FIXED] Right shift key gets stuck being reported as "down"
Post by: FlukeFan on January 06, 2015, 10:09:42
Hi,

I tried that, but I'm still seeing the same behaviour.  :-(

I don't know if this helps at all, but once the bug occurs, if I switch to another window (say Eclipse, or any other app) without touching the keyboard, Windows seems to think the shift is still down too (my mouse starts selecting text, rather than just moving the pointer).
Title: Re: [FIXED] Right shift key gets stuck being reported as "down"
Post by: Cornix on January 06, 2015, 10:25:01
Have you tried updating your drivers? Maybe its a hardware fault.
Title: Re: [FIXED] Right shift key gets stuck being reported as "down"
Post by: FlukeFan on January 06, 2015, 11:12:30
Hi,

Ok, I've managed to reproduce this in Notepad alone, so it looks like a hardware problem (if I press/release both shift keys then press a letter, every 10th (or so) is capitalised).  I'm amazed, because I've never seen any other problems with this (Dell) laptop's keyboard.

I'm really sorry for wasting everyone's time.  Thanks for all your help.

Richard
Title: Re: [FIXED] Right shift key gets stuck being reported as "down"
Post by: cursorcorner on May 24, 2015, 21:12:21
This issue is reproducible in many API's and many programs. It is not an issue with the hardware, but appears to be an issue with ambiguity in W3C DOM keyboard event handling standard. The only legal types that the standard allows for are left, right, number pad and standard. This leaves ambiguity in how API's are expected to handle the same key code with multiple simultaneous key positions.

On a 2012 Macbook Pro running Yosemite 10, I am also seeing the left/right shift stuck-key behavior. This issue also exists for left/right alt and ctrl keys. The issue is easily reproduced by holding the left key, then the right, then releasing the left and finally the right for any of types mentioned.

AWT correctly alerts that one of the two (either left or right) is released via the event handler leaving the other in what might be interpreted as a stuck state by applications that try to distinguish left and right keys, but LWJGL does not report that either has been released for some reason. This is why the key is sometimes still reported as "pressed." I also saw this issue in Windows 7.

The mentioned issue is why many games refuse to distinguish left and right action-keys even though the hardware is capable of distinguishing them. The only Java API I have seen that is capable of directly accessing the left/right action key states so far is jnativehook, but it shows the simultaneous selection of left and right action keys as a modifier change only and not as an event.

If this issue can be fixed in LWJGL, I would choose it over AWT. I can also do some digging in the code to see if I find anything.

Sample code:

import org.lwjgl.LWJGLException;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
 
public class InputSampler {
 
    public void start() {
        try {
           Display.setDisplayMode(new DisplayMode(800, 600));
           Display.create();
       } catch (LWJGLException e) {
           e.printStackTrace();
           System.exit(0);
       }
        while (!Display.isCloseRequested()) {
        pollInput();
        Display.update();
    }
 
    Display.destroy();
    }
 
    public void pollInput() {
         
       while( Keyboard.next() ) {
           if( Keyboard.getEventKeyState() ) {
              System.out.print(Keyboard.getKeyName(Keyboard.getEventKey()));
              System.out.println(" pressed");
          } else {
              System.out.print(Keyboard.getKeyName(Keyboard.getEventKey()));
              System.out.println(" released");
           }
       }

    }
 
    public static void main(String[] argv) {
        InputSampler inputExample = new InputSampler();
        inputExample.start();
    }

}
Title: Re: [FIXED] Right shift key gets stuck being reported as "down"
Post by: FlukeFan on May 27, 2015, 14:40:09
I managed to replicate this in Notepad.  I don't think any API that runs on top of the operating system will be able to avoid this?
Title: Re: [FIXED] Right shift key gets stuck being reported as "down"
Post by: spasi on May 27, 2015, 15:32:21
If this issue can be fixed in LWJGL, I would choose it over AWT. I can also do some digging in the code to see if I find anything.

I cannot reproduce what you describe, on either Windows or OS X. I used both your code above and other LWJGL tests.

Have you tried LWJGL 3 on the same machines?