J2ME: can you clear the key pressed buffer on a cell?

I am having trouble with keypressed.
I am writing a cell phone game in MIDP 2.0. Basically its the standard GameCanvas setup, where you have a main game loop and it calls your input function. The input function then does:
int keyStates = getKeyStates();
keyprocessing
to get the states.
When I demo it on the color phone in WTK2.0, and tap the right arrow, and my input loop processes the right multiple times. My initial thought was that the loop ran too fast perhaps and it read the press several times, so I put in a delay before keyprocessing:
int keyStates = getKeyStates();
long time = System.currentTimeMillis();
if ((time - timeOfLastInput) < MIN_TIME_INPUT)
return;
keyprocessing
timeOfLastInput = System.currentTimeMillis();
Now, I have cut it down to only going in a direction once. However, if I press up, down, left, right, (in honor of Konami) My input goes as follow Up (delays MIN_TIME_INPUT) then Down, delays again, left, delays, right.
And so I am out of ideas.

haha, this is interesting... a month ago, i had the same problem, until i flash of intellect put me out of my misery...
rule 1: dont use the time delay. with the processor speed of the cell phones, with your delay, it will take a decade before the app runs...
heres how to do the coding to keep it to only 1 step at a time: ( i do it in the run method of the thread)
int originalKey = 0; // keeps the keystates once the execution completes once
int key = 0; // keeps the keystates for comparision
while(isRunning) {
    key = getKeyStates();
    if(key == originalKey) {
        originalKey = 0;
        continue;
    if( (key & LEFT_PRESSED) != 0) {
        // do what you want to do, or who you want to do
        originalKey = key;
// do the same for right pressed and etc etc... this will keep your code from executing too many timesheheh... i think i deserve a few dukes...

Similar Messages

  • Java arrays - can you name the keys?

    i have 1 function that declares 2 ArrayLists of type String. They are the same size and the values are related. Let's call them A & B.
    I was hoping i could have my function return just 1 array, with the values of arraylist A being they keys of the returned array, and the values of arraylist B being the values in the returned array.
    But can you name the elements of an array in java? in php you can, but in java, the only examples i have seen only use the standard #'s: 0, 1, 2, 3, etc. if you can, please post sample syntax.
    i guess i could use a double scripted array, but... well i don't exactly know what the sytax would be for that in declaring the funciton.
    if someone has a link of these kinds of examples / topics they could send me - that would be great too - i googled it but haven't really come across any.
    thanks in advance

    It appears as though what you are trying to do is associate a specific "key" value with another value. For example, let's say you want the word "dog" to be the key for the arraylist value of "bull dog."
    The perfect data structure for this that Java provides is called a Map. Here is some sample code.
    //create something called a HashMap where the first
    //value is the key, and the second value is the value
    //that the key is associated with
    HashMap<String, String> map = new HashMap<String, String>();
    //now, consider the previous example...
    //here is the syntax for storing those values
    map.put("dog", "bull dog");
    //and this would be the syntax for getting a value
    String value = map.get('dog");
    //which would give "bull dog"
    {code}
    Hope that helps :)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           

  • How can we trape the Keys Pressed on the Keyboard and do action depending on the keys

    I want to know how we can capture the keys pressed on the keyboard by the user and do necessary action depending on the key pressed by the user....
    For example.... If user press Alt+q the form should go in the Enter Query Mode.
    Can anyone tell me how i can do this in Form 6i....
    Please Help me out from this problem...
    Thanks in Advance
    Shan

    Can somebody point me to what I need to use to
    intercept all the 'keys' for an application.
    At the moment I have set Focus to parts of the
    application when the Mouse is over it, and then get
    the Keys with a KeyListener.
    Is there a way to get the keys, as long as the Java
    app is active.
    - Its an image display program, and I want to use keys
    for - next image , zooom etc etc.
    If soemone could just point me to the right part of
    the API to look in?java.awt.Toolkit.addAWTEventListener()
    then use the mask defined in AWTEvent.KEY_EVENT_MASK to listen to all the events.
    Steve

  • Is there anyway to organize icloud photo steams into abc order? and can you change the key photo image??? makes me crazy!

    I am searching for ways to organize my photo streams...alphabetical preferably.
    Also how do you change the key photo from something random that i did not choose to be the key photo (in the photo stream/icloud mode).
    Thank you!
    sue

    As far as I can see, sue, the shared Photo Streams will always show the photo last added as the key photo. So just make sure the last photo you add is a photo like to see as the key photo. I do no know of any method to arrange the photo streams alphabetically. My photo streams are neither shown in chronological order nor in the order I created them.

  • Can you move the keys about on a 2010 MBA, 11"?

    Hi,
    A friend has called me from his European holiday to say he can get me a new MBA for what works out to just over £500, about 1/3 off the UK RRP! The only problem is the keyboard would be AZERTY, not QWERTY.
    Is it possible to pop the keycaps off and swpa them about to 'make' a QWERTY keyboard on the new MBAs?
    Thanks,

    No offense but the above poster is 100% wrong. The key caps on the MacBook Air are totally removable and switchable; I just pulled off a couple of mine and swapped them around.
    There are plenty of tutorials on YouTube on how to do it, though the ones I saw involved popping off the underlying scissor mechanism, which is overkill imho. Just lift up the bottom right corner of the key you need to swap out with a fingernail or a small jeweler's screwdriver and then pull delicately up. To reinsert a key, just slide the key in towards the monitor and push down.
    You do have to be very, very careful, though, as the plastic parts underneath are very delicate. Once you've switched the keys around once, I wouldn't do it again. If you do break one of the scissor mechanisms, though, you might be able to swing by a local Apple store and get a replacement for free.

  • Can you clear the old time machine backups and start fresh?

    Is there a way to delete all of the backup sets at once, and start fresh with a new set of backups?

    Do you really need Time Machine? It installed itself using my 300GB ext. drive. I never used it but watched it working-hard ever hour. And slowly it started filling-up my ext. drive until it warned me to remove some back-ups.
    So I backed-up (manually) all the files I needed on the Ext. drive, cleaned the ext. drive completely with Disk Utility, disabled Time machine and haven't re-started it.
    Now I see how great Leopard really is. Stable and fast. And now I back-up manually what I need when I need it.

  • How do you clear the buffer of excess key presses, in Java?

    Right now, in the game I am working on, if the user presses a key multiple times the actions will be performed as many times as it registers, even though I disallow key presses during the time that the action is taking place.
    For example, a move of 1 space can be carried out with a key press. While the character is moving, key presses are not allowed. However, if you just press the key 5 or 25 times, the character will just keep moving, once he's done with the first one.
    What I need to do is clear the buffer of key presses after an action has taken place, so that the excess keys are not registered. I know the commands to do so in C++, but not in Java.

    Well, I think you've got what I was saying backwards, or I'm not understanding what you are asking correctly.
    it is suppposed to return if isMoving is true, not false. If isMoving is true, it means that the moving animation, and actions are taking place, Which is a process that take about a second, for the player to move one space.
    Here's the code where I process the keypresses
    private void processKey(KeyEvent e) {
        if (isMoving) {return;}
        int keyCode = e.getKeyCode();
        if (!isPaused && !gameOver) {
            isMoving = true;
          if (keyCode == KeyEvent.VK_UP) {
            player.move(player.NE);
          else if (keyCode == KeyEvent.VK_DOWN) {
            player.move(player.SW);
          else if (keyCode == KeyEvent.VK_LEFT) {
            player.move(player.NW);
          else if (keyCode == KeyEvent.VK_RIGHT) {
            player.move(player.SE);
            isMoving = false;
    }And in case I need to clear up what I want to happen, and the results I'm seeing. The player, in this case, moves one space in a direction, with a single press of an arrow key. That functionality is working fine, as intended. However, I don't want you to press the key twice quickly and move two spaces. I don't want you to hold down an arrow key and move continually.
    That part of the functionality is what is not working.
    As I understand my code, pressing the key once should set isMoving to true and, therefore further presses shouldn't register until isMoving is false again, which shouldn't happen until the player moves have finished.
    But, the way that it is working now is that if I press the key 2, 3 or 20 times, as soon as one move is finished another begins. Are the key presses just sitting in the buffer somewhere waiting for isMoving to become false again? And if so, is there a function I can call to clear that buffer after a move is finished?

  • When I am on my home screen on the iPhone, swipe left to the search page. Put a letter in the search field. then scroll down. eventually you get to all your texts previously deleted or not. The are all there. How can I clear the entire text from the phone

    When I am on my home screen on the iPhone, swipe left to the search page. Put a letter in the search field. then scroll down. eventually you get to all your texts previously deleted or not. The are all there. How can I clear the entire text from the phone

    When I am on my home screen on the iPhone, swipe left to the search page. Put a letter in the search field. then scroll down. eventually you get to all your texts previously deleted or not. The are all there. How can I clear the entire text from the phone

  • Can you advise the process to clear down the table(REGUH-payment table)

    Can you advise the process to clear down the table(REGUH-payment table)

    Frankly speaking i have not understood the question.
    If you want to clear the data from REGUH internal table,
    Use,
    Clear  REGUH or Refresh REGUH.
    Reply if this is not what you wanted.
    Regards,
    Mayank

  • When using the Save Layer Comps to PDF Script, can you change the PDF settings. I need both Smallest File Size and Press Quality?

    When using the Save Layer Comps to PDF Script, can you change the PDF settings. I need both Smallest File Size and Press Quality? Thanks in advance to the group for assisting with this matter.
    — John

    No, there isn't. The way you're using the file is not recommended. You
    should use a review tracker to avoid getting in the way of one another.

  • HT4211 I just updated my iPad 2 to iOS 7 and whenever I try to type on the keyboard screen, it takes several seconds to acknowledge the key press. It really is slow.How can I fix this?

    I just updated my iPad 2 to iOS 7 and whenever I try to type on the keyboard screen, it takes several seconds to acknowledge the key press. It really is slow.How can I fix this?

    Settings>General>Reset>Reset All Settings

  • My configuration audio "output" is only numerique and I would like listen to audio by integrated loudspeaker. I can't use the keys "sound on my computer. If I click on I have a icon "forbiden".  Can you help me

    my configuration audio "output" is only numerique and I would like listen to audio by integrated loudspeaker. I can't use the keys "sound on my computer. If I click on I have a icon "forbiden".  Can you help me

    Si vous tappez sûr le cle <<option>> et cliquez vôtre souris de l'icon de haut-parleur
    de le haut partie de sa Desktop, vous pouvez voir les choix pour le son: les haut-parleurs internales, Line out et Digital Out. C'est tout ! Ou cliquez de la Pomme a gauche, choissisez Préferences, et Son, et voila..... le meme chose.....
    Merci d'avoir choisi Apple
    John B

  • If you clear the recent history can your employer still track what sites you were on?

    If you clear the recent history can your employer still see what sites you were recently on?

    If you are using your employers network to connect to the internet they can track every connection that is made through their network server - outside of each individual PC that is on that network.
    Also, if the PC in question is owned by the employer, they may have installed tracking software to protect their investment.

  • Can you hear the fans on iMac? (my new 2011 27'' i5 fans are constantly audible even from a distance)

    I have the 2011 27'' Imac i5 and I can hear the fans all the time as of start. It's not super strong but clearly audible even from distance. istat shows normal fan speeds (1100/1300/1000 respectively) . The noise bothers me a lot especially in a silent room. The fans start to be audible immediately after switching on and get higher (+temperature rises significantly) under any easy activity .. like streaming a video from net.
    Can you hear the fans on yours at all?

    Do a SMC reset (maybe do it 2-3x) and if the problem persists as you have been advised call AppleCare.
    SMC RESET
    Shut down the computer.
    Unplug the computer's power cord and all peripherals.
    Press and hold the power button for 5 seconds.
    Release the power button.
    Attach the computers power cable.
    Press the power button to turn on the computer.
    PRAM RESET
    Shut down the computer.
    Locate the following keys on the keyboard: Command, Option, P, and R. You will need to hold these keys down simultaneously in step 4.
    Turn on the computer.
    Press and hold the Command-Option-P-R keys. You must press this key combination before the gray screen appears.
    Hold the keys down until the computer restarts and you hear the startup sound for the second time.
    Release the keys.
    Roger

  • How can I clear the stored browsing history in iCloud?

    As the headline says, how can I clear the stored browsing history in iCloud?
    After resetting my iPhone, previous history came back as soon as I signed in with iCloud on it. I would really like this to not occur.

    The only place that Thunderbird gets suggested addresses is from your address books. I have seen some not show up in the books because some key info was never stored when the contact was saved.
    Sometimes you can start a new message and when the bad contact shows up select it. Create the message and save it to drafts. Then you can go to the Draft folder open the draft message by right clicking and select Open Message in Tab. Then click the star to the right of the contact and it will open the contact edit button. From there you can delete the contact.
    This does not always work though.

Maybe you are looking for

  • Control design and simulation toolkit

    Hello everyone, Does anybody know if indexing is possible in a Control and Simulation loop in Labview? I need to save in an array all the simulation data. Thanks, Ussr123. Solved! Go to Solution.

  • Desktop Picture Preference Pane issue

    Strange, newish issue: in dektop pictures preference pane, when I choose a photo from "photos" as my background, it always sets a plain blue background. Any other picture from any of the other folders works fine. All my photos appear in the pane as c

  • Windows 8 consumer preview: amd 7470 switchable graphics - unswitchable ???

    windows 8 consumer preview: amd 7470 switchable graphics - unswitchable   ??? can any one helpe me >>??  This question was solved. View Solution.

  • Add OU(Organizational Unit)  to active directory plugin

    Hi Guys, i am using Ad plugin with BOE 3.1 to authenticate Active directory users, i am able to map AD Groups while configuring the ad plugin. my question is, is it possible to map OU instead of the AD Groups ? in my production active directry i have

  • Phone has frozen...

    My Phone has frozen... and will not switch off using on/off button How can I fix this?