From 92d9a2556fc04a04dd6e3b1a97806195a2c12eb3 Mon Sep 17 00:00:00 2001 From: vaxerski <43317083+vaxerski@users.noreply.github.com> Date: Sun, 17 Apr 2022 10:13:06 +0200 Subject: [PATCH] Query for keys better with xmodmap --- src/KeybindManager.cpp | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/src/KeybindManager.cpp b/src/KeybindManager.cpp index b8a96c0..1602476 100644 --- a/src/KeybindManager.cpp +++ b/src/KeybindManager.cpp @@ -33,14 +33,21 @@ uint32_t KeybindManager::getKeyCodeFromName(std::string name) { transform(name.begin(), name.end(), name.begin(), ::tolower); if (name.length() == 1) { - // key - std::string command = "xmodmap -pk | grep \"(" + name + ")\""; + // key v keysyms can be max 8 places, + space, + 0x + std::string command = "xmodmap -pk | grep -E -o \".{0,11}\\(" + name + "\\)\""; std::string returnValue = exec(command.c_str()); + if (returnValue == "") { + Debug::log(ERR, "Key " + name + " returned no results in the xmodmap!"); + return 0; + } + try { - returnValue = returnValue.substr(returnValue.find_first_of('x') + 1); + returnValue = returnValue.substr(returnValue.find_last_of('x') + 1); returnValue = returnValue.substr(0, returnValue.find_first_of(' ')); + Debug::log(LOG, "queried for key " + name + " -> response keysym " + returnValue); + return std::stoi(returnValue, nullptr, 16); // return hex to int } catch(...) { } } else { @@ -57,17 +64,23 @@ uint32_t KeybindManager::getKeyCodeFromName(std::string name) { } else if (name == "space") { return 0x20; } else { - // unknown key, find in the xmodmap - std::string command = "xmodmap -pk | grep \"(" + ORIGINALCASENAME + ")\""; + // unknown key, find in the xmodmap v case insensitive + std::string command = "xmodmap -pk | grep -E -i -o \".{0,11}\\(" + name + "\\)\""; std::string returnValue = exec(command.c_str()); + if (returnValue == "") { + Debug::log(ERR, "Key " + name + " returned no results in the xmodmap!"); + return 0; + } + try { - returnValue = returnValue.substr(returnValue.find_first_of('x') + 1); + returnValue = returnValue.substr(returnValue.find_last_of('x') + 1); returnValue = returnValue.substr(0, returnValue.find_first_of(' ')); + Debug::log(LOG, "queried for key " + name + " -> response keysym " + returnValue); + return std::stoi(returnValue, nullptr, 16); // return hex to int } catch (...) { - Debug::log(ERR, "Unknown key: " + ORIGINALCASENAME); } } }