mirror of
https://github.com/scratchfoundation/bgfx.git
synced 2024-11-28 10:35:43 -05:00
Added char events for entry-sdl.
This commit is contained in:
parent
205fa041e9
commit
9ea92d4a47
3 changed files with 65 additions and 0 deletions
|
@ -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) )
|
||||
|
|
|
@ -56,6 +56,8 @@ namespace entry
|
|||
|
||||
int main(int _argc, char** _argv);
|
||||
|
||||
char keyToAscii(Key::Enum _key, uint8_t _modifiers);
|
||||
|
||||
struct Event
|
||||
{
|
||||
enum Enum
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Reference in a new issue