I’m curious because I want to modify it to be slightly faster
It’s implemented in the “backend” for each target. For example, the HTML5Application class listens for a native JS KeyboardEvent:
For native targets (C++, Neko, HashLink), a message is sent from C++ to Haxe, and the NativeApplication backend dispatches Lime’s onKeyDown/onKeyUp:
So how exactly is KeyEventType
made?
ProcessKeyEvent() in SDLApplication.cpp is where to find C++ code for key events on native targets:
So I managed to potentially optimize it
void SDLApplication::ProcessKeyEvent(SDL_Event* event) {
if (KeyEvent::callback) {
switch (event->type) {
case SDL_KEYDOWN: keyEvent.type = KEY_DOWN; break;
case SDL_KEYUP: keyEvent.type = KEY_UP; break;
}
keyEvent.keyCode = event->key.keysym.sym;
keyEvent.modifier = event->key.keysym.mod;
keyEvent.windowID = event->key.windowID;
if (keyEvent.type == KEY_DOWN) {
// Key modifier lookup table
const std::unordered_map<SDL_Keycode, Uint16> modifierMap = {
{SDLK_CAPSLOCK, KMOD_CAPS},
{SDLK_LALT, KMOD_LALT},
{SDLK_LCTRL, KMOD_LCTRL},
{SDLK_LGUI, KMOD_LGUI},
{SDLK_LSHIFT, KMOD_LSHIFT},
{SDLK_MODE, KMOD_MODE},
{SDLK_NUMLOCKCLEAR, KMOD_NUM},
{SDLK_RALT, KMOD_RALT},
{SDLK_RCTRL, KMOD_RCTRL},
{SDLK_RGUI, KMOD_RGUI},
{SDLK_RSHIFT, KMOD_RSHIFT}
};
// Check if the key is a modifier and update the modifier flags
auto it = modifierMap.find(keyEvent.keyCode);
if (it != modifierMap.end()) {
keyEvent.modifier |= it->second;
}
}
KeyEvent::Dispatch(&keyEvent);
}
}
It uses an unordered_map
(cpp) instead of just using redundant if checks btw