Time for action – handling keyboard, D-Pad, and trackball events natively
Let's extend our new Input system with more event types:
Open
jni/InputHandler.hpp
and add the keyboard and trackball event handlers:#ifndef _PACKT_INPUTHANDLER_HPP_ #define _PACKT_INPUTHANDLER_HPP_ #include <android/input.h> class InputHandler { public: virtual ~InputHandler() {}; virtual bool onTouchEvent(AInputEvent* pEvent) = 0; virtual bool onKeyboardEvent(AInputEvent* pEvent) = 0; virtual bool onTrackballEvent(AInputEvent* pEvent) = 0; }; #endif
Update the method
processInputEvent()
inside the existing filejni/EventLoop.cpp
to redirect the keyboard and trackball events toInputHandler
.Trackballs and touch events are assimilated to motion events and can be discriminated according to their source. On the opposite side, key events are discriminated according to their type. Indeed, there exists two dedicated APIs for
MotionEvents
(the same for trackballs and touch events) and forKeyEvents...