Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
Hotkey.h
Go to the documentation of this file.
1/* Copyright (C) 2026 Wildfire Games.
2 * This file is part of 0 A.D.
3 *
4 * 0 A.D. is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * 0 A.D. is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#ifndef INCLUDED_HOTKEY
19#define INCLUDED_HOTKEY
20
21/**
22 * @file
23 * Hotkey system.
24 *
25 * Hotkeys consist of a name (an arbitrary string), and a key mapping.
26 * The names and mappings are loaded from the config system (any
27 * config setting with the name prefix "hotkey.").
28 * When a hotkey is pressed one SDL_HOTKEYPRESS is triggered. While the key is
29 * kept down repeated SDL_HOTKEYDOWN events are triggered at an interval
30 * determined by the OS. When a hotkey is released an SDL_HOTKEYUP event is
31 * triggered. All with the hotkey name stored in ev.user.data1 as a const char*.
32 */
33
34#include "ps/CStr.h"
36#include "ps/Input.h"
37
38#include <unordered_map>
39#include <vector>
40
41// SDL_Scancode is an enum, we'll use an explicit int to avoid including SDL in this header.
42using SDL_Scancode_ = int;
43
44// 0x8000 is SDL_USEREVENT, this is static_asserted in Hotkey.cpp
45// We do this to avoid including SDL in this header.
46const unsigned int SDL_USEREVENT_ = 0x8000;
47const unsigned int SDL_HOTKEYPRESS = SDL_USEREVENT_;
48const unsigned int SDL_HOTKEYDOWN = SDL_USEREVENT_ + 1;
49const unsigned int SDL_HOTKEYUP = SDL_USEREVENT_ + 2;
50const unsigned int SDL_HOTKEYPRESS_SILENT = SDL_USEREVENT_ + 3;
51const unsigned int SDL_HOTKEYUP_SILENT = SDL_USEREVENT_ + 4;
52
53// Value not mapping to any valid SDL_SCANCODE_*
55
56struct SKey
57{
58 SDL_Scancode_ code; // scancode or MOUSE_ or UNIFIED_ value
59 bool operator<(const SKey& o) const { return code < o.code; }
60 bool operator==(const SKey& o) const { return code == o.code; }
61};
62
63// Hotkey data associated with an externally-specified 'primary' keycode
65{
66 CStr name; // name of the hotkey
67 SKey primary; // the primary key
68 PS::StaticVector<SKey, 8> required; // list of non-primary keys that must also be active
69};
70
71typedef std::vector<SHotkeyMapping> KeyMapping;
72
73// A mapping of scancodes onto the hotkeys that are associated with that key.
74// (A hotkey triggered by a combination of multiple keys will be in this map
75// multiple times.)
76extern std::unordered_map<SDL_Scancode_, KeyMapping> g_HotkeyMap;
77
78class CConfigDB;
79extern void LoadHotkeys(CConfigDB& configDB);
80extern void UnloadHotkeys();
81
82/**
83 * Updates g_HotkeyMap.
84 */
85extern Input::Reaction HotkeyStateChange(const SDL_Event& ev);
86
87/**
88 * Detects hotkeys that should fire. This allows using EventWillFireHotkey,
89 * (and then possibly preventing those hotkeys from firing by handling the event).
90 */
91extern Input::Reaction HotkeyInputPrepHandler(const SDL_Event& ev);
92/**
93 * Actually fires hotkeys.
94 */
95extern Input::Reaction HotkeyInputActualHandler(const SDL_Event& ev);
96
97/**
98 * @return whether the event @param ev will fire the hotkey @param keyname.
99 */
100extern bool EventWillFireHotkey(const SDL_Event& ev, const CStr& keyname);
101
102/**
103 * Resets all currently active hotkeys (and clears in-flight hotkeys).
104 * You should call this when something grabs key input, e.g. an input box,
105 * as those prevent keydown/keyup messages from reaching the hotkey system,
106 * and can lead to hotkeys being stuck active.
107 * NB: active hotkeys are released immediately and "HotkeyUp" message sent.
108 */
109extern void ResetActiveHotkeys();
110
111/**
112 * @return whether the hotkey is currently pressed (i.e. active).
113 */
114extern bool HotkeyIsPressed(const CStr& keyname);
115
116#endif // INCLUDED_HOTKEY
bool HotkeyIsPressed(const CStr &keyname)
Definition: Hotkey.cpp:492
const unsigned int SDL_HOTKEYUP_SILENT
Definition: Hotkey.h:51
void LoadHotkeys(CConfigDB &configDB)
Definition: Hotkey.cpp:148
constexpr SDL_Scancode_ UNUSED_HOTKEY_CODE
Definition: Hotkey.h:54
int SDL_Scancode_
Definition: Hotkey.h:42
const unsigned int SDL_HOTKEYUP
Definition: Hotkey.h:49
void UnloadHotkeys()
Definition: Hotkey.cpp:155
const unsigned int SDL_HOTKEYPRESS_SILENT
Definition: Hotkey.h:50
const unsigned int SDL_HOTKEYDOWN
Definition: Hotkey.h:48
std::unordered_map< SDL_Scancode_, KeyMapping > g_HotkeyMap
Definition: Hotkey.cpp:47
const unsigned int SDL_HOTKEYPRESS
Definition: Hotkey.h:47
Input::Reaction HotkeyStateChange(const SDL_Event &ev)
Updates g_HotkeyMap.
Definition: Hotkey.cpp:178
Input::Reaction HotkeyInputActualHandler(const SDL_Event &ev)
Actually fires hotkeys.
Definition: Hotkey.cpp:363
const unsigned int SDL_USEREVENT_
Definition: Hotkey.h:46
Input::Reaction HotkeyInputPrepHandler(const SDL_Event &ev)
Detects hotkeys that should fire.
Definition: Hotkey.cpp:187
bool EventWillFireHotkey(const SDL_Event &ev, const CStr &keyname)
Definition: Hotkey.cpp:467
void ResetActiveHotkeys()
Resets all currently active hotkeys (and clears in-flight hotkeys).
Definition: Hotkey.cpp:477
std::vector< SHotkeyMapping > KeyMapping
Definition: Hotkey.h:71
Definition: ConfigDB.h:69
Reaction
Definition: Input.h:39
Definition: Hotkey.h:65
SKey primary
Definition: Hotkey.h:67
CStr name
Definition: Hotkey.h:66
PS::StaticVector< SKey, 8 > required
Definition: Hotkey.h:68
Definition: Hotkey.h:57
bool operator==(const SKey &o) const
Definition: Hotkey.h:60
SDL_Scancode_ code
Definition: Hotkey.h:58
bool operator<(const SKey &o) const
Definition: Hotkey.h:59