Skip to content

Commit c863f8e

Browse files
committed
Discard auto-repeated keys in P2D/P3D
1 parent ea9a4c4 commit c863f8e

5 files changed

Lines changed: 46 additions & 5 deletions

File tree

core/src/processing/core/PApplet.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,11 @@ public class PApplet implements PConstants {
371371
*/
372372
public int pixelHeight;
373373

374+
/**
375+
* Keeps track of ENABLE_KEY_AUTO_REPEAT hint
376+
*/
377+
protected boolean isKeyAutoRepeatEnabled = false;
378+
374379
/**
375380
* ( begin auto-generated from mouseX.xml )
376381
*
@@ -2911,6 +2916,10 @@ public void mouseWheel(MouseEvent event) {
29112916

29122917

29132918
protected void handleKeyEvent(KeyEvent event) {
2919+
2920+
// Get rid of auto-repeating keys if desired and supported
2921+
if (!isKeyAutoRepeatEnabled && event.isAutoRepeat()) return;
2922+
29142923
keyEvent = event;
29152924
key = event.getKey();
29162925
keyCode = event.getKeyCode();

core/src/processing/core/PConstants.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -514,8 +514,11 @@ public interface PConstants {
514514
static final int ENABLE_STROKE_PURE = 9;
515515
static final int DISABLE_STROKE_PURE = -9;
516516

517-
static final int ENABLE_BUFFER_READING = 10;
518-
static final int DISABLE_BUFFER_READING = -10;
517+
static final int ENABLE_BUFFER_READING = 10;
518+
static final int DISABLE_BUFFER_READING = -10;
519519

520-
static final int HINT_COUNT = 11;
520+
static final int DISABLE_KEY_AUTO_REPEAT = 11;
521+
static final int ENABLE_KEY_AUTO_REPEAT = -11;
522+
523+
static final int HINT_COUNT = 12;
521524
}

core/src/processing/core/PGraphics.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1119,6 +1119,11 @@ protected void reapplySettings() {
11191119
* hint(DISABLE_DEPTH_READING) if you don't plan to read depth from
11201120
* this PGraphics anymore.
11211121
* <br/> <br/>
1122+
* hint(ENABLE_KEY_AUTO_REPEAT) - Auto-repeating key events are discarded
1123+
* by default (works only in P2D/P3D); use this hint to get all the key events
1124+
* (including auto-repeated). Call hint(DISABLE_KEY_AUTO_REPEAT) to get events
1125+
* only when the key goes physically up or down.
1126+
* <br/> <br/>
11221127
* As of release 0149, unhint() has been removed in favor of adding
11231128
* additional ENABLE/DISABLE constants to reset the default behavior. This
11241129
* prevents the double negatives, and also reinforces which hints can be
@@ -1139,6 +1144,11 @@ public void hint(int which) {
11391144
showWarning("hint(ENABLE_NATIVE_FONTS) no longer supported. " +
11401145
"Use createFont() instead.");
11411146
}
1147+
if (which == ENABLE_KEY_AUTO_REPEAT) {
1148+
parent.isKeyAutoRepeatEnabled = true;
1149+
} else if (which == DISABLE_KEY_AUTO_REPEAT) {
1150+
parent.isKeyAutoRepeatEnabled = false;
1151+
}
11421152
if (which > 0) {
11431153
hints[which] = true;
11441154
} else {

core/src/processing/event/KeyEvent.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ public class KeyEvent extends Event {
3131
char key;
3232
int keyCode;
3333

34+
boolean isAutoRepeat;
35+
3436

3537
public KeyEvent(Object nativeObject,
3638
long millis, int action, int modifiers,
@@ -41,6 +43,16 @@ public KeyEvent(Object nativeObject,
4143
this.keyCode = keyCode;
4244
}
4345

46+
public KeyEvent(Object nativeObject,
47+
long millis, int action, int modifiers,
48+
char key, int keyCode, boolean isAutoRepeat) {
49+
super(nativeObject, millis, action, modifiers);
50+
this.flavor = KEY;
51+
this.key = key;
52+
this.keyCode = keyCode;
53+
this.isAutoRepeat = isAutoRepeat;
54+
}
55+
4456

4557
public char getKey() {
4658
return key;
@@ -50,4 +62,9 @@ public char getKey() {
5062
public int getKeyCode() {
5163
return keyCode;
5264
}
65+
66+
67+
public boolean isAutoRepeat() {
68+
return isAutoRepeat;
69+
}
5370
}

core/src/processing/opengl/PSurfaceJOGL.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -924,7 +924,8 @@ protected void nativeKeyEvent(com.jogamp.newt.event.KeyEvent nativeEvent,
924924
KeyEvent ke = new KeyEvent(nativeEvent, nativeEvent.getWhen(),
925925
peAction, peModifiers,
926926
keyChar,
927-
keyCode);
927+
keyCode,
928+
nativeEvent.isAutoRepeat());
928929

929930
sketch.postEvent(ke);
930931

@@ -935,7 +936,8 @@ protected void nativeKeyEvent(com.jogamp.newt.event.KeyEvent nativeEvent,
935936
KeyEvent tke = new KeyEvent(nativeEvent, nativeEvent.getWhen(),
936937
KeyEvent.TYPE, peModifiers,
937938
keyChar,
938-
0);
939+
keyCode,
940+
nativeEvent.isAutoRepeat());
939941

940942
sketch.postEvent(tke);
941943
}

0 commit comments

Comments
 (0)