diff --git a/frontend/device/input.lua b/frontend/device/input.lua index 3fbbfa803..9660280d8 100644 --- a/frontend/device/input.lua +++ b/frontend/device/input.lua @@ -773,28 +773,31 @@ function Input:handleKeyBoardEv(ev) if ev.value == KEY_PRESS then return Event:new("KeyPress", key) elseif ev.value == KEY_REPEAT then - -- NOTE: We only care about repeat events from the pageturn buttons... + -- NOTE: We only care about repeat events from the page-turn buttons and cursor keys... -- And we *definitely* don't want to flood the Event queue with useless SleepCover repeats! - if keycode == "LPgBack" - or keycode == "RPgBack" - or keycode == "LPgFwd" - or keycode == "RPgFwd" then + if keycode == "Up" or keycode == "Down" or keycode == "Left" or keycode == "Right" + or keycode == "RPgBack" or keycode == "RPgFwd" or keycode == "LPgBack" or keycode == "LPgFwd" then --- @fixme Crappy event staggering! -- -- The Forma & co repeats every 80ms after a 400ms delay, and 500ms roughly corresponds to a flashing update, -- so stuff is usually in sync when you release the key. - -- Obvious downside is that this ends up slower than just mashing the key. -- -- A better approach would be an onKeyRelease handler that flushes the Event queue... - self.repeat_count = self.repeat_count + 1 - if self.repeat_count == 1 then + local rep_period = self.device.key_repeat and self.device.key_repeat[C.REP_PERIOD] or 80 + local now = time.now() + if not self.last_repeat_time then + self.last_repeat_time = now return Event:new("KeyRepeat", key) - elseif self.repeat_count >= 6 then - self.repeat_count = 0 + else + local time_diff = time.to_ms(now - self.last_repeat_time) + if time_diff >= rep_period then + self.last_repeat_time = now + return Event:new("KeyRepeat", key) + end end end elseif ev.value == KEY_RELEASE then - self.repeat_count = 0 + self.last_repeat_time = nil return Event:new("KeyRelease", key) end end diff --git a/frontend/device/kindle/device.lua b/frontend/device/kindle/device.lua index ee6e87348..cee2fc8dd 100644 --- a/frontend/device/kindle/device.lua +++ b/frontend/device/kindle/device.lua @@ -543,6 +543,29 @@ function Kindle:otaModel() return model, "ota" end +function Kindle:toggleKeyRepeat(toggle) + if toggle == true then + self.key_repeat[C.REP_DELAY] = 400 + self.key_repeat[C.REP_PERIOD] = 120 + + -- We can't easily clear existing hooks, but we can overwrite the eventAdjustHook + -- with the default empty implementation to effectively remove previous hooks + local Input = require("device/input") + self.input.eventAdjustHook = Input.eventAdjustHook + else + self.key_repeat[C.REP_DELAY] = 0 + self.key_repeat[C.REP_PERIOD] = 0 + + -- Register an event hook that filters out KEY_REPEAT events + self.input:registerEventAdjustHook(function(this, ev) + if ev.type == C.EV_KEY and ev.value == 2 then -- KEY_REPEAT = 2 + ev.value = -1 -- Set to an invalid value that will be ignored + end + end) + end + return true +end + function Kindle:init() -- Check if the device supports deep sleep/quick boot if lfs.attributes("/sys/devices/platform/falconblk/uevent", "mode") == "file" then @@ -586,6 +609,18 @@ function Kindle:init() end end + if self:hasDPad() then + self.canKeyRepeat = yes + self.key_repeat = ffi.new("unsigned int[?]", C.REP_CNT) + if G_reader_settings:isTrue("input_no_key_repeat") then + self.key_repeat[C.REP_DELAY] = 0 + self.key_repeat[C.REP_PERIOD] = 0 + else + self.key_repeat[C.REP_DELAY] = 400 + self.key_repeat[C.REP_PERIOD] = 120 + end + end + Generic.init(self) end diff --git a/frontend/ui/elements/physical_buttons.lua b/frontend/ui/elements/physical_buttons.lua index 6abb50301..312e3ec90 100644 --- a/frontend/ui/elements/physical_buttons.lua +++ b/frontend/ui/elements/physical_buttons.lua @@ -62,7 +62,7 @@ if Device:hasDPad() and Device:useDPadAsActionKeys() then end if Device:canKeyRepeat() then - table.insert(PhysicalButtons.sub_item_table, { + table.insert(PhysicalButtons.sub_item_table, 1, { text = _("Disable key repeat"), help_text = _("Useful if you don't like the behavior or if your device has faulty switches"), checked_func = function() diff --git a/frontend/ui/widget/focusmanager.lua b/frontend/ui/widget/focusmanager.lua index 9460ef367..2564a2c96 100644 --- a/frontend/ui/widget/focusmanager.lua +++ b/frontend/ui/widget/focusmanager.lua @@ -525,5 +525,6 @@ function FocusManager:onKeyPress(key) end return InputContainer.onKeyPress(self, key) end +FocusManager.onKeyRepeat = FocusManager.onKeyPress return FocusManager