refactoring of keyboard key handling

This commit is contained in:
DYefremov
2026-02-14 12:58:13 +03:00
parent a47a7417c2
commit a5412cd2b3
12 changed files with 63 additions and 63 deletions

View File

@@ -2,7 +2,7 @@
#
# The MIT License (MIT)
#
# Copyright (c) 2018-2024 Dmitriy Yefremov
# Copyright (c) 2018-2026 Dmitriy Yefremov
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
@@ -223,11 +223,11 @@ class BackupDialog:
self._settings.add("backup_tool_window_size", window.get_size())
def on_key_release(self, view, event):
""" Handling keystrokes """
key_code = event.hardware_keycode
if not KeyboardKey.value_exist(key_code):
""" Handling keystrokes. """
key = KeyboardKey(event.hardware_keycode)
if key is KeyboardKey.UNDEFINED:
return
key = KeyboardKey(key_code)
ctrl = event.state & MOD_MASK
if key is KeyboardKey.DELETE:

View File

@@ -1217,10 +1217,10 @@ class EpgDialog:
def on_key_press(self, view, event):
""" Handling keystrokes """
key_code = event.hardware_keycode
if not KeyboardKey.value_exist(key_code):
key = KeyboardKey(event.hardware_keycode)
if key is KeyboardKey.UNDEFINED:
return
key = KeyboardKey(key_code)
ctrl = event.state & Gdk.ModifierType.CONTROL_MASK
if ctrl and key is KeyboardKey.C:

View File

@@ -2,7 +2,7 @@
#
# The MIT License (MIT)
#
# Copyright (c) 2018-2025 Dmitriy Yefremov
# Copyright (c) 2018-2026 Dmitriy Yefremov
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
@@ -856,11 +856,10 @@ class FtpClientBox(Gtk.HBox):
self._settings.ftp_bookmarks = [r[0] for r in self._bookmark_model]
def on_view_key_press(self, view, event):
key_code = event.hardware_keycode
if not KeyboardKey.value_exist(key_code):
key = KeyboardKey(event.hardware_keycode)
if key is KeyboardKey.UNDEFINED:
return
key = KeyboardKey(key_code)
ctrl = event.state & MOD_MASK
if key is KeyboardKey.F7:

View File

@@ -426,10 +426,9 @@ class ImportDialog:
def on_key_press(self, view, event):
""" Handling keystrokes """
key_code = event.hardware_keycode
if not KeyboardKey.value_exist(key_code):
key = KeyboardKey(event.hardware_keycode)
if key is KeyboardKey.UNDEFINED:
return
key = KeyboardKey(key_code)
if key is KeyboardKey.SPACE:
model = view.get_model()

View File

@@ -2,7 +2,7 @@
#
# The MIT License (MIT)
#
# Copyright (c) 2018-2025 Dmitriy Yefremov
# Copyright (c) 2018-2026 Dmitriy Yefremov
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
@@ -1331,10 +1331,9 @@ class YtListImportDialog:
view.get_model().foreach(lambda mod, path, itr: mod.set_value(itr, 2, select))
def on_key_press(self, view, event):
key_code = event.hardware_keycode
if not KeyboardKey.value_exist(key_code):
key = KeyboardKey(event.hardware_keycode)
if key is KeyboardKey.UNDEFINED:
return
key = KeyboardKey(key_code)
if key is KeyboardKey.SPACE:
path, column = view.get_cursor()

View File

@@ -2975,11 +2975,10 @@ class Application(Gtk.Application):
def on_tree_view_key_press(self, view, event):
""" Handling keystrokes on press """
key_code = event.hardware_keycode
if not KeyboardKey.value_exist(key_code):
return
key = KeyboardKey(event.hardware_keycode)
if key is KeyboardKey.UNDEFINED:
return False
key = KeyboardKey(key_code)
ctrl = event.state & MOD_MASK
if key is KeyboardKey.F:
if ctrl:
@@ -3028,11 +3027,10 @@ class Application(Gtk.Application):
def on_tree_view_key_release(self, view, event):
""" Handling keystrokes on release """
key_code = event.hardware_keycode
if not KeyboardKey.value_exist(key_code):
key = KeyboardKey(event.hardware_keycode)
if key is KeyboardKey.UNDEFINED:
return
key = KeyboardKey(key_code)
ctrl = event.state & MOD_MASK
shift = event.state & Gdk.ModifierType.SHIFT_MASK
model_name, model = get_model_data(view)
@@ -4539,11 +4537,10 @@ class Application(Gtk.Application):
self.emit("fav-changed", srv)
def on_alt_view_key_press(self, view, event):
key_code = event.hardware_keycode
if not KeyboardKey.value_exist(key_code):
key = KeyboardKey(event.hardware_keycode)
if key is KeyboardKey.UNDEFINED:
return
key = KeyboardKey(key_code)
ctrl = event.state & MOD_MASK
if ctrl and key == KeyboardKey.V:

View File

@@ -2,7 +2,7 @@
#
# The MIT License (MIT)
#
# Copyright (c) 2018-2024 Dmitriy Yefremov
# Copyright (c) 2018-2026 Dmitriy Yefremov
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
@@ -976,11 +976,10 @@ class PiconManager(Gtk.Box):
return True
def on_tree_view_key_press(self, view, event):
key_code = event.hardware_keycode
if not KeyboardKey.value_exist(key_code):
key = KeyboardKey(event.hardware_keycode)
if key is KeyboardKey.UNDEFINED:
return
key = KeyboardKey(key_code)
if key is KeyboardKey.DELETE:
self.on_local_remove(view)

View File

@@ -304,11 +304,10 @@ class RecordingsTool(Gtk.Box):
self._filter_entry.grab_focus() if button.get_active() else self._filter_entry.set_text("")
def on_recordings_key_press(self, view, event):
key_code = event.hardware_keycode
if not KeyboardKey.value_exist(key_code):
key = KeyboardKey(event.hardware_keycode)
if key is KeyboardKey.UNDEFINED:
return
key = KeyboardKey(key_code)
if key is KeyboardKey.DELETE:
self.on_recording_remove()

View File

@@ -2,7 +2,7 @@
#
# The MIT License (MIT)
#
# Copyright (c) 2018-2025 Dmitriy Yefremov
# Copyright (c) 2018-2026 Dmitriy Yefremov
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
@@ -129,11 +129,10 @@ class TelnetClient(Gtk.Box):
self.do_command()
return True
key_code = event.hardware_keycode
if not KeyboardKey.value_exist(key_code):
return None
key = KeyboardKey(event.hardware_keycode)
if key is KeyboardKey.UNDEFINED:
return False
key = KeyboardKey(key_code)
ctrl = event.state & MOD_MASK
if ctrl and key is KeyboardKey.C:
if self._tn and self._tn.sock:

View File

@@ -2,7 +2,7 @@
#
# The MIT License (MIT)
#
# Copyright (c) 2018-2024 Dmitriy Yefremov
# Copyright (c) 2018-2026 Dmitriy Yefremov
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
@@ -460,11 +460,10 @@ class TimerTool(Gtk.Box):
on_popup_menu(menu, event)
def on_timers_key_release(self, view, event):
key_code = event.hardware_keycode
if not KeyboardKey.value_exist(key_code):
key = KeyboardKey(event.hardware_keycode)
if key is KeyboardKey.UNDEFINED:
return
key = KeyboardKey(key_code)
ctrl = event.state & MOD_MASK
if key is KeyboardKey.DELETE:

View File

@@ -2,7 +2,7 @@
#
# The MIT License (MIT)
#
# Copyright (c) 2018-2025 Dmitriy Yefremov
# Copyright (c) 2018-2026 Dmitriy Yefremov
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
@@ -290,14 +290,8 @@ class Column(IntEnum):
# *************** Keyboard keys *************** #
class BaseKeyboardKey(Enum):
@classmethod
def value_exist(cls, value):
return value in (val.value for val in cls.__members__.values())
if IS_LINUX:
class KeyboardKey(BaseKeyboardKey):
class KeyboardKey(IntEnum):
""" The raw(hardware) codes [Linux] of the keyboard keys. """
E = 26
R = 27
@@ -335,8 +329,14 @@ if IS_LINUX:
PAGE_UP_KP = 81
PAGE_DOWN_KP = 89
UNDEFINED = -1
@classmethod
def _missing_(cls, value):
return cls.UNDEFINED
elif IS_DARWIN:
class KeyboardKey(BaseKeyboardKey):
class KeyboardKey(IntEnum):
""" The raw(hardware) codes [macOS] of the keyboard keys. """
F = 3
E = 14
@@ -376,8 +376,14 @@ elif IS_DARWIN:
PAGE_UP_KP = -1
PAGE_DOWN_KP = -1
UNDEFINED = -1
@classmethod
def _missing_(cls, value):
return cls.UNDEFINED
else:
class KeyboardKey(BaseKeyboardKey):
class KeyboardKey(IntEnum):
""" The raw(hardware) codes [Windows] of the keyboard keys. """
E = 69
R = 82
@@ -415,6 +421,12 @@ else:
PAGE_UP_KP = -1
PAGE_DOWN_KP = -1
UNDEFINED = -1
@classmethod
def _missing_(cls, value):
return cls.UNDEFINED
# Keys for move in lists. KEY_KP_(NAME) for laptop!
MOVE_KEYS = {KeyboardKey.UP, KeyboardKey.PAGE_UP,
KeyboardKey.DOWN, KeyboardKey.PAGE_DOWN,

View File

@@ -2,7 +2,7 @@
#
# The MIT License (MIT)
#
# Copyright (c) 2018-2025 Dmitriy Yefremov
# Copyright (c) 2018-2026 Dmitriy Yefremov
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
@@ -310,11 +310,10 @@ class SatellitesTool(Gtk.Box):
def on_key_press(self, view, event):
""" Handling keystrokes. """
key_code = event.hardware_keycode
if not KeyboardKey.value_exist(key_code):
key = KeyboardKey(event.hardware_keycode)
if key is KeyboardKey.UNDEFINED:
return
key = KeyboardKey(key_code)
ctrl = event.state & MOD_MASK
if key is KeyboardKey.DELETE:
@@ -329,12 +328,11 @@ class SatellitesTool(Gtk.Box):
view.do_unselect_all(view)
def on_tr_key_press(self, view, event):
""" Handling transponder view keystrokes. """
key_code = event.hardware_keycode
if not KeyboardKey.value_exist(key_code):
""" Handling transponder view keystrokes. """
key = KeyboardKey(event.hardware_keycode)
if key is KeyboardKey.UNDEFINED:
return
key = KeyboardKey(key_code)
ctrl = event.state & MOD_MASK
if key is KeyboardKey.DELETE: