From 8bc88acde55bad29397f76faa831db1d0ea05a2d Mon Sep 17 00:00:00 2001 From: Commodore64user <97603719+Commodore64user@users.noreply.github.com> Date: Fri, 13 Nov 2026 23:02:02 +0000 Subject: [PATCH] ExternalKeyboard: reset modifier state during connection/disconnection A dropped connection can leave a modifier stuck true if the timing is right, so we want to simply clear any potential discrepancies. --- frontend/device/input.lua | 9 +++++++++ plugins/externalkeyboard.koplugin/main.lua | 7 +++++++ 2 files changed, 16 insertions(+) diff --git a/frontend/device/input.lua b/frontend/device/input.lua index 4bfc3e470..3e8a742e9 100644 --- a/frontend/device/input.lua +++ b/frontend/device/input.lua @@ -711,6 +711,15 @@ function Input:resetState() } end +-- A modifier release can get lost (e.g., a BT keyboard dropping connection mid-press +-- while going to sleep), which would leave that modifier stuck as pressed. +function Input:resetModifierKeys() + for key in pairs(self.modifiers) do + self.modifiers[key] = false + end + self.keyboard_toggle_tapped = nil +end + function Input:handleKeyBoardEv(ev) -- Detect loss of contact for the "snow" protocol, as we *never* get EV_ABS:ABS_MT_TRACKING_ID:-1 on those... -- NOTE: The same logic *could* be used on *some* ST devices to detect contact states, diff --git a/plugins/externalkeyboard.koplugin/main.lua b/plugins/externalkeyboard.koplugin/main.lua index dc64297bb..ec707672c 100644 --- a/plugins/externalkeyboard.koplugin/main.lua +++ b/plugins/externalkeyboard.koplugin/main.lua @@ -205,6 +205,10 @@ function ExternalKeyboard:_onEvdevInputRemove(event_path) -- Close our Input handle on it Device.input:close(event_path) + -- Modifier key_release may have been lost while the keyboard + -- was disconnected, don't let them stay stuck true. + Device.input:resetModifierKeys() + ExternalKeyboard.keyboard_fds[event_path] = nil ExternalKeyboard.connected_keyboards = ExternalKeyboard.connected_keyboards - 1 logger.dbg("ExternalKeyboard: USB keyboard", event_path, "was disconnected; total:", ExternalKeyboard.connected_keyboards) @@ -353,6 +357,9 @@ function ExternalKeyboard:setupKeyboard(data) ExternalKeyboard.keyboard_fds[keyboard_info.event_path] = fd -- External keyboard arrows are keyboard-relative, not device-relative. Device.input.rotation_ignored_fds[fd] = true + -- Modifier key_release may have been lost while the keyboard + -- was disconnected, don't let them stay stuck true. + Device.input:resetModifierKeys() ExternalKeyboard.connected_keyboards = ExternalKeyboard.connected_keyboards + 1 logger.dbg("ExternalKeyboard: USB keyboard", keyboard_info.name, "@", keyboard_info.event_path, "was connected; total:", ExternalKeyboard.connected_keyboards)