Dashu
Dashu

Reputation: 33

How to get mouse pointer position when Android is not in ACTION_DOWN state

When a mouse is plugged into Android device(no touch screen), there will be a classic mouse cursor shown on the screen, we can control the mouse to move cursor and click. We can get the current pointer location when mouse click(equal to "tap screen", ACTION_DOWN) by following code:

@Override
public boolean onTouchEvent(MotionEvent event) {
    int x = (int)event.getRawX();
    int y = (int)event.getRawY();
    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
        //do something
        case MotionEvent.ACTION_MOVE:
        //do something
        case MotionEvent.ACTION_UP:
        //do something
    }
 return true;
}

But we get the pointer position when only mouse click action happen, can not get the mouse pointer position when it is moving on the screen by moving real mouse.

So my question is, how to get the mouse pointer location in this condition(NOT click mouse, just moving)?

Upvotes: 1

Views: 2818

Answers (1)

icodestuff
icodestuff

Reputation: 350

You want your activity to implement GestureDetector.OnGestureListener implement onGenericMotionEvent or have your view override it:

@Override
public boolean onGenericMotionEvent(MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_HOVER_MOVE) {
         // Do something
         // Maybe also check for ACTION_HOVER_ENTER and ACTION_HOVER_EXIT events
         // in which case you'll want to use a switch. 
    }
    return true;
}

Upvotes: 2

Related Questions