From 726e0c7dcea3f187ac02bcd25a1a049b98b84502 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Sun, 23 Mar 2025 00:22:49 +0100 Subject: Allow binding non alpha-numerical keys without a modifier --- src/GlobalHotkeysLinux.cpp | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) (limited to 'src/GlobalHotkeysLinux.cpp') diff --git a/src/GlobalHotkeysLinux.cpp b/src/GlobalHotkeysLinux.cpp index 4df6390..fbba0ea 100644 --- a/src/GlobalHotkeysLinux.cpp +++ b/src/GlobalHotkeysLinux.cpp @@ -9,6 +9,7 @@ extern "C" { #include } #include +#include #include #define PIPE_READ 0 @@ -58,6 +59,10 @@ namespace gsr { return result; } + static bool x11_key_is_alpha_numerical(KeySym keysym) { + return (keysym >= XK_A && keysym <= XK_Z) || (keysym >= XK_a && keysym <= XK_z) || (keysym >= XK_0 && keysym <= XK_9); + } + GlobalHotkeysLinux::GlobalHotkeysLinux(GrabType grab_type) : grab_type(grab_type) { for(int i = 0; i < 2; ++i) { read_pipes[i] = -1; @@ -173,7 +178,7 @@ namespace gsr { return false; } - if(hotkey.modifiers == 0) { + if(hotkey.modifiers == 0 && x11_key_is_alpha_numerical(hotkey.key)) { //fprintf(stderr, "Error: GlobalHotkeysLinux::bind_key_press: hotkey requires a modifier\n"); return false; } @@ -185,7 +190,12 @@ namespace gsr { const std::string modifiers_command = linux_keys_to_command_string(modifiers.data(), modifiers.size()); char command[256]; - const int command_size = snprintf(command, sizeof(command), "bind %s %d+%s\n", id.c_str(), (int)keycode, modifiers_command.c_str()); + int command_size = 0; + if(modifiers_command.empty()) + command_size = snprintf(command, sizeof(command), "bind %s %d\n", id.c_str(), (int)keycode); + else + command_size = snprintf(command, sizeof(command), "bind %s %d+%s\n", id.c_str(), (int)keycode, modifiers_command.c_str()); + if(write(write_pipes[PIPE_WRITE], command, command_size) != command_size) { fprintf(stderr, "Error: GlobalHotkeysLinux::bind_key_press: failed to write command to gsr-global-hotkeys, error: %s\n", strerror(errno)); return false; -- cgit v1.2.3-70-g09d2