got rid of the interval polling architecture, also displaying mouse keycodes in the status bar

xkb
sezanzeb 4 years ago committed by sezanzeb
parent 23a08f4c20
commit 8c85bf5d39

@ -266,7 +266,7 @@
<property name="title" translatable="yes">Key Mapper</property> <property name="title" translatable="yes">Key Mapper</property>
<property name="icon_name">mouse</property> <property name="icon_name">mouse</property>
<signal name="delete-event" handler="on_close" swapped="no"/> <signal name="delete-event" handler="on_close" swapped="no"/>
<signal name="key-press-event" handler="on_window_key_press_event" swapped="no"/> <signal name="event" handler="on_window_event" swapped="no"/>
<child> <child>
<object class="GtkBox" id="wrapper"> <object class="GtkBox" id="wrapper">
<property name="visible">True</property> <property name="visible">True</property>

@ -59,23 +59,10 @@ class Row(Gtk.ListBoxRow):
character = self.character_input.get_text() character = self.character_input.get_text()
return character if character else None return character if character else None
def start_watching_keycodes(self, *args): def set_new_keycode(self, new_keycode):
"""Start to periodically check if a keycode has been pressed.
This also includes e.g. middle mouse buttons, as opposed to
get_keycode from gdk events.
"""
keycode_reader.clear()
def iterate():
self.check_newest_keycode()
return self.keycode.is_focus() and self.window.window.is_active()
GLib.timeout_add(1000 / 30, iterate)
def check_newest_keycode(self):
"""Check if a keycode has been pressed and if so, display it.""" """Check if a keycode has been pressed and if so, display it."""
new_keycode = keycode_reader.read() # the newest_keycode is populated since the ui regularly polls it
# in order to display it in the status bar.
previous_keycode = self.get_keycode() previous_keycode = self.get_keycode()
character = self.get_character() character = self.get_character()
@ -108,7 +95,7 @@ class Row(Gtk.ListBoxRow):
return return
# else, the keycode has changed, the character is set, all good # else, the keycode has changed, the character is set, all good
custom_mapping.change(previous_keycode, new_keycode, character) custom_mapping.change(new_keycode, character, previous_keycode)
def highlight(self): def highlight(self):
"""Mark this row as changed.""" """Mark this row as changed."""
@ -150,12 +137,6 @@ class Row(Gtk.ListBoxRow):
if keycode is not None: if keycode is not None:
keycode_input.set_label(str(keycode)) keycode_input.set_label(str(keycode))
# to capture regular keyboard keys or extra-mouse keys
keycode_input.connect(
'focus-in-event',
self.start_watching_keycodes
)
# make the togglebutton go back to its normal state when doing # make the togglebutton go back to its normal state when doing
# something else in the UI # something else in the UI
keycode_input.connect( keycode_input.connect(

@ -193,12 +193,30 @@ class Window:
key_list = self.get('key_list') key_list = self.get('key_list')
key_list.forall(lambda row: row.unhighlight()) key_list.forall(lambda row: row.unhighlight())
def on_window_key_press_event(self, window, event): def on_window_event(self, button, event):
"""Write down the pressed key on the UI. """Write down the pressed key in the UI.
Helps to understand what the numbers in the mapping are about. Works with any mouse and keyboard event.
""" """
self.get('keycode').set_text(str(event.get_keycode()[1])) # to capture regular keyboard keys or extra-mouse keys
keycode = keycode_reader.read()
if keycode is None:
return
if keycode == 280:
# disable mapping the left mouse button because it would break
# the mouse. Also it is emitted right when focusing the row
# which breaks the current workflow.
return
self.get('keycode').set_text(str(keycode))
# inform the currently selected row about the new keycode
focused = self.window.get_focus()
row = focused.get_parent().get_parent()
if isinstance(focused, Gtk.ToggleButton) and isinstance(row, Row):
row.set_new_keycode(keycode)
def on_apply_system_layout_clicked(self, button): def on_apply_system_layout_clicked(self, button):
"""Load the mapping.""" """Load the mapping."""
@ -292,6 +310,7 @@ class Window:
try: try:
new_preset = get_available_preset_name(self.selected_device) new_preset = get_available_preset_name(self.selected_device)
custom_mapping.empty()
custom_mapping.save(self.selected_device, new_preset) custom_mapping.save(self.selected_device, new_preset)
self.get('preset_selection').append(new_preset, new_preset) self.get('preset_selection').append(new_preset, new_preset)
self.get('preset_selection').set_active_id(new_preset) self.get('preset_selection').set_active_id(new_preset)

@ -66,7 +66,7 @@ class KeycodeReader:
] ]
def read(self): def read(self):
"""Get the newest key or None if none was pressed.""" """Get the newest keycode or None if none was pressed."""
newest_keycode = None newest_keycode = None
for virtual_device in self.virtual_devices: for virtual_device in self.virtual_devices:
while True: while True:

@ -128,8 +128,7 @@ class Integration(unittest.TestCase):
Event(evdev.events.EV_KEY, keycode - 8, 1) Event(evdev.events.EV_KEY, keycode - 8, 1)
] ]
time.sleep(0.1) self.window.on_window_event(None, None)
gtk_iteration()
self.assertEqual(int(row.keycode.get_label()), keycode) self.assertEqual(int(row.keycode.get_label()), keycode)

Loading…
Cancel
Save