Fixed button pressed timer logic to not duplicate enteries sometimes

This commit is contained in:
2025-12-27 18:38:04 -06:00
parent 328dfaa594
commit 8596d5cb69
2 changed files with 66 additions and 37 deletions

View File

@@ -46,8 +46,8 @@ class Backspace_Key(Key):
super(Backspace_Key, self).__init__("Backspace", "Backspace", iscontrol=True) super(Backspace_Key, self).__init__("Backspace", "Backspace", iscontrol=True)
def _setup_signals(self): def _setup_signals(self):
self.connect("button-press-event", self._do_press_special_key_repeater) self.connect("button-press-event", self._do_press_special_key)
self.connect("button-release-event", self._do_release) self.connect("button-release-event", self._do_release_special_key)
class Emoji_Key(Key): class Emoji_Key(Key):
def __init__(self, emoji_popover): def __init__(self, emoji_popover):
@@ -97,8 +97,8 @@ class Enter_Key(Key):
super()._setup_styling() super()._setup_styling()
def _setup_signals(self): def _setup_signals(self):
self.connect("button-press-event", self._do_press_special_key_repeater) self.connect("button-press-event", self._do_press_special_key)
self.connect("button-release-event", self._do_release) self.connect("button-release-event", self._do_release_special_key)
############################# Bottom_Key_Row Keys ############################# ############################# Bottom_Key_Row Keys #############################
@@ -115,8 +115,8 @@ class Space_Key(Key):
super(Space_Key, self).__init__("Space", "Space", iscontrol=True) super(Space_Key, self).__init__("Space", "Space", iscontrol=True)
def _setup_signals(self): def _setup_signals(self):
self.connect("button-press-event", self._do_press_special_key_repeater) self.connect("button-press-event", self._do_press_special_key)
self.connect("button-release-event", self._do_release) self.connect("button-release-event", self._do_release_special_key)
class AT_Key(Key): class AT_Key(Key):
def __init__(self): def __init__(self):
@@ -134,16 +134,16 @@ class Tab_Key(Key):
super(Tab_Key, self).__init__("Tab", "Tab", iscontrol=True) super(Tab_Key, self).__init__("Tab", "Tab", iscontrol=True)
def _setup_signals(self): def _setup_signals(self):
self.connect("button-press-event", self._do_press_special_key_repeater) self.connect("button-press-event", self._do_press_special_key)
self.connect("button-release-event", self._do_release) self.connect("button-release-event", self._do_release_special_key)
class Del_Key(Key): class Del_Key(Key):
def __init__(self): def __init__(self):
super(Del_Key, self).__init__("Del", "Del", iscontrol=True) super(Del_Key, self).__init__("Del", "Del", iscontrol=True)
def _setup_signals(self): def _setup_signals(self):
self.connect("button-press-event", self._do_press_special_key_repeater) self.connect("button-press-event", self._do_press_special_key)
self.connect("button-release-event", self._do_release) self.connect("button-release-event", self._do_release_special_key)
class Ctrl_Key(Key): class Ctrl_Key(Key):
def __init__(self): def __init__(self):
@@ -178,29 +178,29 @@ class Up_Key(Key):
super(Up_Key, self).__init__("Up", "Up", iscontrol=True) super(Up_Key, self).__init__("Up", "Up", iscontrol=True)
def _setup_signals(self): def _setup_signals(self):
self.connect("button-press-event", self._do_press_special_key_repeater) self.connect("button-press-event", self._do_press_special_key)
self.connect("button-release-event", self._do_release) self.connect("button-release-event", self._do_release_special_key)
class Down_Key(Key): class Down_Key(Key):
def __init__(self): def __init__(self):
super(Down_Key, self).__init__("Down", "Down", iscontrol=True) super(Down_Key, self).__init__("Down", "Down", iscontrol=True)
def _setup_signals(self): def _setup_signals(self):
self.connect("button-press-event", self._do_press_special_key_repeater) self.connect("button-press-event", self._do_press_special_key)
self.connect("button-release-event", self._do_release) self.connect("button-release-event", self._do_release_special_key)
class Left_Key(Key): class Left_Key(Key):
def __init__(self): def __init__(self):
super(Left_Key, self).__init__("Left", "Left", iscontrol=True) super(Left_Key, self).__init__("Left", "Left", iscontrol=True)
def _setup_signals(self): def _setup_signals(self):
self.connect("button-press-event", self._do_press_special_key_repeater) self.connect("button-press-event", self._do_press_special_key)
self.connect("button-release-event", self._do_release) self.connect("button-release-event", self._do_release_special_key)
class Right_Key(Key): class Right_Key(Key):
def __init__(self): def __init__(self):
super(Right_Key, self).__init__("Right", "Right", iscontrol=True) super(Right_Key, self).__init__("Right", "Right", iscontrol=True)
def _setup_signals(self): def _setup_signals(self):
self.connect("button-press-event", self._do_press_special_key_repeater) self.connect("button-press-event", self._do_press_special_key)
self.connect("button-release-event", self._do_release) self.connect("button-release-event", self._do_release_special_key)

View File

@@ -52,15 +52,33 @@ class Key(Gtk.Button or Gtk.ToggleButton):
self._alt_symbol = parts[-1] self._alt_symbol = parts[-1]
def _do_press(self, widget = None, eve = None): def _do_press(self, widget = None, eve = None):
if self.timer_id: if not self.timer_id:
self.did_loop_type = False
self.timer_id = GLib.timeout_add(500, self._do_wait_loop_type)
def _do_release(self, widget = None, eve = None):
if self.did_loop_type:
GLib.source_remove(self.timer_id)
self.timer_id = None
self.did_loop_type = False
return
self._do_type()
GLib.source_remove(self.timer_id) GLib.source_remove(self.timer_id)
self.timer_id = None self.timer_id = None
self._do_type() def _do_wait_loop_type(self):
if not self.timer_id: return False
self.did_loop_type = True
self.timer_id = GLib.timeout_add(200, self._do_type) self.timer_id = GLib.timeout_add(200, self._do_type)
return False
def _do_type(self):
if not self.timer_id: return False
def _do_type(self, widget = None, eve = None):
key = self.get_label().strip() key = self.get_label().strip()
if self._is_emoji: if self._is_emoji:
typwriter.set_clipboard_data(key, "utf-16") typwriter.set_clipboard_data(key, "utf-16")
@@ -74,36 +92,47 @@ class Key(Gtk.Button or Gtk.ToggleButton):
typwriter.isShiftOn = self.isShiftOn typwriter.isShiftOn = self.isShiftOn
typwriter.type(self._alt_symbol) typwriter.type(self._alt_symbol)
typwriter.isShiftOn = False typwriter.isShiftOn = False
else:
typwriter.type(key)
return True return True
def _do_release(self, widget = None, eve = None): typwriter.type(key)
if not self.timer_id: return return True
GLib.source_remove(self.timer_id)
self.timer_id = None
def _do_press_special_key(self, widget = None, eve = None):
def _do_press_special_key(self, widget = None): if not self.timer_id:
self._do_type_special_key(widget) self.did_loop_type = False
self.timer_id = GLib.timeout_add(500, self._do_wait_loop_special_type)
def _do_press_special_key_repeater(self, widget = None, eve = None):
if self.timer_id: def _do_release_special_key(self, widget = None, eve = None):
if self.did_loop_type:
GLib.source_remove(self.timer_id) GLib.source_remove(self.timer_id)
self.timer_id = None self.timer_id = None
self.did_loop_type = False
return
self._do_type_special_key() self._do_type_special_key()
self.timer_id = GLib.timeout_add(200, self._do_type_special_key) GLib.source_remove(self.timer_id)
self.timer_id = None
def _do_wait_loop_special_type(self):
if not self.timer_id: return False
self.did_loop_type = True
self.timer_id = GLib.timeout_add(200, self._do_type_special_key)
return False
def _do_type_special_key(self):
if not self.timer_id: return False
def _do_type_special_key(self, widget = None):
key = self.get_label() key = self.get_label()
if key in ["Ctrl", "Shift", "Alt"]: if key in ["Ctrl", "Shift", "Alt"]:
ctx = widget.get_style_context() ctx = self.get_style_context()
ctx.remove_class("toggled_bttn") if ctx.has_class("toggled_bttn") else ctx.add_class("toggled_bttn") ctx.remove_class("toggled_bttn") if ctx.has_class("toggled_bttn") else ctx.add_class("toggled_bttn")
typwriter.press_special_keys(key) typwriter.press_special_keys(key)
return True return True
def toggle_symbol_keys(self, widget = None, eve = None): def toggle_symbol_keys(self, widget = None, eve = None):
if not self.iscontrol: if not self.iscontrol:
self._is_symbol = not self._is_symbol self._is_symbol = not self._is_symbol