From 9f9e2f40aea95d8b3964d90f5804accf92b4a6b9 Mon Sep 17 00:00:00 2001 From: Sergi095 Date: Sat, 14 Nov 2026 23:00:37 +0100 Subject: [PATCH 1/2] Keep KVM sessions through rotation and hardware keyboard changes --- README.md | 1 + app/build.gradle | 4 +- app/src/main/AndroidManifest.xml | 1 + .../java/org/androidkvm/app/MainActivity.java | 131 +++++++++++++++++- .../org/androidkvm/app/NanoTerminalPanel.java | 5 + 5 files changed, 135 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 9c4f240..5ab9908 100644 --- a/README.md +++ b/README.md @@ -36,6 +36,7 @@ If you previously used a PiKVM bridge profile for NanoKVM, add the NanoKVM's own | Move and click | Touch the video to move the pointer. Tap to click, double tap (or tap with two fingers) to double click, and hold to right click. Pinch to zoom; move two fingers to pan while zoomed. Swipe with two fingers to scroll when the image fits the screen. | | Adjust video | Tap **⋮ → Video settings**. PiKVM settings stay limited to frame rate, JPEG quality, supported capture resolutions, and phone display. NanoKVM also offers H.264 or MJPEG, bitrate, and GOP. | | Type | Tap **⌨** for your Android keyboard. The extra key bar includes Ctrl, Alt, **WIN/⌘**, arrows, F1–F12, and Delete. Tap WIN/⌘ to open the host menu; hold it, then press another key for shortcuts. In the terminal, tap it as a Meta modifier. Hide the keyboard to enlarge the screen. | +| Use a physical keyboard | Connect a keyboard to your phone or tablet to type on the KVM. The extra keys stay visible without opening the Android keyboard. Rotating the device keeps the KVM connection open. | | Use a NanoKVM terminal | Tap **⋮ → NanoKVM terminal** for its shell, or **Serial terminal** to choose a port and speed. The terminal opens inside the console and uses the same extra keys above your Android keyboard. | | Manage NanoKVM scripts | Tap **⋮ → NanoKVM scripts** to upload, run, or delete `.sh` and `.py` scripts on the NanoKVM device. PiKVM has keyboard macros, which are a different feature. | | Configure NanoKVM | Tap **⋮ → NanoKVM settings** for its password, device, USB, Wi-Fi, DNS, and system controls. Open its full web interface there for other settings. **Security settings** manages the app's PIN and biometrics. | diff --git a/app/build.gradle b/app/build.gradle index b97e83d..bfe8bc1 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -16,8 +16,8 @@ android { applicationId 'org.androidkvm.app' minSdk 26 targetSdk 37 - versionCode 35 - versionName '0.2.9' + versionCode 36 + versionName '0.2.10' } dependenciesInfo { diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index d7caf7a..2ba0084 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -14,6 +14,7 @@ diff --git a/app/src/main/java/org/androidkvm/app/MainActivity.java b/app/src/main/java/org/androidkvm/app/MainActivity.java index 3389af2..a48800f 100644 --- a/app/src/main/java/org/androidkvm/app/MainActivity.java +++ b/app/src/main/java/org/androidkvm/app/MainActivity.java @@ -244,6 +244,28 @@ public final class MainActivity extends FragmentActivity { super.onSaveInstanceState(state); } + @Override public void onConfigurationChanged(Configuration configuration) { + super.onConfigurationChanged(configuration); + if (client != null && hasHardwareKeyboard(configuration)) { + View focused = getCurrentFocus(); + InputMethodManager keyboard = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE); + if (focused != null && keyboard != null) + keyboard.hideSoftInputFromWindow(focused.getWindowToken(), 0); + } + handler.post(() -> { + updateLoginKeyboardInset(); + updateConsoleKeyboardInset(); + if (screen != null) { screen.resetGesture(); screen.invalidate(); } + updateFastVideoTransform(); + }); + } + + private boolean hasHardwareKeyboard(Configuration configuration) { + return configuration.keyboard != Configuration.KEYBOARD_NOKEYS + && configuration.keyboard != Configuration.KEYBOARD_UNDEFINED + && configuration.hardKeyboardHidden != Configuration.HARDKEYBOARDHIDDEN_YES; + } + @Override public void onStart() { super.onStart(); foreground = true; @@ -2139,8 +2161,18 @@ private void updateConsoleKeyboardInset() { consolePage.getWindowVisibleDisplayFrame(visible); WindowInsets insets = consolePage.getRootWindowInsets(); boolean keyboardShown = isConsoleKeyboardShown(insets, visible); - if (extraKeys != null && extraKeys.getVisibility() != (keyboardShown ? View.VISIBLE : View.GONE)) - extraKeys.setVisibility(keyboardShown ? View.VISIBLE : View.GONE); + boolean showExtraKeys = keyboardShown || hasHardwareKeyboard(getResources().getConfiguration()); + if (extraKeys != null) { + int width = Math.min(consolePage.getWidth(), dp(784)); + LinearLayout.LayoutParams keysLayout = (LinearLayout.LayoutParams) extraKeys.getLayoutParams(); + if (width > 0 && (keysLayout.width != width || keysLayout.gravity != Gravity.CENTER_HORIZONTAL)) { + keysLayout.width = width; + keysLayout.gravity = Gravity.CENTER_HORIZONTAL; + extraKeys.setLayoutParams(keysLayout); + } + if (extraKeys.getVisibility() != (showExtraKeys ? View.VISIBLE : View.GONE)) + extraKeys.setVisibility(showExtraKeys ? View.VISIBLE : View.GONE); + } int[] position = new int[2]; consolePage.getLocationOnScreen(position); int contentBottom = position[1] + consolePage.getHeight(); @@ -2168,15 +2200,35 @@ private boolean isConsoleKeyboardShown(WindowInsets insets, Rect visible) { private HorizontalScrollView keyRow(String[][] keys) { HorizontalScrollView scroll = new HorizontalScrollView(this); scroll.setHorizontalScrollBarEnabled(false); + scroll.setFillViewport(true); LinearLayout row = row(); + int minimumWidth = dp(54); + row.setMinimumWidth(keys.length * minimumWidth); for (String[] item : keys) { String label = item[0], code = item[1]; Button key = compactButton(label); - LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(dp(54), dp(43)); + LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(minimumWidth, dp(43)); row.addView(key, params); bindKey(key, label, code); } - scroll.addView(row); + scroll.addView(row, new FrameLayout.LayoutParams(keys.length * minimumWidth, dp(43))); + final int[] lastWidth = {-1}; + scroll.addOnLayoutChangeListener((view, left, top, right, bottom, oldLeft, oldTop, oldRight, oldBottom) -> { + int width = right - left; + if (width <= 0 || width == lastWidth[0]) return; + lastWidth[0] = width; + boolean fits = width >= keys.length * minimumWidth; + FrameLayout.LayoutParams rowLayout = (FrameLayout.LayoutParams) row.getLayoutParams(); + rowLayout.width = fits ? width : keys.length * minimumWidth; + row.setMinimumWidth(rowLayout.width); + row.setLayoutParams(rowLayout); + for (int index = 0; index < row.getChildCount(); index++) { + LinearLayout.LayoutParams keyLayout = (LinearLayout.LayoutParams) row.getChildAt(index).getLayoutParams(); + keyLayout.width = fits ? 0 : minimumWidth; + keyLayout.weight = fits ? 1 : 0; + row.getChildAt(index).setLayoutParams(keyLayout); + } + }); return scroll; } @@ -2582,7 +2634,8 @@ private void login(String type) { if (!locked) remoteInput.postDelayed(() -> { remoteInput.requestFocus(); InputMethodManager keyboard = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE); - if (keyboard != null) keyboard.showSoftInput(remoteInput, InputMethodManager.SHOW_IMPLICIT); + if (keyboard != null && !hasHardwareKeyboard(getResources().getConfiguration())) + keyboard.showSoftInput(remoteInput, InputMethodManager.SHOW_IMPLICIT); }, 150); }); } catch (Exception error) { @@ -2790,10 +2843,20 @@ private void updateModifierButtons() { } private void sendKey(String code) { + sendKey(code, null); + } + + private void sendKey(String code, KeyEvent hardwareEvent) { PikvmClient active = client; if (active == null) return; flushTextNow(); List held = consumeModifiers(); + if (hardwareEvent != null) { + if (hardwareEvent.isCtrlPressed() && !held.contains("ControlLeft")) held.add("ControlLeft"); + if (hardwareEvent.isAltPressed() && !held.contains("AltLeft")) held.add("AltLeft"); + if (hardwareEvent.isMetaPressed() && !held.contains("MetaLeft")) held.add("MetaLeft"); + if (hardwareEvent.isShiftPressed() && !held.contains("ShiftLeft")) held.add("ShiftLeft"); + } if (terminalPanel != null) { terminalPanel.sendKey(code, held); return; @@ -2870,6 +2933,45 @@ private String keyForCharacter(char value) { } } + private static String hardwareKeyCode(int keyCode) { + if (keyCode >= KeyEvent.KEYCODE_A && keyCode <= KeyEvent.KEYCODE_Z) + return "Key" + (char) ('A' + keyCode - KeyEvent.KEYCODE_A); + if (keyCode >= KeyEvent.KEYCODE_0 && keyCode <= KeyEvent.KEYCODE_9) + return "Digit" + (keyCode - KeyEvent.KEYCODE_0); + if (keyCode >= KeyEvent.KEYCODE_F1 && keyCode <= KeyEvent.KEYCODE_F12) + return "F" + (keyCode - KeyEvent.KEYCODE_F1 + 1); + switch (keyCode) { + case KeyEvent.KEYCODE_ENTER: + case KeyEvent.KEYCODE_NUMPAD_ENTER: return "Enter"; + case KeyEvent.KEYCODE_ESCAPE: return "Escape"; + case KeyEvent.KEYCODE_DEL: return "Backspace"; + case KeyEvent.KEYCODE_FORWARD_DEL: return "Delete"; + case KeyEvent.KEYCODE_TAB: return "Tab"; + case KeyEvent.KEYCODE_SPACE: return "Space"; + case KeyEvent.KEYCODE_DPAD_LEFT: return "ArrowLeft"; + case KeyEvent.KEYCODE_DPAD_RIGHT: return "ArrowRight"; + case KeyEvent.KEYCODE_DPAD_UP: return "ArrowUp"; + case KeyEvent.KEYCODE_DPAD_DOWN: return "ArrowDown"; + case KeyEvent.KEYCODE_MOVE_HOME: return "Home"; + case KeyEvent.KEYCODE_MOVE_END: return "End"; + case KeyEvent.KEYCODE_PAGE_UP: return "PageUp"; + case KeyEvent.KEYCODE_PAGE_DOWN: return "PageDown"; + case KeyEvent.KEYCODE_INSERT: return "Insert"; + case KeyEvent.KEYCODE_MINUS: return "Minus"; + case KeyEvent.KEYCODE_EQUALS: return "Equal"; + case KeyEvent.KEYCODE_LEFT_BRACKET: return "BracketLeft"; + case KeyEvent.KEYCODE_RIGHT_BRACKET: return "BracketRight"; + case KeyEvent.KEYCODE_BACKSLASH: return "Backslash"; + case KeyEvent.KEYCODE_SEMICOLON: return "Semicolon"; + case KeyEvent.KEYCODE_APOSTROPHE: return "Quote"; + case KeyEvent.KEYCODE_GRAVE: return "Backquote"; + case KeyEvent.KEYCODE_COMMA: return "Comma"; + case KeyEvent.KEYCODE_PERIOD: return "Period"; + case KeyEvent.KEYCODE_SLASH: return "Slash"; + default: return null; + } + } + private PointF screenPoint(float x, float y) { if (videoWidth <= 0 || videoHeight <= 0 || screen.getWidth() == 0 || screen.getHeight() == 0) return null; float scale = videoScale(screen.getWidth(), screen.getHeight(), videoWidth, videoHeight); @@ -2973,6 +3075,25 @@ private final class RemoteInput extends View { @Override public boolean onCheckIsTextEditor() { return true; } + @Override public boolean onKeyDown(int keyCode, KeyEvent event) { + if (client == null) return super.onKeyDown(keyCode, event); + String code = hardwareKeyCode(keyCode); + if (code == null) return super.onKeyDown(keyCode, event); + if (event.isCtrlPressed() || event.isAltPressed() || event.isMetaPressed()) { + sendKey(code, event); + return true; + } + int character = event.getUnicodeChar(); + if (character > 0 && !Character.isISOControl(character)) + sendRemoteText(new String(Character.toChars(character))); + else sendKey(code, event); + return true; + } + + @Override public boolean onKeyUp(int keyCode, KeyEvent event) { + return (client != null && hardwareKeyCode(keyCode) != null) || super.onKeyUp(keyCode, event); + } + @Override public InputConnection onCreateInputConnection(EditorInfo info) { info.inputType = InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS; diff --git a/app/src/main/java/org/androidkvm/app/NanoTerminalPanel.java b/app/src/main/java/org/androidkvm/app/NanoTerminalPanel.java index 35464a4..0b17c4f 100644 --- a/app/src/main/java/org/androidkvm/app/NanoTerminalPanel.java +++ b/app/src/main/java/org/androidkvm/app/NanoTerminalPanel.java @@ -1,6 +1,7 @@ package org.androidkvm.app; import android.content.Context; +import android.content.res.Configuration; import android.graphics.Color; import android.os.Handler; import android.os.Looper; @@ -175,6 +176,10 @@ void showKeyboard() { if (stopped) return; terminal.requestFocus(); terminal.evaluateJavascript("window.terminalFocus()", null); + Configuration configuration = getResources().getConfiguration(); + if (configuration.keyboard != Configuration.KEYBOARD_NOKEYS + && configuration.keyboard != Configuration.KEYBOARD_UNDEFINED + && configuration.hardKeyboardHidden != Configuration.HARDKEYBOARDHIDDEN_YES) return; InputMethodManager input = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE); if (input != null) handler.postDelayed(() -> input.showSoftInput(terminal, InputMethodManager.SHOW_IMPLICIT), 120); From baf30c809b184f9f8196ae9e9c88a4d2d48ea9cc Mon Sep 17 00:00:00 2001 From: Sergi095 Date: Sat, 14 Nov 2026 23:08:08 +0100 Subject: [PATCH 2/2] Keep function keys visible above landscape keyboard --- .../org/androidkvm/app/ExtraKeyLayout.java | 8 +++ .../java/org/androidkvm/app/MainActivity.java | 54 +++++++++++++++++-- 2 files changed, 59 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/org/androidkvm/app/ExtraKeyLayout.java b/app/src/main/java/org/androidkvm/app/ExtraKeyLayout.java index 7c149cb..8296bb4 100644 --- a/app/src/main/java/org/androidkvm/app/ExtraKeyLayout.java +++ b/app/src/main/java/org/androidkvm/app/ExtraKeyLayout.java @@ -11,6 +11,14 @@ final class ExtraKeyLayout { {"←", "ArrowLeft"}, {"↓", "ArrowDown"}, {"→", "ArrowRight"}, {"PGDN", "PageDown"} }; static final String[][] FUNCTIONS = functionKeys(); + static final String[][] COMPACT_FIXED = compactFixedKeys(); + + private static String[][] compactFixedKeys() { + String[][] keys = new String[FIRST.length + SECOND.length][]; + System.arraycopy(FIRST, 0, keys, 0, FIRST.length); + System.arraycopy(SECOND, 0, keys, FIRST.length, SECOND.length); + return keys; + } private static String[][] functionKeys() { String[][] keys = new String[14][2]; diff --git a/app/src/main/java/org/androidkvm/app/MainActivity.java b/app/src/main/java/org/androidkvm/app/MainActivity.java index a48800f..2bec6de 100644 --- a/app/src/main/java/org/androidkvm/app/MainActivity.java +++ b/app/src/main/java/org/androidkvm/app/MainActivity.java @@ -131,6 +131,7 @@ public final class MainActivity extends FragmentActivity { private LinearLayout savedPanel; private LinearLayout kvmHome, updatesHome; private LinearLayout extraKeys; + private View firstKeysRow, secondKeysRow, compactFixedKeysRow, functionKeysRow; private EditText addressInput, userInput, passwordInput; private Button deviceTypeInput; private String selectedDeviceType = ""; @@ -2075,9 +2076,15 @@ private void buildUi() { extraKeys = column(); extraKeys.setBackgroundColor(CONSOLE_BG); - extraKeys.addView(compactKeyRow(ExtraKeyLayout.FIRST)); - extraKeys.addView(compactKeyRow(ExtraKeyLayout.SECOND)); - extraKeys.addView(keyRow(ExtraKeyLayout.FUNCTIONS)); + firstKeysRow = compactKeyRow(ExtraKeyLayout.FIRST); + secondKeysRow = compactKeyRow(ExtraKeyLayout.SECOND); + compactFixedKeysRow = keyRow(ExtraKeyLayout.COMPACT_FIXED); + compactFixedKeysRow.setVisibility(View.GONE); + functionKeysRow = keyRow(ExtraKeyLayout.FUNCTIONS); + extraKeys.addView(firstKeysRow); + extraKeys.addView(secondKeysRow); + extraKeys.addView(compactFixedKeysRow); + extraKeys.addView(functionKeysRow); consolePage.addView(extraKeys); } @@ -2186,11 +2193,52 @@ private void updateConsoleKeyboardInset() { } } int bottom = Math.max(consoleBottomInset, covered); + int usableHeight = Math.max(0, consolePage.getHeight() - consoleTopInset - bottom); + boolean compact = usableHeight < dp(130); + boolean hideStatus = usableHeight < dp(193); + if (terminalPanel == null && videoStatus.getVisibility() != (hideStatus ? View.GONE : View.VISIBLE)) + videoStatus.setVisibility(hideStatus ? View.GONE : View.VISIBLE); + if (firstKeysRow.getVisibility() != (compact ? View.GONE : View.VISIBLE)) { + firstKeysRow.setVisibility(compact ? View.GONE : View.VISIBLE); + secondKeysRow.setVisibility(compact ? View.GONE : View.VISIBLE); + compactFixedKeysRow.setVisibility(compact ? View.VISIBLE : View.GONE); + } + int rows = compact ? 2 : 3; + int statusHeight = hideStatus || terminalPanel != null ? 0 : dp(18); + int rowHeight = Math.min(dp(43), Math.max(dp(24), + (usableHeight - dp(46) - statusHeight) / rows)); + resizeExtraKeyRow(compact ? compactFixedKeysRow : firstKeysRow, rowHeight); + if (!compact) resizeExtraKeyRow(secondKeysRow, rowHeight); + resizeExtraKeyRow(functionKeysRow, rowHeight); if (consolePage.getPaddingTop() != consoleTopInset || consolePage.getPaddingBottom() != bottom) { consolePage.setPadding(0, consoleTopInset, 0, bottom); } } + private void resizeExtraKeyRow(View keyRow, int height) { + ViewGroup.LayoutParams outer = keyRow.getLayoutParams(); + if (outer.height != height) { + outer.height = height; + keyRow.setLayoutParams(outer); + } + ViewGroup content = keyRow instanceof HorizontalScrollView + ? (ViewGroup) ((HorizontalScrollView) keyRow).getChildAt(0) : (ViewGroup) keyRow; + if (content != keyRow) { + ViewGroup.LayoutParams inner = content.getLayoutParams(); + if (inner.height != height) { + inner.height = height; + content.setLayoutParams(inner); + } + } + for (int index = 0; index < content.getChildCount(); index++) { + View key = content.getChildAt(index); + ViewGroup.LayoutParams keyLayout = key.getLayoutParams(); + if (keyLayout.height == height) continue; + keyLayout.height = height; + key.setLayoutParams(keyLayout); + } + } + private boolean isConsoleKeyboardShown(WindowInsets insets, Rect visible) { if (Build.VERSION.SDK_INT >= 30 && insets != null) return insets.isVisible(WindowInsets.Type.ime());