Skip to content

Commit

Permalink
JBR-7672: Only abort key repeat when the key that is being repeated i…
Browse files Browse the repository at this point in the history
…s released [WLToolkit]
  • Loading branch information
tsarn committed Sep 30, 2024
1 parent 01052f4 commit 997f8b0
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
13 changes: 12 additions & 1 deletion src/java.desktop/unix/classes/sun/awt/wl/WLKeyboard.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ private class KeyRepeatManager {
// Methods and fields should only be accessed from the EDT
private final Timer timer = new Timer("WLKeyboard.KeyRepeatManager", true);
private TimerTask currentRepeatTask;
private int currentRepeatKeycode;
private int delayBeforeRepeatMillis;
private int delayBetweenRepeatMillis;

Expand All @@ -62,12 +63,20 @@ boolean isRepeatEnabled() {
return this.delayBeforeRepeatMillis > 0 || this.delayBetweenRepeatMillis > 0;
}

// called from native code
void cancelRepeat() {
assert EventQueue.isDispatchThread();
if (currentRepeatTask != null) {
currentRepeatTask.cancel();
currentRepeatTask = null;
currentRepeatKeycode = 0;
}
}

// called from native code
void stopRepeat(int keycode) {
assert EventQueue.isDispatchThread();
if (currentRepeatKeycode == keycode) {
cancelRepeat();
}
}

Expand All @@ -79,6 +88,8 @@ void startRepeat(long timestamp, int keycode) {
return;
}

currentRepeatKeycode = keycode;

long delta = timestamp - System.currentTimeMillis();

currentRepeatTask = new TimerTask() {
Expand Down
6 changes: 3 additions & 3 deletions src/java.desktop/unix/native/libawt_wlawt/WLKeyboard.c
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ static jclass keyboardClass; // sun.awt.wl.WLKeyboard
static jclass keyRepeatManagerClass; // sun.awt.wl.WLKeyboard.KeyRepeatManager
static jmethodID setRepeatInfoMID; // sun.awt.wl.WLKeyboard.KeyRepeatManager.setRepeatInfo
static jmethodID startRepeatMID; // sun.awt.wl.WLKeyboard.KeyRepeatManager.startRepeat
static jmethodID cancelRepeatMID; // sun.awt.wl.WLKeyboard.KeyRepeatManager.cancelRepeat
static jmethodID stopRepeatMID; // sun.awt.wl.WLKeyboard.KeyRepeatManager.stopRepeat

static bool
initJavaRefs(JNIEnv *env) {
Expand All @@ -176,7 +176,7 @@ initJavaRefs(JNIEnv *env) {
CHECK_NULL_RETURN(setRepeatInfoMID = (*env)->GetMethodID(env, keyRepeatManagerClass, "setRepeatInfo", "(II)V"),
false);
CHECK_NULL_RETURN(startRepeatMID = (*env)->GetMethodID(env, keyRepeatManagerClass, "startRepeat", "(JI)V"), false);
CHECK_NULL_RETURN(cancelRepeatMID = (*env)->GetMethodID(env, keyRepeatManagerClass, "cancelRepeat", "()V"), false);
CHECK_NULL_RETURN(stopRepeatMID = (*env)->GetMethodID(env, keyRepeatManagerClass, "stopRepeat", "(I)V"), false);
return true;
}

Expand Down Expand Up @@ -1336,7 +1336,7 @@ handleKey(long timestamp, uint32_t keycode, bool isPressed, bool isRepeat) {
JNU_CHECK_EXCEPTION(env);
}
} else if (keyRepeats) {
(*env)->CallVoidMethod(env, keyboard.keyRepeatManager, cancelRepeatMID);
(*env)->CallVoidMethod(env, keyboard.keyRepeatManager, stopRepeatMID, keycode);
JNU_CHECK_EXCEPTION(env);
}
}
Expand Down

0 comments on commit 997f8b0

Please sign in to comment.