From cbb0fe2fad90818c959fd46976e0ee382f2b2276 Mon Sep 17 00:00:00 2001 From: Franco Battista Date: Mon, 6 May 2024 22:25:49 -0300 Subject: [PATCH 1/2] Keyboard shortcuts added: -Open settings, -Open locations, -Refresh weather, using keyboard library --- src/mousam.py | 37 ++++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/src/mousam.py b/src/mousam.py index 26d51c2..7cd7cdf 100644 --- a/src/mousam.py +++ b/src/mousam.py @@ -2,10 +2,11 @@ import time import threading import gettext +import keyboard gi.require_version("Gtk", "4.0") gi.require_version("Adw", "1") -from gi.repository import Gtk, Adw, Gio +from gi.repository import Gtk, Adw, Gio, GLib from gettext import gettext as _, pgettext as C_ @@ -114,6 +115,11 @@ def __init__(self, *args, **kwargs): thread = threading.Thread(target=self._load_weather_data, name="load_data") thread.start() + #Set key listeners + keyboard_thread = threading.Thread(target=self.listen_to_keyboard_events) + keyboard_thread.start() + + # =========== Create Loader ============= def show_loader(self): # Loader container @@ -358,3 +364,32 @@ def _on_preferences_clicked(self, *args, **kwargs): def _on_locations_clicked(self, *args, **kwargs): adw_preferences_window = WeatherLocations(self.main_window) adw_preferences_window.show() + + + #Exec shortcut method + def on_hotkey_pressed(self,event): + match event: + #Create a new thread if shortcut need a new window + case "location": + GLib.idle_add(self._on_locations_clicked) + case "weather": + self._refresh_weather(None) + case "preferences": + GLib.idle_add(self._on_preferences_clicked) + case _: + return "" + + + #Def shortcuts key listeners + def listen_to_keyboard_events(self): + + #Listeners + keyboard.add_hotkey('ctrl+l', lambda: self.on_hotkey_pressed("location")) + keyboard.add_hotkey('ctrl+r', lambda: self.on_hotkey_pressed("weather")) + keyboard.add_hotkey('ctrl+comma', lambda: self.on_hotkey_pressed("preferences")) + + #Wait empty key. None key stop de listener + keyboard.wait("") + + #Free hooks + keyboard.unhook_all() From f52ad0b0e8920ec8346051a3d584eba62fa0683d Mon Sep 17 00:00:00 2001 From: Franco Battista Date: Tue, 7 May 2024 19:06:20 -0300 Subject: [PATCH 2/2] Refactor shortcuts using GTK --- src/mousam.py | 38 +++++++++++--------------------------- 1 file changed, 11 insertions(+), 27 deletions(-) diff --git a/src/mousam.py b/src/mousam.py index 7cd7cdf..d80a0ad 100644 --- a/src/mousam.py +++ b/src/mousam.py @@ -2,11 +2,10 @@ import time import threading import gettext -import keyboard gi.require_version("Gtk", "4.0") gi.require_version("Adw", "1") -from gi.repository import Gtk, Adw, Gio, GLib +from gi.repository import Gtk, Adw, Gio, GLib, Gdk from gettext import gettext as _, pgettext as C_ @@ -116,8 +115,9 @@ def __init__(self, *args, **kwargs): thread.start() #Set key listeners - keyboard_thread = threading.Thread(target=self.listen_to_keyboard_events) - keyboard_thread.start() + keycont = Gtk.EventControllerKey() + keycont.connect('key-pressed', self.on_key_press) + self.add_controller(keycont) # =========== Create Loader ============= @@ -366,30 +366,14 @@ def _on_locations_clicked(self, *args, **kwargs): adw_preferences_window.show() - #Exec shortcut method - def on_hotkey_pressed(self,event): - match event: - #Create a new thread if shortcut need a new window - case "location": - GLib.idle_add(self._on_locations_clicked) - case "weather": + #Def shortcuts key listeners + def on_key_press(self, key_controller, keyval, keycode, state,*args): + if state & Gdk.ModifierType.CONTROL_MASK: + if keyval == Gdk.KEY_r: self._refresh_weather(None) - case "preferences": + if keyval == Gdk.KEY_l: + GLib.idle_add(self._on_locations_clicked) + if keyval == Gdk.KEY_comma: GLib.idle_add(self._on_preferences_clicked) - case _: - return "" - #Def shortcuts key listeners - def listen_to_keyboard_events(self): - - #Listeners - keyboard.add_hotkey('ctrl+l', lambda: self.on_hotkey_pressed("location")) - keyboard.add_hotkey('ctrl+r', lambda: self.on_hotkey_pressed("weather")) - keyboard.add_hotkey('ctrl+comma', lambda: self.on_hotkey_pressed("preferences")) - - #Wait empty key. None key stop de listener - keyboard.wait("") - - #Free hooks - keyboard.unhook_all()