Quantcast
Channel: Android Mode - Processing 2.x and 3.x Forum
Viewing all articles
Browse latest Browse all 941

Getting the raw MotionEvent

$
0
0

Just posting this if someone else is battling with this. I needed to get the raw events from Android so I could implement Stylus commands for drawing - this worked before when the sketch was extended from the Activity - now that the sketch extends from PApplet, the dispatchTouchEvent(MotionEvent) method might not work, or at least I didn't find a safe way to get it working.

So, to grab the events, use the PApplet's own method nativeMotionEvent(MotionEvent):

@Override
protected void nativeMotionEvent (MotionEvent event) {

     // You can now catch the events and parse them
}

Example:

@Override
protected void nativeMotionEvent (MotionEvent event) {

    // InputNode is a custom class that just stores the values
    InputNode node;
    
    int pointerType = event.getToolType(0);


    int actionType = event.getActionMasked(); 

    if (actionType == MotionEvent.ACTION_DOWN)
    {
        println("Input_Android: Pointer is down, checking GUI hit.");
        gui.GUI_Hit(event.getX(), event.getY());
    }



    int historySize = event.getHistorySize(); 

    for (int i = 0; i < historySize - 1; i++)
    {
        if (historySize > 0)
        {
            node = new InputNode(); 
            node.x = event.getHistoricalX(0, i);
            node.y = event.getHistoricalY(0, i);
            node.pressure = event.getHistoricalPressure(0, i);
            node.size = event.getHistoricalSize(0, i);
            node.pointerAction = event.getActionMasked();
            node.pointerType = pointerType;

            inputModifier.Process(node);
        }
    }



    node = new InputNode(); 
    
    pointerType = event.getToolType(0);

    // MotionEvent.POINTER_TYPE_UNKNOWN = 0;
    // MotionEvent.POINTER_TYPE_FINGER  = 1;
    // MotionEvent.POINTER_TYPE_STYLUS  = 2;
    // MotionEvent.POINTER_TYPE_MOUSE   = 3;
    // MotionEvent.POINTER_TYPE_ERASER  = 4;

    node.x = event.getX(0);
    node.y = event.getY(0);
    node.pressure = event.getPressure(0);
    node.size = event.getSize(0);
    node.pointerAction = event.getActionMasked();
    node.pointerType = pointerType;

    inputModifier.Process(node);

}

Viewing all articles
Browse latest Browse all 941

Trending Articles