You Are Here:

Community: Developer Discussion Boards

#1 Old a question about keyPressed and keyReleased - 2003-04-22, 11:17

Join Date: Mar 2003
Posts: 37
nodevelop
Offline
Registered User
hi

I used this code to move an image over the display:
import javax.microedition.lcdui.*;

public class Displayable1 extends Canvas implements CommandListener {

private Image image;
private int x = 50;
private int y = 50;
private int speed = 5;
Command exit;

public Displayable1() {
try {
image = Image.createImage("/pressRelease/space1on.png");
}
catch (Exception e) {}

exit = new Command("Exit", Command.EXIT, 1);
addCommand(exit);
setCommandListener(this);
}
protected void paint(Graphics g) {
int anchor = Graphics.LEFT | Graphics.TOP;
g.drawImage(image, x, y, anchor);
}
private void moveImage(int direction) {
switch (direction) {
case 1: x+=speed;
break;
case 2: y+=speed;
break;
case 3: x-=speed;
break;
case 4: y-=speed;
break;
}
repaint();
}
public void commandAction(Command command, Displayable displayable) {
}
protected void keyPressed(int keyCode) {
int action = getGameAction(keyCode);
switch (action) {
case Canvas.RIGHT:
moveImage(1);
break;
case Canvas.LEFT:
moveImage(3);
break;
case Canvas.UP:
moveImage(4);
break;
case Canvas.DOWN:
moveImage(2);
break;
}
}
}
how can I add the keyReleased method?
what I need is this: a key is pressed, the image moves and still moving until the key is released.
so first keyPressed, then movement starts, and then when the keyReleased condition is true stops.
can you help me with a code sample?

thank you!
eml
Reply With Quote

#2 Old hope this may help you - 2003-04-23, 17:42

Join Date: Mar 2003
Posts: 1
fareast1976
Offline
Registered User
I suggest you can put 4 boolean variables in your program, and in keyPressed method, a boolean variable set true when a corresponding key was pressed, and in keyReleased method, a boolean variable set false when a corresponding key was released. this may be little difficult, because you must put a thread in your program, it control image moving, when a boolean varible is true, image may move to corresponding direction.

my english is poor, hope you can understand and this may help you.
Reply With Quote

#3 Old previous answer is correct... - 2003-05-08, 15:02

Join Date: May 2003
Posts: 1
auval1
Offline
Registered User
You should use thread anyway, not only for getting the keyRelease event, if you want a smooth animation.

The key latching behavior that you want could be implemented using one int mask (using bitwise operations) instead of four booleans, but that's not that important.

by the way, the behavior you need is part of the MIDP 2.0 spec
It's in GameCanvas: suppressKeyEvents.
Reply With Quote

#4 Old 2003-05-09, 13:12

Join Date: Mar 2003
Posts: 37
nodevelop
Offline
Registered User
I hope that MIDP 2 will be supported by nokia phones soons.

thanks for the answers

eml
Reply With Quote

#5 Old 2003-05-28, 15:50

Join Date: Mar 2003
Posts: 9
lacosebo
Offline
Registered User
Hi her is some code example width rectangle

import javax.microedition.lcdui.*;

public class Test5Canvas extends Canvas implements Runnable
{
static final int DELAY = 150;
private Test5Midlet midlet;
private volatile Thread animationThread = null;
private int x,y;
private int maxx,maxy;

private boolean up,down,left,right=false;


public Test5Canvas(Test5Midlet midlet)
{
this.midlet=midlet;
maxx=getWidth();
maxy=getHeight();
x=maxx/2;
y=maxy/2;
}

public void paint(Graphics g)
{
g.setColor(255,255,255);
g.fillRect(0,0,maxx,maxy);
g.setColor(255,0,0);
g.fillRect(x,y,10,10);
}


public synchronized void start()
{
animationThread = new Thread(this);
animationThread.start();
}

public synchronized void stop()
{
animationThread = null;
}

public void run()
{
Thread currentThread = Thread.currentThread();

try
{
while (currentThread == animationThread)
{
long startTime = System.currentTimeMillis();
KeyRepeated();
repaint();
serviceRepaints();
long delay = System.currentTimeMillis() - startTime;
if(delay < DELAY)
{
synchronized (this)
{
wait(DELAY-delay);
}
}
else
{
currentThread.yield();
}
}
}
catch(Exception e)
{
}
}

public void keyPressed(int keyCode)
{
if(keyCode<0)
midlet.exit();
else
{
switch(keyCode)
{
case KEY_NUM2: up = true;break;
case KEY_NUM8: down = true;break;
case KEY_NUM4: left = true;break;
case KEY_NUM6: right = true;break;
}
}
}

public void keyReleased(int keyCode)
{
switch(keyCode)
{
case KEY_NUM2: up = false;break;
case KEY_NUM8: down = false;break;
case KEY_NUM4: left = false;break;
case KEY_NUM6: right = false;break;
}
}

public void KeyRepeated()
{
if(up){if(y>1)y--;}
if(down){if(y<maxy-15)y++;}
if(left){if(x>1)x--;}
if(right){if(x<maxx-15)x++;}
}

}
Reply With Quote
Reply « Previous Thread | Next Thread »
Display Modes
Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules

You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Forum Jump

Rate This

 
Bookmark this page: DeliciousDiggFacebookGoogleYahooStumbleUponRedditDiigoTechnocratiTwitter  Share this page Share this page Print this Page Print this page Invite a friend Invite a friend
京ICP备05048969号    Email Newsletters Press Terms & Conditions Privacy Policy Sitemap Contact Us © 2009 Nokia 
RDF Facets: qdcZidentifierQSxhttpE3aE2fE2fdiscussionE2eforumE2enokiaE2ecomhttpE3aE2fE2fdiscussionE2eforumE2enokiaE2ecomE2fforumE2fshowthreadE2ephpE3ftE3d18645X qdcZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qdcZtypeQUqfntypeZCommunityContentQ qdcZtypeQUqfntypeZE44iscussionQ qdcZtypeQUqfntypeZE44iscussionContentQ qdcZtypeQUqfntypeZE52esourceQ qdcZtypeQUqfntypeZWebpageQ qdcZtypeQUqmarsZManagedE52esourceQ qdcZtypeQUqwebZInformationE52esourceQ qdcZtypeQUqwebZPageQ qdcZtypeQUqwebZE52esourceQ qdcZtypeQUqrdfsZE52esourceQ qfnZtopicQUqfnTopicZentertainmentQ qfnZtopicQUqfnTopicZj2meQ qfnZtopicQUqfnTopicZjavaQ qfnZtopicQUqfnTopicZmediaQ qfnZtypeQUqfntypeZCommunityContentQ qfnZtypeQUqfntypeZE44iscussionQ qfnZtypeQUqfntypeZE44iscussionContentQ qfnZtypeQUqfntypeZE52esourceQ qfnZtypeQUqfntypeZWebpageQ qmarsZlanguageQUxhttpE3aE2fE2fswE2enokiaE2ecomE2flanguageE2d1E2fenX qrdfZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qrdfZtypeQUqfntypeZCommunityContentQ qrdfZtypeQUqfntypeZE44iscussionQ qrdfZtypeQUqfntypeZE44iscussionContentQ qrdfZtypeQUqfntypeZE52esourceQ qrdfZtypeQUqfntypeZWebpageQ qrdfZtypeQUqmarsZManagedE52esourceQ qrdfZtypeQUqwebZInformationE52esourceQ qrdfZtypeQUqwebZPageQ qrdfZtypeQUqwebZE52esourceQ qrdfZtypeQUqrdfsZE52esourceQ