FifeGUI 0.3.0
A C++ GUI library designed for games.
shortcut.cpp
1// SPDX-License-Identifier: LGPL-2.1-or-later OR BSD-3-Clause
2// SPDX-FileCopyrightText: 2026 Fifengine contributors
3
4// Corresponding header include
5#include "fifechan/shortcut.hpp"
6
7// Standard library includes
8#include <cassert>
9#include <string>
10
11// Project headers (subdirs before local)
12#include "fifechan/events/keyevent.hpp"
13#include "fifechan/key.hpp"
14
15namespace fcn
16{
17
18 Shortcut::Shortcut(Key key, uint16_t modMask) : mKey(key), mModMask(modMask)
19 {
20 }
21
22 Shortcut Shortcut::fromKeycode(uint32_t keycode, uint16_t sdlMods)
23 {
24 // Convert SDL_Keymod bitmask to Shortcut::Modifier
25 // SDL_KMOD_* map as: SHIFT=1, CTRL=64, ALT=256, GUI=1024
26 uint16_t mods = 0;
27 if ((sdlMods & 1) != 0) {
28 mods |= Modifier::Shift; // SDL_KMOD_SHIFT
29 }
30 if ((sdlMods & 64) != 0) {
31 mods |= Modifier::Control; // SDL_KMOD_CTRL
32 }
33 if ((sdlMods & 256) != 0) {
34 mods |= Modifier::Alt; // SDL_KMOD_ALT
35 }
36 if ((sdlMods & 1024) != 0) {
37 mods |= Modifier::Meta; // SDL_KMOD_GUI
38 }
39
40 return Shortcut(Key(static_cast<int>(keycode)), mods);
41 }
42
43 bool Shortcut::matches(KeyEvent const & event) const
44 {
45 assert("event key must be valid" && event.getKey().getValue() != 0);
46 if (mKey.getValue() != event.getKey().getValue()) {
47 return false;
48 }
49
50 uint16_t eventMods = 0;
51 if (event.isShiftPressed()) {
52 eventMods |= Modifier::Shift;
53 }
54 if (event.isControlPressed()) {
55 eventMods |= Modifier::Control;
56 }
57 if (event.isAltPressed()) {
58 eventMods |= Modifier::Alt;
59 }
60 if (event.isMetaPressed()) {
61 eventMods |= Modifier::Meta;
62 }
63
64 return mModMask == eventMods;
65 }
66
67 bool Shortcut::conflicts(Shortcut const & other) const
68 {
69 // Same key and overlapping modifier masks
70 if (mKey.getValue() != other.mKey.getValue()) {
71 return false;
72 }
73
74 return (mModMask & other.mModMask) != 0;
75 }
76
77 std::string Shortcut::to_string() const
78 {
79 std::string result;
80
81 if ((mModMask & Modifier::Control) != 0) {
82 result += "Ctrl+";
83 }
84 if ((mModMask & Modifier::Shift) != 0) {
85 result += "Shift+";
86 }
87 if ((mModMask & Modifier::Alt) != 0) {
88 result += "Alt+";
89 }
90 if ((mModMask & Modifier::Meta) != 0) {
91 result += "Meta+";
92 }
93
94 // Append key name using the value
95 int const val = mKey.getValue();
96 if (val >= 32 && val <= 126) {
97 result += static_cast<char>(val);
98 } else {
99 result += "Key(" + std::to_string(val) + ")";
100 }
101
102 return result;
103 }
104
106 {
107 return mKey;
108 }
109
110 uint16_t Shortcut::getModMask() const
111 {
112 return mModMask;
113 }
114
115 bool Shortcut::operator==(Shortcut const & other) const
116 {
117 return mKey.getValue() == other.mKey.getValue() && mModMask == other.mModMask;
118 }
119
120 bool Shortcut::operator!=(Shortcut const & other) const
121 {
122 return !(*this == other);
123 }
124
125} // namespace fcn
bool isAltPressed() const
Checks if alt is pressed.
bool isControlPressed() const
Checks if control is pressed.
bool isMetaPressed() const
Checks whether meta is pressed.
bool isShiftPressed() const
Checks if shift is pressed.
Represents a key event.
Definition keyevent.hpp:26
Key const & getKey() const
Gets the key of the event.
Definition keyevent.cpp:48
uint16_t mModMask
Bitmask of Modifier flags.
Definition shortcut.hpp:157
bool conflicts(Shortcut const &other) const
Checks for collision with another shortcut.
Definition shortcut.cpp:67
std::string to_string() const
Returns a human-readable string, e.g.
Definition shortcut.cpp:77
bool operator==(Shortcut const &other) const
Compares two shortcuts for equality (same key + same mods).
Definition shortcut.cpp:115
static Shortcut fromKeycode(uint32_t keycode, uint16_t sdlMods=0)
Creates a Shortcut from a raw SDL keycode + SDL modifier mask.
Definition shortcut.cpp:22
Key mKey
The semantic key.
Definition shortcut.hpp:154
bool operator!=(Shortcut const &other) const
Compares two shortcuts for inequality.
Definition shortcut.cpp:120
bool matches(KeyEvent const &event) const
Compares the shortcut to a KeyEvent.
Definition shortcut.cpp:43
Shortcut()=default
Default constructor creates an unbound shortcut (key 0, no mods).
Key getKey() const
Gets the semantic key.
Definition shortcut.cpp:105
uint16_t getModMask() const
Gets the modifier bitmask.
Definition shortcut.cpp:110
Used replacement tokens by configure_file():