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 touch events


Let's intercept touch events in DroidBlaster:

  1. In the same way that we created ActivityHandler to process application events in Chapter 5, Writing a Fully Native Application, create jni/InputHandler.hpp to process input events. The input API is declared in android/input.h. Create onTouchEvent() to handle touch events. These events are packaged in an AInputEvent structure. Other input peripherals will be described later in this chapter:

    #ifndef _PACKT_INPUTHANDLER_HPP_
    #define _PACKT_INPUTHANDLER_HPP_
    
    #include <android/input.h>
    
    class InputHandler {
    public:
        virtual ~InputHandler() {};
    
        virtual bool onTouchEvent(AInputEvent* pEvent) = 0;
    };
    #endif
  2. Modify the jni/EventLoop.hpp header file to include and handle an InputHandler instance.

    In a similar way, to activity events, define an internal method processInputEvent(), which is triggered by a static callback callback_input():

    ...
    #include "ActivityHandler.hpp"
    #include "InputHandler.hpp"
    
    #include...