Example #1
0
static bool _process_keys(bool isJoy, AInputEvent *event, CookedEventCallback callback) {
    if (AInputEvent_getType(event) == AINPUT_EVENT_TYPE_KEY) {
        int action = AKeyEvent_getAction(event);
        int code = _translate_keycode(AKeyEvent_getKeyCode(event));
        bool handled = code >= 0;
        if (code >= 0 && action == AKEY_EVENT_ACTION_DOWN) {
            _report_key_state(code, true, callback);
        } else if (code >= 0 && action == AKEY_EVENT_ACTION_UP) {
            _report_key_state(code, false, callback);
        }
        return handled;
    } else if (isJoy) {
        // use joystick axes to emulate directional key events (we could leave this
        // up to the platform, but joystick-to-dpad conversion doesn't work
        // on NDK on older devices,  so we implement manually for maximum compatibility)
        float x = AMotionEvent_getX(event, 0);
        float y = AMotionEvent_getY(event, 0);
        if (_getAxisValue) {
            // take the hat switches into account too, so that either the
            // regular axes or the hat axes can be used to navigate UIs
            x += _getAxisValue(event, AXIS_HAT_X, 0);
            y += _getAxisValue(event, AXIS_HAT_Y, 0);
            x = Clamp(x, -1.0f, 1.0f);
            y = Clamp(y, -1.0f, 1.0f);
        }
        _report_key_states_from_axes(x, y, callback);
        return true;
    }
}
Example #2
0
static uint8_t sdl_keyevent_to_keycode(const SDL_KeyboardEvent *ev)
{
    int keycode;

    keycode = ev->keysym.scancode;

    if (keycode < 9) {
        keycode = 0;
    } else if (keycode < 97) {
        keycode -= 8; /* just an offset */
    } else if (keycode < 212) {
        /* use conversion table */
        keycode = _translate_keycode(keycode - 97);
    } else {
        keycode = 0;
    }
    return keycode;
}