From 8285f4e3bb90ab0d4b0b37bf5e77bb7f3e023f9d Mon Sep 17 00:00:00 2001 From: David <97603719+Commodore64user@users.noreply.github.com> Date: Tue, 3 Feb 2026 21:10:53 +0000 Subject: [PATCH] [InputText] Decouple text input from VirtualKeyboard visibility (#14901) Previously, on SDL targets (Desktop, Ubuntu Touch), the system text input loop was gated by the visibility of the On-Screen Virtual Keyboard. Hiding the VK would call Device:stopTextInput, effectively disabling the physical keyboard. This commit binds the text input state to the lifecycle of the InputText widget instead: * Improve up/down behavior. Down at the end of an input moves focus down, up at the beginning moves focus up. * Undo removal of key_event for touch devices, allowing vk to work regardless of vk state, #FreeTheKeyboard --- frontend/ui/widget/inputtext.lua | 37 +++++++++++++++---- frontend/ui/widget/virtualkeyboard.lua | 51 +++----------------------- 2 files changed, 35 insertions(+), 53 deletions(-) diff --git a/frontend/ui/widget/inputtext.lua b/frontend/ui/widget/inputtext.lua index 8f8278143..dae8cbd3c 100644 --- a/frontend/ui/widget/inputtext.lua +++ b/frontend/ui/widget/inputtext.lua @@ -403,6 +403,12 @@ function InputText:init() self:initKeyboard() self:initEventListener() end + --- @todo In MultiInputDialogs, this will fire multiple times as that widget both inherits + -- inputtexts from InputDialog and also creates its own. + -- See . + if self.focused and not self.for_measurement_only then + Device:startTextInput() + end end -- This will be called when we add or del chars, as we need to recreate @@ -588,12 +594,14 @@ function InputText:unfocus() self.focused = false self.text_widget:unfocus() self._frame_textwidget.color = Blitbuffer.COLOR_DARK_GRAY + Device:stopTextInput() end function InputText:focus() self.focused = true self.text_widget:focus() self._frame_textwidget.color = Blitbuffer.COLOR_BLACK + Device:startTextInput() end -- NOTE: This key_map can be used for keyboards without numeric keys, such as on Kindles with keyboards. It is loosely 'inspired' by the symbol layer on the virtual keyboard but, @@ -632,12 +640,25 @@ function InputText:onKeyPress(key) self:leftChar() elseif key["Right"] then self:rightChar() - -- NOTE: When we are not showing the virtual keyboard, let focusmanger handle up/down keys, as they are used to directly move around the widget - -- seamlessly in and out of text fields and onto virtual buttons like `[cancel] [search dict]`, no need to unfocus first. - elseif key["Up"] and G_reader_settings:nilOrTrue("virtual_keyboard_enabled") then + -- NOTE: The VirtualKeyboard has focus when shown, and handles up/down/left/right. + elseif key["Up"] then + if #self.charlist == 0 then + return false -- let FocusManager move focus up + end + local old_charpos, old_top = self.charpos, self.top_line_num self:upLine() - elseif key["Down"] and G_reader_settings:nilOrTrue("virtual_keyboard_enabled") then + if self.charpos == old_charpos and self.top_line_num == old_top then + return false -- let FocusManager move focus up + end + elseif key["Down"] then + if #self.charlist == 0 then + return false -- let FocusManager move focus down + end + local old_charpos, old_top = self.charpos, self.top_line_num self:downLine() + if self.charpos == old_charpos and self.top_line_num == old_top then + return false -- let FocusManager move focus down + end elseif key["End"] then self:goToEnd() elseif key["Home"] then @@ -646,10 +667,9 @@ function InputText:onKeyPress(key) self:addChars("\n") elseif key["Tab"] then self:addChars(" ") - -- as stated before, we also don't need to unfocus when there is no keyboard, one less key press to exit widgets, yay! - elseif key["Back"] and G_reader_settings:nilOrTrue("virtual_keyboard_enabled") then - if self.focused then - self:unfocus() + elseif key["Back"] then + if self.parent and not self.keyboard:isVisible() then + UIManager:close(self.parent) end else handled = false @@ -791,6 +811,7 @@ function InputText:onCloseWidget() if self.keyboard then self.keyboard:free() end + Device:stopTextInput() self:free() end diff --git a/frontend/ui/widget/virtualkeyboard.lua b/frontend/ui/widget/virtualkeyboard.lua index 49e95307d..5aac88c81 100644 --- a/frontend/ui/widget/virtualkeyboard.lua +++ b/frontend/ui/widget/virtualkeyboard.lua @@ -870,31 +870,14 @@ function VirtualKeyboard:init() if keyboard.wrapInputBox then self.uwrap_func = keyboard.wrapInputBox(self.inputbox) or self.uwrap_func end - if Device:hasDPad() then - -- hadDPad() would have FocusManager handle arrow keys strokes to navigate - -- and activate this VirtualKeyboard's touch keys (needed on non-touch Kindle). - -- If we have a keyboard, we'd prefer arrow keys (and Enter, and Del) to be - -- handled by InputText to navigate the cursor inside the text box, and to - -- add newline and delete chars. And if we are a touch device, we don't - -- need focus manager to help us navigate keys and fields. - -- So, disable all key_event handled by FocusManager - if Device:isTouchDevice() then - -- Remove all FocusManager key event handlers. - for k, _ in pairs(self.builtin_key_events) do + if Device:hasDPad() and Device:hasKeyboard() then + -- Use physical keyboard for most characters + -- For special characters not available in physical keyboard + -- Use arrow and Press keys to select in VirtualKeyboard + for k, seq in pairs(self.extra_key_events) do + if self:_isTextKeyWithoutModifier(seq) then self.key_events[k] = nil end - for k, _ in pairs(self.extra_key_events) do - self.key_events[k] = nil - end - elseif Device:hasKeyboard() then - -- Use physical keyboard for most characters - -- For special characters not available in physical keyboard - -- Use arrow and Press keys to select in VirtualKeyboard - for k, seq in pairs(self.extra_key_events) do - if self:_isTextKeyWithoutModifier(seq) then - self.key_events[k] = nil - end - end end end end @@ -939,17 +922,6 @@ end function VirtualKeyboard:onClose() UIManager:close(self) - if self.inputbox and Device:hasDPad() then - -- Let InputText handle this KeyPress "Back" event to unfocus, otherwise, another extra Back event is needed. - -- NOTE: Keep in mind InputText is a special snowflake, and implements the raw onKeyPress handler for this! - -- Also, notify another widget that actually may want to know when *we* get closed, i.e., the parent (Input*Dialog*). - -- We need to do this manually because InputText's onKeyPress handler will very likely return true, - -- stopping event propagation (c.f., the last hasDPad branch of said handler). - if self.inputbox and self.inputbox.parent and self.inputbox.parent.onKeyboardClosed then - self.inputbox.parent:onKeyboardClosed() - end - return false - end return true end @@ -970,23 +942,12 @@ end function VirtualKeyboard:onShow() self:_refresh(true) self.visible = true - Device:startTextInput() return true end function VirtualKeyboard:onCloseWidget() self:_refresh(true) self.visible = false - -- NOTE: This effectively stops SDL text input when a keyboard is hidden (... but navigational stuff still works). - -- If you instead wanted it to be enabled as long as an input dialog is displayed, regardless of VK's state, - -- this could be moved to InputDialog's onShow/onCloseWidget handlers (but, it would allow input on unfocused fields). - -- NOTE: But something more complex, possibly based on an in-class ref count would have to be implemented in order to be able to deal - -- with multiple InputDialogs being shown and closed in asymmetric fashion... Ugh. - -- NOTE: You would also have to deal with the fact that, once InputText loses focus, - -- it will stop dealing with key events because it wouldn't know where to send them when there are multiple live instances of it, - -- specifically because, given how we propagate events, the key event will go to whichever inputtext comes earlier in the container's array... - -- c.f., 2ccf7601fe1cbd9794aea0be754ea4166b9767d7 in #12361 and the comments surrounding it ;). - Device:stopTextInput() end function VirtualKeyboard:lockVisibility(toggle)