diff --git a/examples/common/entry/entry.cpp b/examples/common/entry/entry.cpp index 947d42046..1007787a0 100644 --- a/examples/common/entry/entry.cpp +++ b/examples/common/entry/entry.cpp @@ -34,6 +34,43 @@ namespace entry } #endif // ENTRY_CONFIG_IMPLEMENT_DEFAULT_ALLOCATOR + char keyToAscii(Key::Enum _key, uint8_t _modifiers) + { + const bool isAscii = (Key::Key0 <= _key && _key <= Key::KeyZ) + || (Key::Esc <= _key && _key <= Key::Minus); + if (!isAscii) + { + return '\0'; + } + + const bool isNumber = (Key::Key0 <= _key && _key <= Key::Key9); + if (isNumber) + { + return '0' + (_key - Key::Key0); + } + + const bool isChar = (Key::KeyA <= _key && _key <= Key::KeyZ); + if (isChar) + { + enum { ShiftMask = Modifier::LeftShift|Modifier::RightShift }; + + const bool shift = !!(_modifiers&ShiftMask); + return (shift ? 'A' : 'a') + (_key - Key::KeyA); + } + + switch (_key) + { + case Key::Esc: { return 0x1b; } break; + case Key::Return: { return 0x0d; } break; + case Key::Tab: { return 0x09; } break; + case Key::Space: { return 0xa0; } break; + case Key::Backspace: { return 0x08; } break; + case Key::Plus: { return 0x2b; } break; + case Key::Minus: { return 0x2d; } break; + default: { return '\0'; } break; + } + } + bool setOrToggle(uint32_t& _flags, const char* _name, uint32_t _bit, int _first, int _argc, char const* const* _argv) { if (0 == strcmp(_argv[_first], _name) ) diff --git a/examples/common/entry/entry_p.h b/examples/common/entry/entry_p.h index 39a289ed7..74d4756b5 100644 --- a/examples/common/entry/entry_p.h +++ b/examples/common/entry/entry_p.h @@ -56,6 +56,8 @@ namespace entry int main(int _argc, char** _argv); + char keyToAscii(Key::Enum _key, uint8_t _modifiers); + struct Event { enum Enum diff --git a/examples/common/entry/entry_sdl.cpp b/examples/common/entry/entry_sdl.cpp index 8d9b5b1c6..4aea10440 100644 --- a/examples/common/entry/entry_sdl.cpp +++ b/examples/common/entry/entry_sdl.cpp @@ -412,6 +412,32 @@ namespace entry break; case SDL_KEYDOWN: + { + const SDL_KeyboardEvent& kev = event.key; + WindowHandle handle = findHandle(kev.windowID); + if (isValid(handle) ) + { + uint8_t modifiers = translateKeyModifiers(kev.keysym.mod); + Key::Enum key = translateKey(kev.keysym.scancode); + + const uint8_t shiftMask = Modifier::LeftShift|Modifier::RightShift; + const bool nonShiftModifiers = (0 != (modifiers&(~shiftMask) ) ); + const bool isCharPressed = (Key::Key0 <= key && key <= Key::KeyZ) || (Key::Esc <= key && key <= Key::Minus); + const bool isText = isCharPressed && !nonShiftModifiers; + + if (isText) + { + uint8_t pressedChar[4]; + pressedChar[0] = keyToAscii(key, modifiers); + m_eventQueue.postCharEvent(handle, 1, pressedChar); + } + else + { + m_eventQueue.postKeyEvent(handle, key, modifiers, kev.state == SDL_PRESSED); + } + } + } + break; case SDL_KEYUP: { const SDL_KeyboardEvent& kev = event.key;