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