New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement a way to disable automatic key repeat #1622

Closed
processing-bugs opened this Issue Feb 10, 2013 · 13 comments

Comments

Projects
None yet
5 participants
@processing-bugs

processing-bugs commented Feb 10, 2013

Original author: gargajcns@gmail.com (February 03, 2013 13:25:31)

  1. Write the following sketch: (I'm on Windows)

void setup(){ size(640,480,P3D); }
void draw(){}
void keyPressed() { println("Keydown: "+keyCode); }
void keyReleased() { println("Keyup: "+keyCode); }

  1. Alt-Tab a few times so that the window loses/regains focus.
  2. Press AND HOLD any key.

Result:

Keydown: 69
Keyup: 69
Keydown: 69
Keyup: 69
Keydown: 69
Keyup: 69
Keydown: 69
Keyup: 69
[etc]

...instead of one Keydown waiting for a Keyup.

I went to PGL.Java and added
if (nativeEvent.isAutoRepeat()) return;

to
protected void nativeKeyEvent

And that seemed to have fixed the problem, although I understand that for a textbox, that behaviour might be the exact opposite of the desired one. Unfortunately I was making a keyboard-controlled game, so I needed an isKeyPressed("X")-style behaviour. (Which, by the way, is badly needed.)

Original issue: http://code.google.com/p/processing/issues/detail?id=1584

@processing-bugs

This comment has been minimized.

Show comment
Hide comment
@processing-bugs

processing-bugs Feb 10, 2013

From f...@processing.org on February 03, 2013 16:02:12
Changing to a feature request. Though we might be able to do this on the OpenGL side, we can't for the regular 2D renderer and probably cannot across Android and JavaScript consistently either.

processing-bugs commented Feb 10, 2013

From f...@processing.org on February 03, 2013 16:02:12
Changing to a feature request. Though we might be able to do this on the OpenGL side, we can't for the regular 2D renderer and probably cannot across Android and JavaScript consistently either.

@processing-bugs

This comment has been minimized.

Show comment
Hide comment
@processing-bugs

processing-bugs Feb 10, 2013

From gargajcns@gmail.com on February 03, 2013 16:10:53
Wouldn't it be plausible to introduce an asynchronous keyboard check function (like Windows' GetAsyncKeyState) to this end?

Most people seem to work this situation around by providing their own "boolean keys[512];" array and then flicking it back and forth via keyPressed / keyReleased. If you can do this internally (while dodging the auto-repeat problem on the OpenGL end), you can probably have it both ways.

processing-bugs commented Feb 10, 2013

From gargajcns@gmail.com on February 03, 2013 16:10:53
Wouldn't it be plausible to introduce an asynchronous keyboard check function (like Windows' GetAsyncKeyState) to this end?

Most people seem to work this situation around by providing their own "boolean keys[512];" array and then flicking it back and forth via keyPressed / keyReleased. If you can do this internally (while dodging the auto-repeat problem on the OpenGL end), you can probably have it both ways.

@benfry

This comment has been minimized.

Show comment
Hide comment
@benfry

benfry Feb 11, 2013

Member

Yes, that would be the way to go about it. It would require native code for Mac OS X, Windows, and Linux (and for both 32- and 64-bit) to work anywhere besides from the OpenGL renderers. We'd also need need to test the plausibility of whether it could be done with Android and with JavaScript. Contributions welcome.

Member

benfry commented Feb 11, 2013

Yes, that would be the way to go about it. It would require native code for Mac OS X, Windows, and Linux (and for both 32- and 64-bit) to work anywhere besides from the OpenGL renderers. We'd also need need to test the plausibility of whether it could be done with Android and with JavaScript. Contributions welcome.

@lharding

This comment has been minimized.

Show comment
Hide comment
@lharding

lharding Mar 16, 2013

In absence of a decent way of fixing this portably, here's my workaround hack for systems where it's possible to recognize a key-repeat event pair by way of noticing that the key goes down again too soon after coming up (fair warning, it's functional, not pretty):

https://gist.github.com/lharding/5176082

This works on my linux box with a maximum timeout of 5ms between up/down events. You may have to tweak this ("DEBOUNCE" in the code) depending on how loaded the system you're running on is.

EDIT: Updated my sample code to handle the case where two keys go down or up on the same millisecond. Also, discovered that I'm capable of repeatably pressing two keys on the same millisecond.

lharding commented Mar 16, 2013

In absence of a decent way of fixing this portably, here's my workaround hack for systems where it's possible to recognize a key-repeat event pair by way of noticing that the key goes down again too soon after coming up (fair warning, it's functional, not pretty):

https://gist.github.com/lharding/5176082

This works on my linux box with a maximum timeout of 5ms between up/down events. You may have to tweak this ("DEBOUNCE" in the code) depending on how loaded the system you're running on is.

EDIT: Updated my sample code to handle the case where two keys go down or up on the same millisecond. Also, discovered that I'm capable of repeatably pressing two keys on the same millisecond.

@benfry

This comment has been minimized.

Show comment
Hide comment
@benfry

benfry Jun 16, 2015

Member

@codeanticode is this done easily with JOGL? I suspect JavaFX can do it as well, though Java2D won't really do it properly.

Member

benfry commented Jun 16, 2015

@codeanticode is this done easily with JOGL? I suspect JavaFX can do it as well, though Java2D won't really do it properly.

@JakubValtar

This comment has been minimized.

Show comment
Hide comment
@JakubValtar

JakubValtar Aug 6, 2015

Contributor

We can roll our own keyboard filter. For Java2D, there are multiple keyDown followed by one keyUp, so it's easy. For OpenGL, there is InputEvent.isAutoRepeat() which tells you if it's the real deal or not.

I can implement this one later.

Contributor

JakubValtar commented Aug 6, 2015

We can roll our own keyboard filter. For Java2D, there are multiple keyDown followed by one keyUp, so it's easy. For OpenGL, there is InputEvent.isAutoRepeat() which tells you if it's the real deal or not.

I can implement this one later.

@JakubValtar JakubValtar added revised and removed revision needed labels Aug 6, 2015

@benfry

This comment has been minimized.

Show comment
Hide comment
@benfry

benfry Aug 6, 2015

Member

Let's skip it on Java2D and make it work with JavaFX and OpenGL.

Member

benfry commented Aug 6, 2015

Let's skip it on Java2D and make it work with JavaFX and OpenGL.

@JakubValtar

This comment has been minimized.

Show comment
Hide comment
@JakubValtar

JakubValtar Aug 6, 2015

Contributor

Sounds like a plan.

Contributor

JakubValtar commented Aug 6, 2015

Sounds like a plan.

@Natura-Time

This comment has been minimized.

Show comment
Hide comment
@Natura-Time

Natura-Time Aug 9, 2015

Also, keyTyped() don't work at all with P2D/P3D.

Natura-Time commented Aug 9, 2015

Also, keyTyped() don't work at all with P2D/P3D.

@JakubValtar

This comment has been minimized.

Show comment
Hide comment
@JakubValtar

JakubValtar Aug 9, 2015

Contributor

Ok, thanks for the report, I'll also try to standardize the key events across renderers when I get to this.

Contributor

JakubValtar commented Aug 9, 2015

Ok, thanks for the report, I'll also try to standardize the key events across renderers when I get to this.

@benfry

This comment has been minimized.

Show comment
Hide comment
@benfry

benfry Aug 11, 2015

Member

Let's make the default no auto-repeat, since that's what most people will expect. Maybe we use a hint() to turn it back on.

Member

benfry commented Aug 11, 2015

Let's make the default no auto-repeat, since that's what most people will expect. Maybe we use a hint() to turn it back on.

@benfry benfry added this to the 3.0 final milestone Aug 11, 2015

@benfry

This comment has been minimized.

Show comment
Hide comment
@benfry

benfry Aug 11, 2015

Member

However, if we're going to make that the default, we may need to implement it for Java2D using a hack like the gist in the previous comment (the debouncing notes).

Member

benfry commented Aug 11, 2015

However, if we're going to make that the default, we may need to implement it for Java2D using a hack like the gist in the previous comment (the debouncing notes).

@benfry

This comment has been minimized.

Show comment
Hide comment
@benfry

benfry Sep 11, 2015

Member

Implemented by @JakubValtar for 3.0 beta 6. Works with OpenGL, where key repeat is now disabled by default. hint(ENABLE_KEY_REPEAT) will turn it back on.

For Java2D, it doesn't make sense because we have to write a lot of platform-specific de-bouncing code. JavaFX will likely have an option more like the one used in OpenGL.

Member

benfry commented Sep 11, 2015

Implemented by @JakubValtar for 3.0 beta 6. Works with OpenGL, where key repeat is now disabled by default. hint(ENABLE_KEY_REPEAT) will turn it back on.

For Java2D, it doesn't make sense because we have to write a lot of platform-specific de-bouncing code. JavaFX will likely have an option more like the one used in OpenGL.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment