TGUI  1.3.0
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
26#ifndef TGUI_KEYBOARD_HPP
27#define TGUI_KEYBOARD_HPP
28
30
31#include <TGUI/Config.hpp>
32#include <TGUI/Event.hpp>
33#include <TGUI/Container.hpp>
34#include <TGUI/Widgets/ScrollablePanel.hpp>
35#include <TGUI/Backend/Window/Backend.hpp>
36#include <TGUI/Backend/Window/BackendGui.hpp>
37
39
40namespace tgui
41{
42 namespace keyboard
43 {
45#ifndef TGUI_REMOVE_DEPRECATED_CODE
46 TGUI_DEPRECATED("Use BackendGui::startTextInput instead") inline void openVirtualKeyboard(const Widget* requestingWidget, FloatRect inputRect)
47 {
48 const Widget* widget = requestingWidget;
49 while (widget)
50 {
51 const bool defaultOrigin = (widget->getOrigin().x == 0) && (widget->getOrigin().y == 0);
52 const bool scaledOrRotated = (widget->getScale().x != 1) || (widget->getScale().y != 1) || (widget->getRotation() != 0);
53 if (defaultOrigin && !scaledOrRotated)
54 inputRect.setPosition(inputRect.getPosition() + widget->getPosition());
55 else
56 {
57 const Vector2f origin{widget->getOrigin().x * widget->getSize().x, widget->getOrigin().y * widget->getSize().y};
58 if (!scaledOrRotated)
59 inputRect.setPosition(inputRect.getPosition() + widget->getPosition() - origin);
60 else
61 {
62 const Vector2f rotOrigin{widget->getRotationOrigin().x * widget->getSize().x, widget->getRotationOrigin().y * widget->getSize().y};
63 const Vector2f scaleOrigin{widget->getScaleOrigin().x * widget->getSize().x, widget->getScaleOrigin().y * widget->getSize().y};
64
65 Transform transform;
66 transform.translate(widget->getPosition() - origin);
67 transform.rotate(widget->getRotation(), rotOrigin);
68 transform.scale(widget->getScale(), scaleOrigin);
69 inputRect = transform.transformRect(inputRect);
70 }
71 }
72
73 const Container* parent = widget->getParent();
74 if (parent)
75 {
76 inputRect.setPosition(inputRect.getPosition() + parent->getChildWidgetsOffset());
77
78 const ScrollablePanel* panel = dynamic_cast<const ScrollablePanel*>(parent);
79 if (panel)
80 inputRect.setPosition(inputRect.getPosition() - panel->getContentOffset());
81 }
82
83 widget = parent;
84 }
85
86 const auto gui = requestingWidget->getParentGui();
87 if (gui)
88 {
89 const Vector2f topLeftPos = gui->mapCoordsToPixel(inputRect.getPosition());
90 const Vector2f bottomRightPos = gui->mapCoordsToPixel(inputRect.getPosition() + inputRect.getSize());
91 inputRect = {topLeftPos, bottomRightPos - topLeftPos};
92 }
93
94 TGUI_IGNORE_DEPRECATED_WARNINGS_START
95 getBackend()->openVirtualKeyboard(inputRect);
96 TGUI_IGNORE_DEPRECATED_WARNINGS_END
97 }
98#endif
100
101 TGUI_NODISCARD inline bool isShiftPressed(const Event::KeyEvent& event)
102 {
103 return event.shift;
104 }
105
107
108 TGUI_NODISCARD inline bool isShiftPressed()
109 {
110 return getBackend()->isKeyboardModifierPressed(Event::KeyModifier::Shift);
111 }
112
114#ifndef TGUI_REMOVE_DEPRECATED_CODE
115 TGUI_DEPRECATED("Use BackendGui::stopTextInput instead") inline void closeVirtualKeyboard()
116 {
117 TGUI_IGNORE_DEPRECATED_WARNINGS_START
118 getBackend()->closeVirtualKeyboard();
119 TGUI_IGNORE_DEPRECATED_WARNINGS_END
120 }
121#endif
123
124 TGUI_NODISCARD inline bool isMultiselectModifierPressed(const Event::KeyEvent& event)
125 {
126#ifdef TGUI_SYSTEM_MACOS
127 return event.system;
128#else
129 return event.control;
130#endif
131 }
132
134
135 TGUI_NODISCARD inline bool isMultiselectModifierPressed()
136 {
137#ifdef TGUI_SYSTEM_MACOS
138 return getBackend()->isKeyboardModifierPressed(Event::KeyModifier::System);
139#else
140 return getBackend()->isKeyboardModifierPressed(Event::KeyModifier::Control);
141#endif
142 }
143
145
146 TGUI_NODISCARD inline bool isKeyPressCopy(const Event::KeyEvent& event)
147 {
148#ifdef TGUI_SYSTEM_MACOS
149 return (event.code == Event::KeyboardKey::C) && !event.control && !event.alt && !event.shift && event.system;
150#else
151 return (event.code == Event::KeyboardKey::C) && event.control && !event.alt && !event.shift && !event.system;
152#endif
153 }
154
156
157 TGUI_NODISCARD inline bool isKeyPressCut(const Event::KeyEvent& event)
158 {
159#ifdef TGUI_SYSTEM_MACOS
160 return (event.code == Event::KeyboardKey::X) && !event.control && !event.alt && !event.shift && event.system;
161#else
162 return (event.code == Event::KeyboardKey::X) && event.control && !event.alt && !event.shift && !event.system;
163#endif
164 }
165
167
168 TGUI_NODISCARD inline bool isKeyPressPaste(const Event::KeyEvent& event)
169 {
170#ifdef TGUI_SYSTEM_MACOS
171 return (event.code == Event::KeyboardKey::V) && !event.control && !event.alt && !event.shift && event.system;
172#else
173 return (event.code == Event::KeyboardKey::V) && event.control && !event.alt && !event.shift && !event.system;
174#endif
175 }
176
178
179 TGUI_NODISCARD inline bool isKeyPressSelectAll(const Event::KeyEvent& event)
180 {
181#ifdef TGUI_SYSTEM_MACOS
182 return (event.code == Event::KeyboardKey::A) && !event.control && !event.alt && !event.shift && event.system;
183#else
184 return (event.code == Event::KeyboardKey::A) && event.control && !event.alt && !event.shift && !event.system;
185#endif
186 }
187
189
190 TGUI_NODISCARD inline bool isKeyPressMoveCaretLeft(const Event::KeyEvent& event)
191 {
192 return (event.code == Event::KeyboardKey::Left) && !event.control && !event.alt && !event.system;
193 }
194
196
197 TGUI_NODISCARD inline bool isKeyPressMoveCaretRight(const Event::KeyEvent& event)
198 {
199 return (event.code == Event::KeyboardKey::Right) && !event.control && !event.alt && !event.system;
200 }
201
203
204 TGUI_NODISCARD inline bool isKeyPressMoveCaretWordBegin(const Event::KeyEvent& event)
205 {
206#ifdef TGUI_SYSTEM_MACOS
207 return (event.code == Event::KeyboardKey::Left) && !event.control && event.alt && !event.system;
208#else
209 return (event.code == Event::KeyboardKey::Left) && event.control && !event.alt && !event.system;
210#endif
211 }
212
214
215 TGUI_NODISCARD inline bool isKeyPressMoveCaretWordEnd(const Event::KeyEvent& event)
216 {
217#ifdef TGUI_SYSTEM_MACOS
218 return (event.code == Event::KeyboardKey::Right) && !event.control && event.alt && !event.system;
219#else
220 return (event.code == Event::KeyboardKey::Right) && event.control && !event.alt && !event.system;
221#endif
222 }
223
225
226 TGUI_NODISCARD inline bool isKeyPressMoveCaretUp(const Event::KeyEvent& event)
227 {
228#ifdef TGUI_SYSTEM_MACOS
229 // Option+UpArrow should actually move to the beginning of the paragraph (or the previous one), but we don't support this
230 return (event.code == Event::KeyboardKey::Up) && !event.control && !event.system;
231#else
232 return (event.code == Event::KeyboardKey::Up) && !event.alt && !event.system;
233#endif
234 }
235
237
238 TGUI_NODISCARD inline bool isKeyPressMoveCaretDown(const Event::KeyEvent& event)
239 {
240#ifdef TGUI_SYSTEM_MACOS
241 // Option+DownArrow should actually move to the end of the paragraph (or the next one), but we don't support this
242 return (event.code == Event::KeyboardKey::Down) && !event.control && !event.system;
243#else
244 return (event.code == Event::KeyboardKey::Down) && !event.alt && !event.system;
245#endif
246 }
247
249
250 TGUI_NODISCARD inline bool isKeyPressMoveCaretLineStart(const Event::KeyEvent& event)
251 {
252#ifdef TGUI_SYSTEM_MACOS
253 if ((event.code == Event::KeyboardKey::Left) && !event.control && !event.alt && event.system)
254 return true;
255#endif
256 return (event.code == Event::KeyboardKey::Home) && !event.control && !event.alt && !event.system;
257 }
258
260
261 TGUI_NODISCARD inline bool isKeyPressMoveCaretLineEnd(const Event::KeyEvent& event)
262 {
263#ifdef TGUI_SYSTEM_MACOS
264 if ((event.code == Event::KeyboardKey::Right) && !event.control && !event.alt && event.system)
265 return true;
266#endif
267 return (event.code == Event::KeyboardKey::End) && !event.control && !event.alt && !event.system;
268 }
269
271
272 TGUI_NODISCARD inline bool isKeyPressMoveCaretDocumentBegin(const Event::KeyEvent& event)
273 {
274#ifdef TGUI_SYSTEM_MACOS
275 return ((event.code == Event::KeyboardKey::Up) && !event.control && !event.alt && event.system)
276 || ((event.code == Event::KeyboardKey::Home) && !event.control && !event.alt && event.system);
277#else
278 return (event.code == Event::KeyboardKey::Home) && event.control && !event.alt && !event.system;
279#endif
280 }
281
283
284 TGUI_NODISCARD inline bool isKeyPressMoveCaretDocumentEnd(const Event::KeyEvent& event)
285 {
286#ifdef TGUI_SYSTEM_MACOS
287 return ((event.code == Event::KeyboardKey::Down) && !event.control && !event.alt && event.system)
288 || ((event.code == Event::KeyboardKey::End) && !event.control && !event.alt && event.system);
289#else
290 return (event.code == Event::KeyboardKey::End) && event.control && !event.alt && !event.system;
291#endif
292 }
293
295 }
296}
297
299
300#endif // TGUI_KEYBOARD_HPP
301
T x
X coordinate of the vector.
Definition Vector2.hpp:138
Namespace that contains all TGUI functions and classes.
Definition AbsoluteOrRelativeValue.hpp:39
TGUI_API std::shared_ptr< Backend > getBackend()
Returns the global backend.