From 2db294d0b77a850b6a4f135065ce46ab4532bf73 Mon Sep 17 00:00:00 2001 From: NiLuJe Date: Mon, 31 Oct 2022 21:22:26 +0100 Subject: [PATCH] ExternalKeyboard: Debounce the Connected/Disconnected events Until we find a way to fine-tune the evdev device parsing, we'll often have at least two devices per keyboard, but we only need a single event ;). --- plugins/externalkeyboard.koplugin/main.lua | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/plugins/externalkeyboard.koplugin/main.lua b/plugins/externalkeyboard.koplugin/main.lua index 059bcb8cc..04fb972ca 100644 --- a/plugins/externalkeyboard.koplugin/main.lua +++ b/plugins/externalkeyboard.koplugin/main.lua @@ -261,10 +261,15 @@ function ExternalKeyboard:_onEvdevInputRemove(evdev) -- There's a two-pronged approach here: -- * Call a static class method to modify the class state for future instances of said class -- * Broadcast an Event so that all currently displayed widgets update their own state. - -- This must come after, because widgets *may* rely on static class members. + -- This must come after, because widgets *may* rely on static class members, + -- we have no guarantee about Event delivery order. + self:_broadcastDisconnected() +end + +ExternalKeyboard._broadcastDisconnected = UIManager:debounce(0.5, false, function() InputText.initInputEvents() UIManager:broadcastEvent(Event:new("PhysicalKeyboardDisconnected")) -end +end) -- The keyboard events with the same key codes would override the original events. -- That may cause embedded buttons to lose their original function and produce letters, @@ -342,9 +347,13 @@ function ExternalKeyboard:setupKeyboard(event_path) timeout = 1, }) end + self:_broadcastConnected() +end + +ExternalKeyboard._broadcastConnected = UIManager:debounce(0.5, false, function() InputText.initInputEvents() UIManager:broadcastEvent(Event:new("PhysicalKeyboardConnected")) -end +end) function ExternalKeyboard:showHelp() UIManager:show(InfoMessage:new {