From 997f8b0bc72680c48c5bd3d87a27c7553abe2cec Mon Sep 17 00:00:00 2001 From: Nikita Tsarev Date: Mon, 30 Sep 2024 10:37:47 +0200 Subject: [PATCH] JBR-7672: Only abort key repeat when the key that is being repeated is released [WLToolkit] --- .../unix/classes/sun/awt/wl/WLKeyboard.java | 13 ++++++++++++- .../unix/native/libawt_wlawt/WLKeyboard.c | 6 +++--- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/src/java.desktop/unix/classes/sun/awt/wl/WLKeyboard.java b/src/java.desktop/unix/classes/sun/awt/wl/WLKeyboard.java index 809a396115a7..1fd4bbbeee02 100644 --- a/src/java.desktop/unix/classes/sun/awt/wl/WLKeyboard.java +++ b/src/java.desktop/unix/classes/sun/awt/wl/WLKeyboard.java @@ -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; @@ -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(); } } @@ -79,6 +88,8 @@ void startRepeat(long timestamp, int keycode) { return; } + currentRepeatKeycode = keycode; + long delta = timestamp - System.currentTimeMillis(); currentRepeatTask = new TimerTask() { diff --git a/src/java.desktop/unix/native/libawt_wlawt/WLKeyboard.c b/src/java.desktop/unix/native/libawt_wlawt/WLKeyboard.c index 5f0d06541baf..9ea1f4b03b45 100644 --- a/src/java.desktop/unix/native/libawt_wlawt/WLKeyboard.c +++ b/src/java.desktop/unix/native/libawt_wlawt/WLKeyboard.c @@ -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) { @@ -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; } @@ -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); } }