[network/kdeconnect-android] src/main/java/org/kde/kdeconnect/plugins/mousepad: plugins/mousepad: Fix keyboard only appearing after 2nd try

David Leppla-Weber <[email protected]>
Newsgroups gmane.comp.kde.cvs
Message-ID <[email protected]>
Git commit 837b67638100159ebe0c5d5b2e97b9b746c6edfe by David Leppla-Weber, on behalf of David Leppla-Weber.
Committed on 31/07/2026 at 14:21.
Pushed by albertvaka into branch 'master'.

plugins/mousepad: Fix keyboard only appearing after 2nd try

I noticed that the keyboard would consistently only appear after I
pressed the keyboard button the 2nd time.

I'm unsure what the exact issue was, but since
"toggleSoftInputFromWindow" is deprecated [1], I switched to explicit
showSoftInput as suggested by the documentation which fixes the observed
issue.

To check whether the IME is currently shown, a
View.OnApplyWindowInsetsListener [2] is added to the keyListenerView,
which from the documentation [3] seems to be the recommended way of
checking for it.

I am unsure why previously "keyListenerView.getRootView()" was passed to
that method, for me this did not work and I had to switch to
"keyListenerView". I tested that the only codepath that previously used
"showKeyboard" directly - that is showing the keyboard when opening the
activity - still works.

[1]: https://developer.android.com/reference/android/view/inputmethod/InputMethodManager#toggleSoftInputFromWindow(android.os.IBinder,%20int,%20int)
[2]: https://developer.android.com/reference/android/view/View.OnApplyWindowInsetsListener
[3]:
https://developer.android.com/reference/android/view/inputmethod/InputMethodManager#showSoftInput(android.view.View,%20int,%20android.os.ResultReceiver)

M  +0    -1    src/main/java/org/kde/kdeconnect/plugins/mousepad/KeyListenerView.java
M  +19   -4    src/main/java/org/kde/kdeconnect/plugins/mousepad/MousePadActivity.java

https://invent.kde.org/network/kdeconnect-android/-/commit/837b67638100159ebe0c5d5b2e97b9b746c6edfe

diff --git a/src/main/java/org/kde/kdeconnect/plugins/mousepad/KeyListenerView.java b/src/main/java/org/kde/kdeconnect/plugins/mousepad/KeyListenerView.java
index 5ad31c1a3..6e1b84ee8 100644
--- a/src/main/java/org/kde/kdeconnect/plugins/mousepad/KeyListenerView.java
+++ b/src/main/java/org/kde/kdeconnect/plugins/mousepad/KeyListenerView.java
@@ -166,5 +166,4 @@ public class KeyListenerView extends View {
         return true;
 
     }
-
 }
diff --git a/src/main/java/org/kde/kdeconnect/plugins/mousepad/MousePadActivity.java b/src/main/java/org/kde/kdeconnect/plugins/mousepad/MousePadActivity.java
index ca7eacf8f..861438031 100644
--- a/src/main/java/org/kde/kdeconnect/plugins/mousepad/MousePadActivity.java
+++ b/src/main/java/org/kde/kdeconnect/plugins/mousepad/MousePadActivity.java
@@ -8,6 +8,7 @@ package org.kde.kdeconnect.plugins.mousepad;
 
 import android.content.Intent;
 import android.content.SharedPreferences;
+import android.graphics.Insets;
 import android.hardware.Sensor;
 import android.hardware.SensorEvent;
 import android.hardware.SensorEventListener;
@@ -21,6 +22,8 @@ import android.view.MenuItem;
 import android.view.MotionEvent;
 import android.view.View;
 import android.view.inputmethod.InputMethodManager;
+import android.view.WindowInsets;
+import android.view.WindowInsets.Type;
 import android.widget.Toast;
 
 import androidx.annotation.NonNull;
@@ -45,7 +48,8 @@ public class MousePadActivity
         GestureDetector.OnDoubleTapListener,
         MousePadGestureDetector.OnGestureListener,
         SensorEventListener,
-        SharedPreferences.OnSharedPreferenceChangeListener {
+        SharedPreferences.OnSharedPreferenceChangeListener,
+        View.OnApplyWindowInsetsListener {
     private String deviceId;
 
     private final static float MinDistanceToSendScroll = 2.5f; // touch gesture scroll
@@ -66,6 +70,7 @@ public class MousePadActivity
     private boolean isScrolling = false;
     private double accumulatedDistanceY = 0;
     private boolean pendingShowKeyboard = false;
+    private boolean keyboardShown = false;
 
     private GestureDetector mDetector;
     private SensorManager mSensorManager;
@@ -163,6 +168,7 @@ public class MousePadActivity
 
         keyListenerView = getBinding().keyListener;
         keyListenerView.setDeviceId(deviceId);
+        keyListenerView.setOnApplyWindowInsetsListener(this);
 
         prefs = PreferenceManager.getDefaultSharedPreferences(this);
         prefs.registerOnSharedPreferenceChangeListener(this);
@@ -494,6 +500,13 @@ public class MousePadActivity
         if (prefsApplied) prefsApplied = false;
     }
 
+    @Override
+    public WindowInsets onApplyWindowInsets(View v, WindowInsets insets) {
+        Insets imeInsets = insets.getInsets(Type.ime());
+        keyboardShown = imeInsets.bottom != 0 || imeInsets.top != 0 
+            || imeInsets.left != 0 || imeInsets.right != 0;
+        return v.onApplyWindowInsets(insets);
+    }
 
     private void sendLeftClick() {
         MousePadPlugin plugin = KdeConnect.getInstance().getDevicePlugin(deviceId, MousePadPlugin.class);
@@ -537,9 +550,11 @@ public class MousePadActivity
     }
 
     private void toggleKeyboard() {
-        InputMethodManager imm = ContextCompat.getSystemService(this, InputMethodManager.class);
-        keyListenerView.requestFocus();
-        imm.toggleSoftInputFromWindow(keyListenerView.getWindowToken(), 0, 0);
+        if (keyboardShown) {
+            hideKeyboard();
+        } else {
+            showKeyboard();
+        }
     }
 
     private void hideKeyboard() {
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.