From ccc49036aa169f7630abb8e5a9171f2069a1bbbd Mon Sep 17 00:00:00 2001 From: John Beard Date: Sun, 23 Jan 2022 18:40:35 +0100 Subject: [PATCH] Handle arrow keys and enter in emulator text fields --- frontend/ui/widget/inputtext.lua | 47 ++++++++++++++++++-------- frontend/ui/widget/virtualkeyboard.lua | 14 ++++++++ 2 files changed, 46 insertions(+), 15 deletions(-) diff --git a/frontend/ui/widget/inputtext.lua b/frontend/ui/widget/inputtext.lua index 6cf745ba1..e6ca06df2 100644 --- a/frontend/ui/widget/inputtext.lua +++ b/frontend/ui/widget/inputtext.lua @@ -543,30 +543,47 @@ end -- is shown. Mostly likely to be in the emulator, but could be Android + BT -- keyboard, or a "coder's keyboard" Android input method. function InputText:onKeyPress(key) - if key["Backspace"] then - self:delChar() - elseif key["Del"] then - self:rightChar() - self:delChar() - elseif key["Left"] then - self:leftChar() - elseif key["Right"] then - self:rightChar() - elseif key["End"] then - self:goToEnd() - elseif key["Home"] then - self:goToHome() + + local handled = true + + if not key["Ctrl"] and not key["Shift"] and not key["Alt"] then + if key["Backspace"] then + self:delChar() + elseif key["Del"] then + self:rightChar() + self:delChar() + elseif key["Left"] then + self:leftChar() + elseif key["Right"] then + self:rightChar() + elseif key["Up"] then + self:upLine() + elseif key["Down"] then + self:downLine() + elseif key["End"] then + self:goToEnd() + elseif key["Home"] then + self:goToHome() + elseif key["Press"] then + self:addChars("\n") + elseif key["Tab"] then + self:addChars(" ") + else + handled = false + end elseif key["Ctrl"] and not key["Shift"] and not key["Alt"] then if key["U"] then self:delToStartOfLine() elseif key["H"] then self:delChar() + else + handled = false end else - return false + handled = false end - return true + return handled end -- Handle text coming directly as text from the Device layer (eg. soft keyboard diff --git a/frontend/ui/widget/virtualkeyboard.lua b/frontend/ui/widget/virtualkeyboard.lua index 0d0d25d17..79625a670 100644 --- a/frontend/ui/widget/virtualkeyboard.lua +++ b/frontend/ui/widget/virtualkeyboard.lua @@ -800,6 +800,20 @@ function VirtualKeyboard:init() if keyboard.wrapInputBox then self.uwrap_func = keyboard.wrapInputBox(self.inputbox) or self.uwrap_func end + if Device:hasDPad() and Device:hasKeyboard() and Device:isTouchDevice() 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 + self.key_events.FocusLeft = nil + self.key_events.FocusRight = nil + self.key_events.FocusUp = nil + self.key_events.FocusDown = nil + self.key_events.PressKey = nil -- added above + end end function VirtualKeyboard:getKeyboardLayout()