Index: ps/trunk/binaries/data/config/keys.txt =================================================================== --- ps/trunk/binaries/data/config/keys.txt (revision 19623) +++ ps/trunk/binaries/data/config/keys.txt (revision 19624) @@ -1,250 +1,154 @@ ## This file documents keynames that can be used in .cfg files for specifying hotkeys ## Note: the keynames are not actually configured or implemented here +## Also note: if your keyboard supports keys that do not appear here (such as the French é letter), +## you should be able to use it directly. Backspace, BkSp Tab Clear Return, Ret Pause Escape, Esc Space, Spc !, Exclaim ", DoubleQuote #, Hash $, Dollar &, Ampersand ', SingleQuote (, LeftParen ), RightParen *, Asterisk +, Plus ,, Comma -, Minus ., Period /, ForwardSlash 0 1 2 3 4 5 6 7 8 9 :, Colon ;, Semicolon <, LessThan =, Equals >, GreaterThan ?, Question @, At [, LeftBracket \, BackSlash ], RightBracket ^, Caret _, Underscore `, BackQuote A B C D E F G H I J K L M N O P Q R S T U V W X Y Z Delete, Del Numpad 0, Num0 Numpad 1, Num1 Numpad 2, Num2 Numpad 3, Num3 Numpad 4, Num4 Numpad 5, Num5 Numpad 6, Num6 Numpad 7, Num7 Numpad 8, Num8 Numpad 9, Num9 Numpad ., NumPoint Numpad /, NumDivide Numpad *, NumMultiply Numpad -, NumMinus Numpad +, NumPlus Numpad Enter, NumEnter Numpad =, NumEquals Arrow Up, UpArrow Arrow Down, DownArrow Arrow Right, RightArrow Arrow Left, LeftArrow Insert, Ins Home End Page Up, PgUp Page Down, PgDn F1 F2 F3 F4 F5 F6 F7 F8 F9 F10 F11 F12 F13 F14 F15 Num Lock, NumLock Caps Lock, CapsLock Scroll Lock, ScrlLock Right Shift, RightShift Left Shift, LeftShift Shift, AnyShift Right Ctrl, RightCtrl Left Ctrl, LeftCtrl Ctrl, AnyCtrl Right Alt, RightAlt Left Alt, LeftAlt Alt, AnyAlt Left Super, LeftWin Right Super, RightWin Super, AnyWindows ## Windows key, also Command/meta key on Macs Alt Gr, AltGr Compose Help Print Screen, PrtSc SysRq Break Menu Power Euro Undo Left Mouse Button, MouseLeft Right Mouse Button, MouseRight Middle Mouse Button, MouseMiddle Mouse Wheel Up, WheelUp Mouse Wheel Down, WheelDown MouseButtonX, MouseNX # where X is a number 1-255, for extra mouse buttons ## (note that some mice start numbering their buttons at higher numbers ## so the first extra button on your mouse might be #8 here) - -## Special keys for international and non-QWERTY keyboards: -World0, W0 -World1, W1 -World2, W2 -World3, W3 -World4, W4 -World5, W5 -World6, W6 -World7, W7 -World8, W8 -World9, W9 -World10, W10 -World11, W11 -World12, W12 -World13, W13 -World14, W14 -World15, W15 -World16, W16 -World17, W17 -World18, W18 -World19, W19 -World20, W20 -World21, W21 -World22, W22 -World23, W23 -World24, W24 -World25, W25 -World26, W26 -World27, W27 -World28, W28 -World29, W29 -World30, W30 -World31, W31 -World32, W32 -World33, W33 -World34, W34 -World35, W35 -World36, W36 -World37, W37 -World38, W38 -World39, W39 -World40, W40 -World41, W41 -World42, W42 -World43, W43 -World44, W44 -World45, W45 -World46, W46 -World47, W47 -World48, W48 -World49, W49 -World50, W50 -World51, W51 -World52, W52 -World53, W53 -World54, W54 -World55, W55 -World56, W56 -World57, W57 -World58, W58 -World59, W59 -World60, W60 -World61, W61 -World62, W62 -World63, W63 -World64, W64 -World65, W65 -World66, W66 -World67, W67 -World68, W68 -World69, W69 -World70, W70 -World71, W71 -World72, W72 -World73, W73 -World74, W74 -World75, W75 -World76, W76 -World77, W77 -World78, W78 -World79, W79 -World80, W80 -World81, W81 -World82, W82 -World83, W83 -World84, W84 -World85, W85 -World86, W86 -World87, W87 -World88, W88 -World89, W89 -World90, W90 -World91, W91 -World92, W92 -World93, W93 -World94, W94 -World95, W95 Index: ps/trunk/source/ps/Hotkey.cpp =================================================================== --- ps/trunk/source/ps/Hotkey.cpp (revision 19623) +++ ps/trunk/source/ps/Hotkey.cpp (revision 19624) @@ -1,346 +1,348 @@ /* Copyright (C) 2017 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 2 of the License, or * (at your option) any later version. * * 0 A.D. is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with 0 A.D. If not, see . */ #include "precompiled.h" #include "Hotkey.h" #include #include "ps/CConsole.h" #include "ps/CLogger.h" #include "ps/CStr.h" #include "ps/ConfigDB.h" #include "ps/Globals.h" #include "ps/KeyName.h" static bool unified[UNIFIED_LAST - UNIFIED_SHIFT]; struct SKey { SDL_Keycode code; // keycode or MOUSE_ or UNIFIED_ value bool negated; // whether the key must be pressed (false) or unpressed (true) }; // Hotkey data associated with an externally-specified 'primary' keycode struct SHotkeyMapping { CStr name; // name of the hotkey bool negated; // whether the primary key must be pressed (false) or unpressed (true) std::vector requires; // list of non-primary keys that must also be active }; typedef std::vector KeyMapping; // A mapping of keycodes onto the hotkeys that are associated with that key. // (A hotkey triggered by a combination of multiple keys will be in this map // multiple times.) static std::map g_HotkeyMap; // The current pressed status of hotkeys std::map g_HotkeyStatus; // Look up each key binding in the config file and set the mappings for // all key combinations that trigger it. static void LoadConfigBindings() { for (const std::pair& configPair : g_ConfigDB.GetValuesWithPrefix(CFG_COMMAND, "hotkey.")) { std::string hotkeyName = configPair.first.substr(7); // strip the "hotkey." prefix for (const CStr& hotkey : configPair.second) { if (hotkey.LowerCase() == "unused") continue; std::vector keyCombination; // Iterate through multiple-key bindings (e.g. Ctrl+I) boost::char_separator sep("+"); typedef boost::tokenizer > tokenizer; tokenizer tok(hotkey, sep); for (tokenizer::iterator it = tok.begin(); it != tok.end(); ++it) { // Attempt decode as key name int mapping = FindKeyCode(*it); if (!mapping) + mapping = SDL_GetKeyFromName(it->c_str()); + if (!mapping) { LOGWARNING("Hotkey mapping used invalid key '%s'", hotkey.c_str()); continue; } SKey key = { (SDL_Keycode)mapping, false }; keyCombination.push_back(key); } std::vector::iterator itKey, itKey2; for (itKey = keyCombination.begin(); itKey != keyCombination.end(); ++itKey) { SHotkeyMapping bindCode; bindCode.name = hotkeyName; bindCode.negated = itKey->negated; for (itKey2 = keyCombination.begin(); itKey2 != keyCombination.end(); ++itKey2) if (itKey != itKey2) // Push any auxiliary keys bindCode.requires.push_back(*itKey2); g_HotkeyMap[itKey->code].push_back(bindCode); } } } } void LoadHotkeys() { InitKeyNameMap(); LoadConfigBindings(); // Set up the state of the hotkeys given no key is down. // i.e. find those hotkeys triggered by all negations. for (const std::pair& p : g_HotkeyMap) for (const SHotkeyMapping& hotkey : p.second) { if (!hotkey.negated) continue; bool allNegated = true; for (const SKey& k : hotkey.requires) if (!k.negated) allNegated = false; if (allNegated) g_HotkeyStatus[hotkey.name] = true; } } void UnloadHotkeys() { g_HotkeyMap.clear(); g_HotkeyStatus.clear(); } bool isNegated(const SKey& key) { // Normal keycodes are below EXTRA_KEYS_BASE if ((int)key.code < EXTRA_KEYS_BASE && g_keys[key.code] == key.negated) return false; // Mouse 'keycodes' are after the modifier keys else if ((int)key.code < MOUSE_LAST && (int)key.code > MOUSE_BASE && g_mouse_buttons[key.code - MOUSE_BASE] == key.negated) return false; // Modifier keycodes are between the normal keys and the mouse 'keys' else if ((int)key.code < UNIFIED_LAST && (int)key.code > SDL_SCANCODE_TO_KEYCODE(SDL_NUM_SCANCODES) && unified[key.code - UNIFIED_SHIFT] == key.negated) return false; else return true; } InReaction HotkeyInputHandler(const SDL_Event_* ev) { int keycode = 0; switch(ev->ev.type) { case SDL_KEYDOWN: case SDL_KEYUP: keycode = (int)ev->ev.key.keysym.sym; break; case SDL_MOUSEBUTTONDOWN: case SDL_MOUSEBUTTONUP: // Mousewheel events are no longer buttons, but we want to maintain the order // expected by g_mouse_buttons for compatibility if (ev->ev.button.button >= SDL_BUTTON_X1) keycode = MOUSE_BASE + (int)ev->ev.button.button + 2; else keycode = MOUSE_BASE + (int)ev->ev.button.button; break; case SDL_MOUSEWHEEL: if (ev->ev.wheel.y > 0) { keycode = MOUSE_WHEELUP; break; } else if (ev->ev.wheel.y < 0) { keycode = MOUSE_WHEELDOWN; break; } else if (ev->ev.wheel.x > 0) { keycode = MOUSE_X2; break; } else if (ev->ev.wheel.x < 0) { keycode = MOUSE_X1; break; } return IN_PASS; case SDL_HOTKEYDOWN: g_HotkeyStatus[static_cast(ev->ev.user.data1)] = true; return IN_PASS; case SDL_HOTKEYUP: g_HotkeyStatus[static_cast(ev->ev.user.data1)] = false; return IN_PASS; default: return IN_PASS; } // Somewhat hackish: // Create phantom 'unified-modifier' events when left- or right- modifier keys are pressed // Just send them to this handler; don't let the imaginary event codes leak back to real SDL. SDL_Event_ phantom; phantom.ev.type = ((ev->ev.type == SDL_KEYDOWN) || (ev->ev.type == SDL_MOUSEBUTTONDOWN)) ? SDL_KEYDOWN : SDL_KEYUP; if ((keycode == SDLK_LSHIFT) || (keycode == SDLK_RSHIFT)) { phantom.ev.key.keysym.sym = (SDL_Keycode)UNIFIED_SHIFT; unified[0] = (phantom.ev.type == SDL_KEYDOWN); HotkeyInputHandler(&phantom); } else if ((keycode == SDLK_LCTRL) || (keycode == SDLK_RCTRL)) { phantom.ev.key.keysym.sym = (SDL_Keycode)UNIFIED_CTRL; unified[1] = (phantom.ev.type == SDL_KEYDOWN); HotkeyInputHandler(&phantom); } else if ((keycode == SDLK_LALT) || (keycode == SDLK_RALT)) { phantom.ev.key.keysym.sym = (SDL_Keycode)UNIFIED_ALT; unified[2] = (phantom.ev.type == SDL_KEYDOWN); HotkeyInputHandler(&phantom); } else if ((keycode == SDLK_LGUI) || (keycode == SDLK_RGUI)) { phantom.ev.key.keysym.sym = (SDL_Keycode)UNIFIED_SUPER; unified[3] = (phantom.ev.type == SDL_KEYDOWN); HotkeyInputHandler(&phantom); } // Check whether we have any hotkeys registered for this particular keycode if (g_HotkeyMap.find(keycode) == g_HotkeyMap.end()) return (IN_PASS); // Inhibit the dispatch of hotkey events caused by real keys (not fake mouse button // events) while the console is up. bool consoleCapture = false; if (g_Console->IsActive() && keycode < SDL_SCANCODE_TO_KEYCODE(SDL_NUM_SCANCODES)) consoleCapture = true; // Here's an interesting bit: // If you have an event bound to, say, 'F', and another to, say, 'Ctrl+F', pressing // 'F' while control is down would normally fire off both. // To avoid this, set the modifier keys for /all/ events this key would trigger // (Ctrl, for example, is both group-save and bookmark-save) // but only send a HotkeyDown event for the event with bindings most precisely // matching the conditions (i.e. the event with the highest number of auxiliary // keys, providing they're all down) bool typeKeyDown = ( ev->ev.type == SDL_KEYDOWN ) || ( ev->ev.type == SDL_MOUSEBUTTONDOWN ) || (ev->ev.type == SDL_MOUSEWHEEL); // -- KEYDOWN SECTION -- std::vector closestMapNames; size_t closestMapMatch = 0; for (const SHotkeyMapping& hotkey : g_HotkeyMap[keycode]) { // If a key has been pressed, and this event triggers on its release, skip it. // Similarly, if the key's been released and the event triggers on a keypress, skip it. if (hotkey.negated == typeKeyDown) continue; // Check for no unpermitted keys bool accept = true; for (const SKey& k : hotkey.requires) { accept = isNegated(k); if (!accept) break; } if (accept && !(consoleCapture && hotkey.name != "console.toggle")) { // Check if this is an equally precise or more precise match if (hotkey.requires.size() + 1 >= closestMapMatch) { // Check if more precise if (hotkey.requires.size() + 1 > closestMapMatch) { // Throw away the old less-precise matches closestMapNames.clear(); closestMapMatch = hotkey.requires.size() + 1; } closestMapNames.push_back(hotkey.name.c_str()); } } } for (size_t i = 0; i < closestMapNames.size(); ++i) { SDL_Event_ hotkeyNotification; hotkeyNotification.ev.type = SDL_HOTKEYDOWN; hotkeyNotification.ev.user.data1 = const_cast(closestMapNames[i]); in_push_priority_event(&hotkeyNotification); } // -- KEYUP SECTION -- for (const SHotkeyMapping& hotkey : g_HotkeyMap[keycode]) { // If it's a keydown event, won't cause HotKeyUps in anything that doesn't // use this key negated => skip them // If it's a keyup event, won't cause HotKeyUps in anything that does use // this key negated => skip them too. if (hotkey.negated != typeKeyDown) continue; // Check for no unpermitted keys bool accept = true; for (const SKey& k : hotkey.requires) { accept = isNegated(k); if (!accept) break; } if (accept) { SDL_Event_ hotkeyNotification; hotkeyNotification.ev.type = SDL_HOTKEYUP; hotkeyNotification.ev.user.data1 = const_cast(hotkey.name.c_str()); in_push_priority_event(&hotkeyNotification); } } return IN_PASS; } bool HotkeyIsPressed(const CStr& keyname) { return g_HotkeyStatus[keyname]; }