Monday, April 17, 2006

Stylus Event and Key Events

This is how you can read stylus events, i.e. get the x,y coordinates of where the pointer is pressed and how to create a key event, i.e. read what button the user has pressed:

In order to draw stuff on screen you'll either use Canvas, LWT or GameCanvas. As LWT and GameCanvas both extend Canvas, you can just implement a key and stylus listener.

Just implement the following methods in your class that draws stuff on screen, i.e. extends an interface mentioned above: (this is an extract from our code, you can implement your own handling)

//this method will be called if the stylus touches the screen at x,y:
protected void pointerPressed(int x, int y) {
//do something with the received x,y coordinates
current.actionPerformed(new Message(Message.STYLUS, x,y));
}

//this method will be called if the user presses a key
protected void keyPressed(int keyState){

//determine whether the input is something we can use or not.
boolean flag = true;

switch(keyState){
case -3:
keyState = LEFT_PRESSED;
break;
case -4:
keyState = RIGHT_PRESSED;
break;
case -1:
keyState = UP_PRESSED;
break;
case -2:
keyState = DOWN_PRESSED;
break;
case -5:
keyState = FIRE_PRESSED;
break;
default:
flag = false;
}
//if the data is good, perform the according action
if(flag){
current.actionPerformed(new Message(Message.JOYSTICK, keyState));
}
}

0 Comments:

Post a Comment

<< Home