载入中...
搜索中...
未找到
Keyboard.cpp
浏览该文件的文档.
2
4#include "window/Window.h"
5
6#include <SDL2/SDL.h>
7#include <SDL2/SDL_keyboard.h>
8
9namespace eve::keyboard::sdl {
10
12 // Video subsystem owns the keyboard state / text input on most backends.
13 SDL_InitSubSystem(SDL_INIT_VIDEO);
15}
16
19 SDL_QuitSubSystem(SDL_INIT_VIDEO);
20}
21
22void Keyboard::setKeyRepeat(bool enable) {
23 keyRepeat_ = enable;
24}
25
27 return keyRepeat_;
28}
29
30bool Keyboard::isDown(const std::string& key) const {
31 return isDown(std::vector<std::string>{key});
32}
33
34bool Keyboard::isDown(const std::vector<std::string>& keys) const {
35 const Uint8* state = SDL_GetKeyboardState(nullptr);
36 if (!state) return false;
37
38 for (const auto& keyName : keys) {
39 SDL_Keycode key = SDL_GetKeyFromName(keyName.c_str());
40 if (key == SDLK_UNKNOWN) continue;
41 SDL_Scancode scancode = SDL_GetScancodeFromKey(key);
42 if (scancode != SDL_SCANCODE_UNKNOWN && state[scancode]) return true;
43 }
44 return false;
45}
46
47bool Keyboard::isScancodeDown(const std::string& scancode) const {
48 return isScancodeDown(std::vector<std::string>{scancode});
49}
50
51bool Keyboard::isScancodeDown(const std::vector<std::string>& scancodes) const {
52 const Uint8* state = SDL_GetKeyboardState(nullptr);
53 if (!state) return false;
54
55 for (const auto& scancodeName : scancodes) {
56 SDL_Scancode scancode = SDL_GetScancodeFromName(scancodeName.c_str());
57 if (scancode != SDL_SCANCODE_UNKNOWN && state[scancode]) return true;
58 }
59 return false;
60}
61
62std::string Keyboard::getKeyFromScancode(const std::string& scancode) const {
63 SDL_Scancode sdlScancode = SDL_GetScancodeFromName(scancode.c_str());
64 if (sdlScancode == SDL_SCANCODE_UNKNOWN) return {};
65 SDL_Keycode key = SDL_GetKeyFromScancode(sdlScancode);
66 if (key == SDLK_UNKNOWN) return {};
67 const char* keyName = SDL_GetKeyName(key);
68 return keyName ? keyName : std::string{};
69}
70
71std::string Keyboard::getScancodeFromKey(const std::string& key) const {
72 SDL_Keycode sdlKey = SDL_GetKeyFromName(key.c_str());
73 if (sdlKey == SDLK_UNKNOWN) return {};
74 SDL_Scancode scancode = SDL_GetScancodeFromKey(sdlKey);
75 if (scancode == SDL_SCANCODE_UNKNOWN) return {};
76 const char* scancodeName = SDL_GetScancodeName(scancode);
77 return scancodeName ? scancodeName : std::string{};
78}
79
80void Keyboard::setTextInput(bool enable) {
81 if (enable)
82 SDL_StartTextInput();
83 else
84 SDL_StopTextInput();
85}
86
87void Keyboard::setTextInput(bool enable, double x, double y, double w, double h) {
88 // SDL_SetTextInputRect expects window coords; API takes pixels.
89 auto* window = getModInst(window, Window);
90 if (window) {
91 window->DPIToWindowCoords(&x, &y);
92 window->DPIToWindowCoords(&w, &h);
93 }
94
95 SDL_Rect rect = {static_cast<int>(x), static_cast<int>(y), static_cast<int>(w),
96 static_cast<int>(h)};
97 SDL_SetTextInputRect(&rect);
98 setTextInput(enable);
99}
100
102 return SDL_IsTextInputActive() != SDL_FALSE;
103}
104
106 return SDL_HasScreenKeyboardSupport() != SDL_FALSE;
107}
108
109} // namespace eve::keyboard::sdl
float w
Definition AnimClip.cpp:738
float y
Definition AnimClip.cpp:738
float x
Definition AnimClip.cpp:738
std::uint32_t key
int h
#define getModInst(N, T)
Definition Module.h:38
void setKeyRepeat(bool enable) override
Whether held keys emit repeated keypressed events.
Definition Keyboard.cpp:22
bool hasTextInput() const override
True while text-input / IME is active.
Definition Keyboard.cpp:101
void setTextInput(bool enable) override
Enables or disables text-input / IME composition.
Definition Keyboard.cpp:80
Keyboard()
Constructs the SDL keyboard module.
Definition Keyboard.cpp:11
bool isScancodeDown(const std::string &scancode) const override
True if any of the named physical scancodes is currently down.
Definition Keyboard.cpp:47
~Keyboard() override
Releases keyboard backend state.
Definition Keyboard.cpp:17
std::string getScancodeFromKey(const std::string &key) const override
Physical scancode name for a layout-aware key (SDL names).
Definition Keyboard.cpp:71
bool hasScreenKeyboard() const override
True when the platform exposes an on-screen keyboard.
Definition Keyboard.cpp:105
std::string getKeyFromScancode(const std::string &scancode) const override
Layout-aware key name for a physical scancode (SDL names).
Definition Keyboard.cpp:62
bool hasKeyRepeat() const override
True when key-repeat events are enabled.
Definition Keyboard.cpp:26
bool isDown(const std::string &key) const override
True if any of the named keys is currently down.
Definition Keyboard.cpp:30
void bindKeyboardEventSinkOwner(eve::keyboard::Keyboard *owner) noexcept
Bind the live Keyboard module into the SDL event sink. @ownership Borrowed; cleared when the Keyboard...
Definition EventSink.cpp:69