Book Image

Android NDK: Beginner's Guide

By : Sylvain Ratabouil
Book Image

Android NDK: Beginner's Guide

By: Sylvain Ratabouil

Overview of this book

Table of Contents (18 chapters)
Android NDK Beginner's Guide Second Edition
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Time for action – handling keyboard, D-Pad, and trackball events natively


Let's extend our new Input system with more event types:

  1. 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
  2. Update the method processInputEvent() inside the existing file jni/EventLoop.cpp to redirect the keyboard and trackball events to InputHandler.

    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 for KeyEvents...