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
+32 -237

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))
chord |= ImGuiMod_Super;
for (auto& entry : KEY_MAP)
for (int index = ImGuiKey_Tab; index <= ImGuiKey_KeypadEqual; ++index)
{
auto key = entry.second;
if (ImGui::IsKeyPressed(key))
auto key = (ImGuiKey)index;
if (is_shortcut_key(key) && ImGui::IsKeyPressed(key))
{
chord |= key;
*settingString = chord_to_string(chord);
+27 -111
View File
@@ -6,44 +6,8 @@
#include <imgui/imgui_internal.h>
const std::vector<std::pair<ImGuiKey, const char*>> CANONICAL_KEY_NAMES = {
{ImGuiKey_A, "A"},
{ImGuiKey_B, "B"},
{ImGuiKey_C, "C"},
{ImGuiKey_D, "D"},
{ImGuiKey_E, "E"},
{ImGuiKey_F, "F"},
{ImGuiKey_G, "G"},
{ImGuiKey_H, "H"},
{ImGuiKey_I, "I"},
{ImGuiKey_J, "J"},
{ImGuiKey_K, "K"},
{ImGuiKey_L, "L"},
{ImGuiKey_M, "M"},
{ImGuiKey_N, "N"},
{ImGuiKey_O, "O"},
{ImGuiKey_P, "P"},
{ImGuiKey_Q, "Q"},
{ImGuiKey_R, "R"},
{ImGuiKey_S, "S"},
{ImGuiKey_T, "T"},
{ImGuiKey_U, "U"},
{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"},
// Names shortcuts are saved with where they differ from ImGui's. ImGui's own names still read back.
constexpr std::pair<ImGuiKey, const char*> KEY_NAMES[] = {{ImGuiKey_Keypad0, "Num0"},
{ImGuiKey_Keypad1, "Num1"},
{ImGuiKey_Keypad2, "Num2"},
{ImGuiKey_Keypad3, "Num3"},
@@ -60,78 +24,37 @@ const std::vector<std::pair<ImGuiKey, const char*>> CANONICAL_KEY_NAMES = {
{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"},
};
{ImGuiKey_RightArrow, "Right"}};
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)
if (mappedKey == key)
return key >= ImGuiKey_Tab && key <= ImGuiKey_KeypadEqual && (key < ImGuiKey_LeftCtrl || key > ImGuiKey_RightSuper);
}
static const char* key_name_get(ImGuiKey key)
{
for (auto [named, name] : KEY_NAMES)
if (named == key)
return name;
return nullptr;
return ImGui::GetKeyName(key);
}
std::string chord_to_string(ImGuiKeyChord chord)
{
std::string result;
if (chord & ImGuiMod_Ctrl)
result += "Ctrl+";
if (chord & ImGuiMod_Shift)
result += "Shift+";
if (chord & ImGuiMod_Alt)
result += "Alt+";
if (chord & ImGuiMod_Super)
result += "Super+";
for (auto [mod, name] : MOD_NAMES)
if (chord & mod)
result += std::string(name) + "+";
if (auto key = (ImGuiKey)(chord & ~ImGuiMod_Mask_); key != ImGuiKey_None)
{
if (const char* name = shortcut_canonical_key_name(key); name && *name)
result += name;
else
result += ImGui::GetKeyName(key);
}
result += key_name_get(key);
if (!result.empty() && result.back() == '+')
result.pop_back();
return result;
}
@@ -139,28 +62,21 @@ ImGuiKeyChord string_to_chord(const std::string& string)
{
ImGuiKeyChord chord = 0;
ImGuiKey baseKey = ImGuiKey_None;
std::stringstream ss(string);
std::string token;
while (std::getline(ss, token, '+'))
{
token.erase(0, token.find_first_not_of(" \t\r\n"));
token.erase(token.find_last_not_of(" \t\r\n") + 1);
if (token.empty())
continue;
if (auto it = MOD_MAP.find(token); it != MOD_MAP.end())
chord |= it->second;
else if (baseKey == ImGuiKey_None)
if (auto it2 = KEY_MAP.find(token); it2 != KEY_MAP.end())
baseKey = it2->second;
for (auto [mod, name] : MOD_NAMES)
if (token == name)
chord |= mod;
for (int key = ImGuiKey_Tab; baseKey == ImGuiKey_None && key <= ImGuiKey_KeypadEqual; ++key)
if (is_shortcut_key((ImGuiKey)key) &&
(token == key_name_get((ImGuiKey)key) || token == ImGui::GetKeyName((ImGuiKey)key)))
baseKey = (ImGuiKey)key;
}
if (baseKey != ImGuiKey_None)
chord |= baseKey;
return chord;
return chord | baseKey;
}
bool shortcut(ImGuiKeyChord chord, ShortcutScope type)
+2 -123
View File
@@ -1,134 +1,13 @@
#pragma once
#include <string>
#include <unordered_map>
#include <imgui/imgui.h>
#include "types.h"
inline const std::unordered_map<std::string, ImGuiKey> KEY_MAP = {
{"A", ImGuiKey_A},
{"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},
};
// Keyboard keys a shortcut can use: Tab through the keypad, without the modifier keys themselves.
bool is_shortcut_key(ImGuiKey);
std::string chord_to_string(ImGuiKeyChord);
ImGuiKeyChord string_to_chord(const std::string&);
bool shortcut(ImGuiKeyChord, ShortcutScope = SCOPE_FOCUSED_SET);