Name shortcut keys from ImGui, keeping only the names saved differently

The two key tables become 21 aliases (Num0 to NumEqual, and the arrows). Every key the old tables knew converts to and from text exactly as before.
This commit is contained in:
shweet committed 2026-11-22 18:50:42 -05:00
1 parent a9177b13d0
commit 14e9668bab
3 files changed
+51 -256

No files matched your search

+3 -3
View File
@@ -163,10 +163,10 @@ void Configure::update(Manager& manager, Settings& settings)
if (ImGui::IsKeyDown(ImGuiMod_Super)) if (ImGui::IsKeyDown(ImGuiMod_Super))
chord |= ImGuiMod_Super; chord |= ImGuiMod_Super;
for (auto& entry : KEY_MAP) for (int index = ImGuiKey_Tab; index <= ImGuiKey_KeypadEqual; ++index)
{ {
auto key = entry.second; auto key = (ImGuiKey)index;
if (ImGui::IsKeyPressed(key)) if (is_shortcut_key(key) && ImGui::IsKeyPressed(key))
{ {
chord |= key; chord |= key;
*settingString = chord_to_string(chord); *settingString = chord_to_string(chord);
+46 -130
View File
@@ -6,132 +6,55 @@
#include <imgui/imgui_internal.h> #include <imgui/imgui_internal.h>
const std::vector<std::pair<ImGuiKey, const char*>> CANONICAL_KEY_NAMES = { // Names shortcuts are saved with where they differ from ImGui's. ImGui's own names still read back.
{ImGuiKey_A, "A"}, constexpr std::pair<ImGuiKey, const char*> KEY_NAMES[] = {{ImGuiKey_Keypad0, "Num0"},
{ImGuiKey_B, "B"}, {ImGuiKey_Keypad1, "Num1"},
{ImGuiKey_C, "C"}, {ImGuiKey_Keypad2, "Num2"},
{ImGuiKey_D, "D"}, {ImGuiKey_Keypad3, "Num3"},
{ImGuiKey_E, "E"}, {ImGuiKey_Keypad4, "Num4"},
{ImGuiKey_F, "F"}, {ImGuiKey_Keypad5, "Num5"},
{ImGuiKey_G, "G"}, {ImGuiKey_Keypad6, "Num6"},
{ImGuiKey_H, "H"}, {ImGuiKey_Keypad7, "Num7"},
{ImGuiKey_I, "I"}, {ImGuiKey_Keypad8, "Num8"},
{ImGuiKey_J, "J"}, {ImGuiKey_Keypad9, "Num9"},
{ImGuiKey_K, "K"}, {ImGuiKey_KeypadAdd, "NumAdd"},
{ImGuiKey_L, "L"}, {ImGuiKey_KeypadSubtract, "NumSubtract"},
{ImGuiKey_M, "M"}, {ImGuiKey_KeypadMultiply, "NumMultiply"},
{ImGuiKey_N, "N"}, {ImGuiKey_KeypadDivide, "NumDivide"},
{ImGuiKey_O, "O"}, {ImGuiKey_KeypadEnter, "NumEnter"},
{ImGuiKey_P, "P"}, {ImGuiKey_KeypadDecimal, "NumDecimal"},
{ImGuiKey_Q, "Q"}, {ImGuiKey_KeypadEqual, "NumEqual"},
{ImGuiKey_R, "R"}, {ImGuiKey_UpArrow, "Up"},
{ImGuiKey_S, "S"}, {ImGuiKey_DownArrow, "Down"},
{ImGuiKey_T, "T"}, {ImGuiKey_LeftArrow, "Left"},
{ImGuiKey_U, "U"}, {ImGuiKey_RightArrow, "Right"}};
{ImGuiKey_V, "V"},
{ImGuiKey_W, "W"},
{ImGuiKey_X, "X"},
{ImGuiKey_Y, "Y"},
{ImGuiKey_Z, "Z"},
{ImGuiKey_0, "0"},
{ImGuiKey_1, "1"},
{ImGuiKey_2, "2"},
{ImGuiKey_3, "3"},
{ImGuiKey_4, "4"},
{ImGuiKey_5, "5"},
{ImGuiKey_6, "6"},
{ImGuiKey_7, "7"},
{ImGuiKey_8, "8"},
{ImGuiKey_9, "9"},
{ImGuiKey_Keypad0, "Num0"},
{ImGuiKey_Keypad1, "Num1"},
{ImGuiKey_Keypad2, "Num2"},
{ImGuiKey_Keypad3, "Num3"},
{ImGuiKey_Keypad4, "Num4"},
{ImGuiKey_Keypad5, "Num5"},
{ImGuiKey_Keypad6, "Num6"},
{ImGuiKey_Keypad7, "Num7"},
{ImGuiKey_Keypad8, "Num8"},
{ImGuiKey_Keypad9, "Num9"},
{ImGuiKey_KeypadAdd, "NumAdd"},
{ImGuiKey_KeypadSubtract, "NumSubtract"},
{ImGuiKey_KeypadMultiply, "NumMultiply"},
{ImGuiKey_KeypadDivide, "NumDivide"},
{ImGuiKey_KeypadEnter, "NumEnter"},
{ImGuiKey_KeypadDecimal, "NumDecimal"},
{ImGuiKey_KeypadEqual, "NumEqual"},
{ImGuiKey_F1, "F1"},
{ImGuiKey_F2, "F2"},
{ImGuiKey_F3, "F3"},
{ImGuiKey_F4, "F4"},
{ImGuiKey_F5, "F5"},
{ImGuiKey_F6, "F6"},
{ImGuiKey_F7, "F7"},
{ImGuiKey_F8, "F8"},
{ImGuiKey_F9, "F9"},
{ImGuiKey_F10, "F10"},
{ImGuiKey_F11, "F11"},
{ImGuiKey_F12, "F12"},
{ImGuiKey_UpArrow, "Up"},
{ImGuiKey_DownArrow, "Down"},
{ImGuiKey_LeftArrow, "Left"},
{ImGuiKey_RightArrow, "Right"},
{ImGuiKey_Space, "Space"},
{ImGuiKey_Enter, "Enter"},
{ImGuiKey_Escape, "Escape"},
{ImGuiKey_Tab, "Tab"},
{ImGuiKey_Backspace, "Backspace"},
{ImGuiKey_Delete, "Delete"},
{ImGuiKey_Insert, "Insert"},
{ImGuiKey_Home, "Home"},
{ImGuiKey_End, "End"},
{ImGuiKey_PageUp, "PageUp"},
{ImGuiKey_PageDown, "PageDown"},
{ImGuiKey_Minus, "Minus"},
{ImGuiKey_Equal, "Equal"},
{ImGuiKey_LeftBracket, "LeftBracket"},
{ImGuiKey_RightBracket, "RightBracket"},
{ImGuiKey_Semicolon, "Semicolon"},
{ImGuiKey_Apostrophe, "Apostrophe"},
{ImGuiKey_Comma, "Comma"},
{ImGuiKey_Period, "Period"},
{ImGuiKey_Slash, "Slash"},
{ImGuiKey_Backslash, "Backslash"},
{ImGuiKey_GraveAccent, "GraveAccent"},
};
static const char* shortcut_canonical_key_name(ImGuiKey key) constexpr std::pair<ImGuiKey, const char*> MOD_NAMES[] = {
{ImGuiMod_Ctrl, "Ctrl"}, {ImGuiMod_Shift, "Shift"}, {ImGuiMod_Alt, "Alt"}, {ImGuiMod_Super, "Super"}};
bool is_shortcut_key(ImGuiKey key)
{ {
for (auto& [mappedKey, name] : CANONICAL_KEY_NAMES) return key >= ImGuiKey_Tab && key <= ImGuiKey_KeypadEqual && (key < ImGuiKey_LeftCtrl || key > ImGuiKey_RightSuper);
if (mappedKey == key) }
static const char* key_name_get(ImGuiKey key)
{
for (auto [named, name] : KEY_NAMES)
if (named == key)
return name; return name;
return nullptr; return ImGui::GetKeyName(key);
} }
std::string chord_to_string(ImGuiKeyChord chord) std::string chord_to_string(ImGuiKeyChord chord)
{ {
std::string result; std::string result;
for (auto [mod, name] : MOD_NAMES)
if (chord & ImGuiMod_Ctrl) if (chord & mod)
result += "Ctrl+"; result += std::string(name) + "+";
if (chord & ImGuiMod_Shift)
result += "Shift+";
if (chord & ImGuiMod_Alt)
result += "Alt+";
if (chord & ImGuiMod_Super)
result += "Super+";
if (auto key = (ImGuiKey)(chord & ~ImGuiMod_Mask_); key != ImGuiKey_None) if (auto key = (ImGuiKey)(chord & ~ImGuiMod_Mask_); key != ImGuiKey_None)
{ result += key_name_get(key);
if (const char* name = shortcut_canonical_key_name(key); name && *name)
result += name;
else
result += ImGui::GetKeyName(key);
}
if (!result.empty() && result.back() == '+') if (!result.empty() && result.back() == '+')
result.pop_back(); result.pop_back();
return result; return result;
} }
@@ -139,28 +62,21 @@ ImGuiKeyChord string_to_chord(const std::string& string)
{ {
ImGuiKeyChord chord = 0; ImGuiKeyChord chord = 0;
ImGuiKey baseKey = ImGuiKey_None; ImGuiKey baseKey = ImGuiKey_None;
std::stringstream ss(string); std::stringstream ss(string);
std::string token; std::string token;
while (std::getline(ss, token, '+')) while (std::getline(ss, token, '+'))
{ {
token.erase(0, token.find_first_not_of(" \t\r\n")); token.erase(0, token.find_first_not_of(" \t\r\n"));
token.erase(token.find_last_not_of(" \t\r\n") + 1); token.erase(token.find_last_not_of(" \t\r\n") + 1);
for (auto [mod, name] : MOD_NAMES)
if (token.empty()) if (token == name)
continue; chord |= mod;
for (int key = ImGuiKey_Tab; baseKey == ImGuiKey_None && key <= ImGuiKey_KeypadEqual; ++key)
if (auto it = MOD_MAP.find(token); it != MOD_MAP.end()) if (is_shortcut_key((ImGuiKey)key) &&
chord |= it->second; (token == key_name_get((ImGuiKey)key) || token == ImGui::GetKeyName((ImGuiKey)key)))
else if (baseKey == ImGuiKey_None) baseKey = (ImGuiKey)key;
if (auto it2 = KEY_MAP.find(token); it2 != KEY_MAP.end())
baseKey = it2->second;
} }
return chord | baseKey;
if (baseKey != ImGuiKey_None)
chord |= baseKey;
return chord;
} }
bool shortcut(ImGuiKeyChord chord, ShortcutScope type) bool shortcut(ImGuiKeyChord chord, ShortcutScope type)
+2 -123
View File
@@ -1,134 +1,13 @@
#pragma once #pragma once
#include <string> #include <string>
#include <unordered_map>
#include <imgui/imgui.h> #include <imgui/imgui.h>
#include "types.h" #include "types.h"
inline const std::unordered_map<std::string, ImGuiKey> KEY_MAP = { // Keyboard keys a shortcut can use: Tab through the keypad, without the modifier keys themselves.
{"A", ImGuiKey_A}, bool is_shortcut_key(ImGuiKey);
{"B", ImGuiKey_B},
{"C", ImGuiKey_C},
{"D", ImGuiKey_D},
{"E", ImGuiKey_E},
{"F", ImGuiKey_F},
{"G", ImGuiKey_G},
{"H", ImGuiKey_H},
{"I", ImGuiKey_I},
{"J", ImGuiKey_J},
{"K", ImGuiKey_K},
{"L", ImGuiKey_L},
{"M", ImGuiKey_M},
{"N", ImGuiKey_N},
{"O", ImGuiKey_O},
{"P", ImGuiKey_P},
{"Q", ImGuiKey_Q},
{"R", ImGuiKey_R},
{"S", ImGuiKey_S},
{"T", ImGuiKey_T},
{"U", ImGuiKey_U},
{"V", ImGuiKey_V},
{"W", ImGuiKey_W},
{"X", ImGuiKey_X},
{"Y", ImGuiKey_Y},
{"Z", ImGuiKey_Z},
{"0", ImGuiKey_0},
{"1", ImGuiKey_1},
{"2", ImGuiKey_2},
{"3", ImGuiKey_3},
{"4", ImGuiKey_4},
{"5", ImGuiKey_5},
{"6", ImGuiKey_6},
{"7", ImGuiKey_7},
{"8", ImGuiKey_8},
{"9", ImGuiKey_9},
{"Num0", ImGuiKey_Keypad0},
{"Num1", ImGuiKey_Keypad1},
{"Num2", ImGuiKey_Keypad2},
{"Num3", ImGuiKey_Keypad3},
{"Num4", ImGuiKey_Keypad4},
{"Num5", ImGuiKey_Keypad5},
{"Num6", ImGuiKey_Keypad6},
{"Num7", ImGuiKey_Keypad7},
{"Num8", ImGuiKey_Keypad8},
{"Num9", ImGuiKey_Keypad9},
{"NumAdd", ImGuiKey_KeypadAdd},
{"NumSubtract", ImGuiKey_KeypadSubtract},
{"NumMultiply", ImGuiKey_KeypadMultiply},
{"NumDivide", ImGuiKey_KeypadDivide},
{"NumEnter", ImGuiKey_KeypadEnter},
{"NumDecimal", ImGuiKey_KeypadDecimal},
{"NumEqual", ImGuiKey_KeypadEqual},
{"F1", ImGuiKey_F1},
{"F2", ImGuiKey_F2},
{"F3", ImGuiKey_F3},
{"F4", ImGuiKey_F4},
{"F5", ImGuiKey_F5},
{"F6", ImGuiKey_F6},
{"F7", ImGuiKey_F7},
{"F8", ImGuiKey_F8},
{"F9", ImGuiKey_F9},
{"F10", ImGuiKey_F10},
{"F11", ImGuiKey_F11},
{"F12", ImGuiKey_F12},
{"Up", ImGuiKey_UpArrow},
{"Down", ImGuiKey_DownArrow},
{"Left", ImGuiKey_LeftArrow},
{"Right", ImGuiKey_RightArrow},
{"Space", ImGuiKey_Space},
{"Enter", ImGuiKey_Enter},
{"Escape", ImGuiKey_Escape},
{"Tab", ImGuiKey_Tab},
{"Backspace", ImGuiKey_Backspace},
{"Delete", ImGuiKey_Delete},
{"Insert", ImGuiKey_Insert},
{"Home", ImGuiKey_Home},
{"End", ImGuiKey_End},
{"PageUp", ImGuiKey_PageUp},
{"PageDown", ImGuiKey_PageDown},
{"Minus", ImGuiKey_Minus},
{"Equal", ImGuiKey_Equal},
{"LeftBracket", ImGuiKey_LeftBracket},
{"RightBracket", ImGuiKey_RightBracket},
{"Semicolon", ImGuiKey_Semicolon},
{"Apostrophe", ImGuiKey_Apostrophe},
{"Comma", ImGuiKey_Comma},
{"Period", ImGuiKey_Period},
{"Slash", ImGuiKey_Slash},
{"Backslash", ImGuiKey_Backslash},
{"GraveAccent", ImGuiKey_GraveAccent},
{"Keypad0", ImGuiKey_Keypad0},
{"Keypad1", ImGuiKey_Keypad1},
{"Keypad2", ImGuiKey_Keypad2},
{"Keypad3", ImGuiKey_Keypad3},
{"Keypad4", ImGuiKey_Keypad4},
{"Keypad5", ImGuiKey_Keypad5},
{"Keypad6", ImGuiKey_Keypad6},
{"Keypad7", ImGuiKey_Keypad7},
{"Keypad8", ImGuiKey_Keypad8},
{"Keypad9", ImGuiKey_Keypad9},
{"KeypadAdd", ImGuiKey_KeypadAdd},
{"KeypadSubtract", ImGuiKey_KeypadSubtract},
{"KeypadMultiply", ImGuiKey_KeypadMultiply},
{"KeypadDivide", ImGuiKey_KeypadDivide},
{"KeypadEnter", ImGuiKey_KeypadEnter},
{"KeypadDecimal", ImGuiKey_KeypadDecimal},
{"KeypadEqual", ImGuiKey_KeypadEqual},
{"UpArrow", ImGuiKey_UpArrow},
{"DownArrow", ImGuiKey_DownArrow},
{"LeftArrow", ImGuiKey_LeftArrow},
{"RightArrow", ImGuiKey_RightArrow},
};
inline const std::unordered_map<std::string, ImGuiKey> MOD_MAP = {
{"Ctrl", ImGuiMod_Ctrl},
{"Shift", ImGuiMod_Shift},
{"Alt", ImGuiMod_Alt},
{"Super", ImGuiMod_Super},
};
std::string chord_to_string(ImGuiKeyChord); std::string chord_to_string(ImGuiKeyChord);
ImGuiKeyChord string_to_chord(const std::string&); ImGuiKeyChord string_to_chord(const std::string&);
bool shortcut(ImGuiKeyChord, ShortcutScope = SCOPE_FOCUSED_SET); bool shortcut(ImGuiKeyChord, ShortcutScope = SCOPE_FOCUSED_SET);