Handling Stylus Events

Stylus events can be detected using the Canvas class, which provides developers with methods to handle pointer events:

• Canvas.hasPointerEvents() indicates whether the device supports pointer press and release events (e.g., when the user touches the screen with a stylus).

• Canvas.hasPointerMotionEvents() indicates whether the device supports pointer motion events (e.g., when the user drags the pointer).

• Canvas.pointerDragged(int x, int y) is called when the pointer is dragged.

• Canvas.pointerPressed(int x, int y) is called when the pointer is pressed.

• Canvas.pointerReleased(int x, int y) is called when the pointer is released.

The pointer events delivery methods are only called while the Canvas is visible on the screen. CustomItem also supports pointer events and the way to query for supported input methods is the getInteraction-Modes() method.

The following snippet of code demonstrates how to handle stylus events:

public class StylusSupportExamplet extends Canvas { // Members private final TouchPoint pressed = new TouchPoint(); private final TouchPoint released = new TouchPoint(); private final TouchPoint dragged = new TouchPoint();

// Stylus support

// Indicate if pointer and pointer motion events are supported public boolean supportsStylus() {

return this.hasPointerEvents() && this.hasPointerMotionEvents();

protected void pointerPressed(int x,

int y) {

pressed.x = x;

pressed.y = y;

// TODO: handle event accordingly

protected void pointerReleased(int x,

int y) {

released.x = x;

released.y = y;

// TODO: handle event accordingly

protected void pointerDragged(int x,

int y) {

dragged.x = x;

dragged.y = y;

// TODO: handle event accordingly

}

// Encapsulation of [x,y] point on the

screen

class TouchPoint {

int x, y;

public String toString(){

return "[" + x + "," + y + "]";

}

To ensure that a user can use your application with a touch screen, you first need to add code to handle stylus events to the relevant Canvas-based screens.

0 0

Post a comment

  • Receive news updates via email from this site