TGUI  1.6.1
Loading...
Searching...
No Matches
Keyboard.hpp
1
2//
3// TGUI - Texus' Graphical User Interface
4// Copyright (C) 2012-2024 Bruno Van de Velde (vdv_b@tgui.eu)
5//
6// This software is provided 'as-is', without any express or implied warranty.
7// In no event will the authors be held liable for any damages arising from the use of this software.
8//
9// Permission is granted to anyone to use this software for any purpose,
10// including commercial applications, and to alter it and redistribute it freely,
11// subject to the following restrictions:
12//
13// 1. The origin of this software must not be misrepresented;
14// you must not claim that you wrote the original software.
15// If you use this software in a product, an acknowledgment
16// in the product documentation would be appreciated but is not required.
17//
18// 2. Altered source versions must be plainly marked as such,
19// and must not be misrepresented as being the original software.
20//
21// 3. This notice may not be removed or altered from any source distribution.
22//
24
25#ifndef TGUI_KEYBOARD_HPP
26#define TGUI_KEYBOARD_HPP
27
29
30#include <TGUI/Config.hpp>
31#include <TGUI/Event.hpp>
32#include <TGUI/Container.hpp>
33#include <TGUI/Widgets/ScrollablePanel.hpp>
34#include <TGUI/Backend/Window/Backend.hpp>
35#include <TGUI/Backend/Window/BackendGui.hpp>
36
38
39namespace tgui
40{
41 namespace keyboard
42 {
44#ifndef TGUI_REMOVE_DEPRECATED_CODE
45 TGUI_DEPRECATED("Use BackendGui::startTextInput instead") inline void openVirtualKeyboard(const Widget* requestingWidget, FloatRect inputRect)
46 {
47 const Widget* widget = requestingWidget;
48 while (widget)
49 {
50 const bool defaultOrigin = (widget->getOrigin().x == 0) && (widget->getOrigin().y == 0);
51 const bool scaledOrRotated = (widget->getScale().x != 1) || (widget->getScale().y != 1) || (widget->getRotation() != 0);
52 if (defaultOrigin && !scaledOrRotated)
53 inputRect.setPosition(inputRect.getPosition() + widget->getPosition());
54 else
55 {
56 const Vector2f origin{widget->getOrigin().x * widget->getSize().x, widget->getOrigin().y * widget->getSize().y};
57 if (!scaledOrRotated)
58 inputRect.setPosition(inputRect.getPosition() + widget->getPosition() - origin);
59 else
60 {
61 const Vector2f rotOrigin{widget->getRotationOrigin().x * widget->getSize().x, widget->getRotationOrigin().y * widget->getSize().y};
62 const Vector2f scaleOrigin{widget->getScaleOrigin().x * widget->getSize().x, widget->getScaleOrigin().y * widget->getSize().y};
63
64 Transform transform;
65 transform.translate(widget->getPosition() - origin);
66 transform.rotate(widget->getRotation(), rotOrigin);
67 transform.scale(widget->getScale(), scaleOrigin);
68 inputRect = transform.transformRect(inputRect);
69 }
70 }
71
72 const Container* parent = widget->getParent();
73 if (parent)
74 {
75 inputRect.setPosition(inputRect.getPosition() + parent->getChildWidgetsOffset());
76
77 const ScrollablePanel* panel = dynamic_cast<const ScrollablePanel*>(parent);
78 if (panel)
79 inputRect.setPosition(inputRect.getPosition() - panel->getContentOffset());
80 }
81
82 widget = parent;
83 }
84
85 const auto gui = requestingWidget->getParentGui();
86 if (gui)
87 {
88 const Vector2f topLeftPos = gui->mapCoordsToPixel(inputRect.getPosition());
89 const Vector2f bottomRightPos = gui->mapCoordsToPixel(inputRect.getPosition() + inputRect.getSize());
90 inputRect = {topLeftPos, bottomRightPos - topLeftPos};
91 }
92
93 TGUI_IGNORE_DEPRECATED_WARNINGS_START
94 getBackend()->openVirtualKeyboard(inputRect);
95 TGUI_IGNORE_DEPRECATED_WARNINGS_END
96 }
97#endif
99
100 TGUI_NODISCARD inline bool isShiftPressed(const Event::KeyEvent& event)
101 {
102 return event.shift;
103 }
104
106#ifndef TGUI_REMOVE_DEPRECATED_CODE
107 TGUI_DEPRECATED("Use isShiftPressed(gui) instead") TGUI_NODISCARD inline bool isShiftPressed()
108 {
109 TGUI_IGNORE_DEPRECATED_WARNINGS_START
110 return getBackend()->isKeyboardModifierPressed(Event::KeyModifier::Shift);
111 TGUI_IGNORE_DEPRECATED_WARNINGS_END
112 }
113#endif
115
116 TGUI_NODISCARD inline bool isShiftPressed(const BackendGui* gui)
117 {
118 return gui->isKeyboardModifierPressed(Event::KeyModifier::Shift);
119 }
120
122#ifndef TGUI_REMOVE_DEPRECATED_CODE
123 TGUI_DEPRECATED("Use BackendGui::stopTextInput instead") inline void closeVirtualKeyboard()
124 {
125 TGUI_IGNORE_DEPRECATED_WARNINGS_START
126 getBackend()->closeVirtualKeyboard();
127 TGUI_IGNORE_DEPRECATED_WARNINGS_END
128 }
129#endif
131
132 TGUI_NODISCARD inline bool isMultiselectModifierPressed(const Event::KeyEvent& event)
133 {
134#ifdef TGUI_SYSTEM_MACOS
135 return event.system;
136#else
137 return event.control;
138#endif
139 }
140
142#ifndef TGUI_REMOVE_DEPRECATED_CODE
143 TGUI_DEPRECATED("Use isMultiselectModifierPressed(gui) instead") TGUI_NODISCARD inline bool isMultiselectModifierPressed()
144 {
145 TGUI_IGNORE_DEPRECATED_WARNINGS_START
146#ifdef TGUI_SYSTEM_MACOS
147 return getBackend()->isKeyboardModifierPressed(Event::KeyModifier::System);
148#else
149 return getBackend()->isKeyboardModifierPressed(Event::KeyModifier::Control);
150#endif
151 TGUI_IGNORE_DEPRECATED_WARNINGS_END
152 }
153#endif
155
156 TGUI_NODISCARD inline bool isMultiselectModifierPressed(const BackendGui* gui)
157 {
158#ifdef TGUI_SYSTEM_MACOS
159 return gui->isKeyboardModifierPressed(Event::KeyModifier::System);
160#else
161 return gui->isKeyboardModifierPressed(Event::KeyModifier::Control);
162#endif
163 }
164
166
167 TGUI_NODISCARD inline bool isKeyPressCopy(const Event::KeyEvent& event)
168 {
169#ifdef TGUI_SYSTEM_MACOS
170 return (event.code == Event::KeyboardKey::C) && !event.control && !event.alt && !event.shift && event.system;
171#else
172 return (event.code == Event::KeyboardKey::C) && event.control && !event.alt && !event.shift && !event.system;
173#endif
174 }
175
177
178 TGUI_NODISCARD inline bool isKeyPressCut(const Event::KeyEvent& event)
179 {
180#ifdef TGUI_SYSTEM_MACOS
181 return (event.code == Event::KeyboardKey::X) && !event.control && !event.alt && !event.shift && event.system;
182#else
183 return (event.code == Event::KeyboardKey::X) && event.control && !event.alt && !event.shift && !event.system;
184#endif
185 }
186
188
189 TGUI_NODISCARD inline bool isKeyPressPaste(const Event::KeyEvent& event)
190 {
191#ifdef TGUI_SYSTEM_MACOS
192 return (event.code == Event::KeyboardKey::V) && !event.control && !event.alt && !event.shift && event.system;
193#else
194 return (event.code == Event::KeyboardKey::V) && event.control && !event.alt && !event.shift && !event.system;
195#endif
196 }
197
199
200 TGUI_NODISCARD inline bool isKeyPressSelectAll(const Event::KeyEvent& event)
201 {
202#ifdef TGUI_SYSTEM_MACOS
203 return (event.code == Event::KeyboardKey::A) && !event.control && !event.alt && !event.shift && event.system;
204#else
205 return (event.code == Event::KeyboardKey::A) && event.control && !event.alt && !event.shift && !event.system;
206#endif
207 }
208
210
211 TGUI_NODISCARD inline bool isKeyPressMoveCaretLeft(const Event::KeyEvent& event)
212 {
213 return (event.code == Event::KeyboardKey::Left) && !event.control && !event.alt && !event.system;
214 }
215
217
218 TGUI_NODISCARD inline bool isKeyPressMoveCaretRight(const Event::KeyEvent& event)
219 {
220 return (event.code == Event::KeyboardKey::Right) && !event.control && !event.alt && !event.system;
221 }
222
224
225 TGUI_NODISCARD inline bool isKeyPressMoveCaretWordBegin(const Event::KeyEvent& event)
226 {
227#ifdef TGUI_SYSTEM_MACOS
228 return (event.code == Event::KeyboardKey::Left) && !event.control && event.alt && !event.system;
229#else
230 return (event.code == Event::KeyboardKey::Left) && event.control && !event.alt && !event.system;
231#endif
232 }
233
235
236 TGUI_NODISCARD inline bool isKeyPressMoveCaretWordEnd(const Event::KeyEvent& event)
237 {
238#ifdef TGUI_SYSTEM_MACOS
239 return (event.code == Event::KeyboardKey::Right) && !event.control && event.alt && !event.system;
240#else
241 return (event.code == Event::KeyboardKey::Right) && event.control && !event.alt && !event.system;
242#endif
243 }
244
246
247 TGUI_NODISCARD inline bool isKeyPressMoveCaretUp(const Event::KeyEvent& event)
248 {
249#ifdef TGUI_SYSTEM_MACOS
250 // Option+UpArrow should actually move to the beginning of the paragraph (or the previous one), but we don't support this
251 return (event.code == Event::KeyboardKey::Up) && !event.control && !event.system;
252#else
253 return (event.code == Event::KeyboardKey::Up) && !event.alt && !event.system;
254#endif
255 }
256
258
259 TGUI_NODISCARD inline bool isKeyPressMoveCaretDown(const Event::KeyEvent& event)
260 {
261#ifdef TGUI_SYSTEM_MACOS
262 // Option+DownArrow should actually move to the end of the paragraph (or the next one), but we don't support this
263 return (event.code == Event::KeyboardKey::Down) && !event.control && !event.system;
264#else
265 return (event.code == Event::KeyboardKey::Down) && !event.alt && !event.system;
266#endif
267 }
268
270
271 TGUI_NODISCARD inline bool isKeyPressMoveCaretLineStart(const Event::KeyEvent& event)
272 {
273#ifdef TGUI_SYSTEM_MACOS
274 if ((event.code == Event::KeyboardKey::Left) && !event.control && !event.alt && event.system)
275 return true;
276#endif
277 return (event.code == Event::KeyboardKey::Home) && !event.control && !event.alt && !event.system;
278 }
279
281
282 TGUI_NODISCARD inline bool isKeyPressMoveCaretLineEnd(const Event::KeyEvent& event)
283 {
284#ifdef TGUI_SYSTEM_MACOS
285 if ((event.code == Event::KeyboardKey::Right) && !event.control && !event.alt && event.system)
286 return true;
287#endif
288 return (event.code == Event::KeyboardKey::End) && !event.control && !event.alt && !event.system;
289 }
290
292
293 TGUI_NODISCARD inline bool isKeyPressMoveCaretDocumentBegin(const Event::KeyEvent& event)
294 {
295#ifdef TGUI_SYSTEM_MACOS
296 return ((event.code == Event::KeyboardKey::Up) && !event.control && !event.alt && event.system)
297 || ((event.code == Event::KeyboardKey::Home) && !event.control && !event.alt && event.system);
298#else
299 return (event.code == Event::KeyboardKey::Home) && event.control && !event.alt && !event.system;
300#endif
301 }
302
304
305 TGUI_NODISCARD inline bool isKeyPressMoveCaretDocumentEnd(const Event::KeyEvent& event)
306 {
307#ifdef TGUI_SYSTEM_MACOS
308 return ((event.code == Event::KeyboardKey::Down) && !event.control && !event.alt && event.system)
309 || ((event.code == Event::KeyboardKey::End) && !event.control && !event.alt && event.system);
310#else
311 return (event.code == Event::KeyboardKey::End) && event.control && !event.alt && !event.system;
312#endif
313 }
314
316 }
317}
318
320
321#endif // TGUI_KEYBOARD_HPP
322
Namespace that contains all TGUI functions and classes.
Definition AbsoluteOrRelativeValue.hpp:38
TGUI_API std::shared_ptr< Backend > getBackend()
Returns the global backend.