Discussions
Categories
- 385.5K All Categories
- 4.9K Data
- 2.5K Big Data Appliance
- 2.4K Data Science
- 453.4K Databases
- 223.2K General Database Discussions
- 3.8K Java and JavaScript in the Database
- 47 Multilingual Engine
- 606 MySQL Community Space
- 486 NoSQL Database
- 7.9K Oracle Database Express Edition (XE)
- 3.2K ORDS, SODA & JSON in the Database
- 585 SQLcl
- 4K SQL Developer Data Modeler
- 188K SQL & PL/SQL
- 21.5K SQL Developer
- 46 Data Integration
- 46 GoldenGate
- 298.4K Development
- 4 Application Development
- 20 Developer Projects
- 166 Programming Languages
- 295K Development Tools
- 150 DevOps
- 3.1K QA/Testing
- 646.7K Java
- 37 Java Learning Subscription
- 37.1K Database Connectivity
- 201 Java Community Process
- 108 Java 25
- 22.2K Java APIs
- 138.3K Java Development Tools
- 165.4K Java EE (Java Enterprise Edition)
- 22 Java Essentials
- 176 Java 8 Questions
- 86K Java Programming
- 82 Java Puzzle Ball
- 65.1K New To Java
- 1.7K Training / Learning / Certification
- 13.8K Java HotSpot Virtual Machine
- 94.3K Java SE
- 13.8K Java Security
- 208 Java User Groups
- 25 JavaScript - Nashorn
- Programs
- 667 LiveLabs
- 41 Workshops
- 10.3K Software
- 6.7K Berkeley DB Family
- 3.6K JHeadstart
- 6K Other Languages
- 2.3K Chinese
- 207 Deutsche Oracle Community
- 1.1K Español
- 1.9K Japanese
- 474 Portuguese
KeyListener anomoly: delay in firing KeyEvent

843807
Member Posts: 46,582
Hello,
I've implemented a KeyListener that's exhibiting some anomolies. I'm using it in a video game. I'm using it to control the main character's movement. It determines his movement based on strokes of the arrow keys. I've programmed it so that only one key has an effect at a time. That is, so long as (say) the up arrow was pressed first and held down, the character moves up, and no other key has an effect. Other keys only have an effect if the up arrow is released.
The anomoly comes about when two keys are held down (only one of them determining the movement of the character - which ever was first), and then the first is released. When the first is released (but the second is still held down), the character freezes for about a second, and only after that second does he move in the direction of the second key which is still held down.
It's this second-long delay which is the anomoly. I'm guessing it results from the fact that the KeyEvent is not being fired constantly or immediately after the release of the first key. Even though the second key is held down, it takes a second to fire the KeyEvent after the first key is released (though it seems to fire immediately upon depressing the key).
I'm wondering if there's a way to get rid of the delay or at least shorten it so it's not noticeable.
Here's my KeyListener code:
Thanks
I've implemented a KeyListener that's exhibiting some anomolies. I'm using it in a video game. I'm using it to control the main character's movement. It determines his movement based on strokes of the arrow keys. I've programmed it so that only one key has an effect at a time. That is, so long as (say) the up arrow was pressed first and held down, the character moves up, and no other key has an effect. Other keys only have an effect if the up arrow is released.
The anomoly comes about when two keys are held down (only one of them determining the movement of the character - which ever was first), and then the first is released. When the first is released (but the second is still held down), the character freezes for about a second, and only after that second does he move in the direction of the second key which is still held down.
It's this second-long delay which is the anomoly. I'm guessing it results from the fact that the KeyEvent is not being fired constantly or immediately after the release of the first key. Even though the second key is held down, it takes a second to fire the KeyEvent after the first key is released (though it seems to fire immediately upon depressing the key).
I'm wondering if there's a way to get rid of the delay or at least shorten it so it's not noticeable.
Here's my KeyListener code:
import java.awt.event.*; public class InputProcessor implements KeyListener { // key codes public static final int LEFT = 37; public static final int UP = 38; public static final int RIGHT = 39; public static final int DOWN = 40; private boolean left_arrow_down = false; private boolean right_arrow_down = false; private boolean up_arrow_down = false; private boolean down_arrow_down = false; // flag to mark when a key (any key) is pressed private boolean key_pressed = false; public InputProcessor() {} public void keyPressed (KeyEvent KE) { if (key_pressed) return; // all other keys disabled if one key is down switch (KE.getKeyCode()) { case LEFT: left_arrow_down true; key_pressed = true; break; case RIGHT: right_arrow_down = true; key_pressed = true; break; case UP: up_arrow_down = true; key_pressed = true; break; case DOWN: down_arrow_down = true; key_pressed = true; break; } } public void keyReleased (KeyEvent KE) { switch (KE.getKeyCode()) { case LEFT: left_arrow_down = false; break; case RIGHT: right_arrow_down = false; break; case UP: up_arrow_down = false; break; case DOWN: down_arrow_down = false; break; } // only set key_pressed to false if all keys have been released if (!left_arrow_down && !right_arrow_down && !up_arrow_down && !down_arrow_down) key_pressed = false; } public void keyTyped (KeyEvent KE) {} public boolean isLeftArrowDown() {return left_arrow_down;} public boolean isUpArrowDown() {return up_arrow_down;} public boolean isRightArrowDown() {return right_arrow_down;} public boolean isDownArrowDown() {return down_arrow_down;} public static void main (String[] args) { InputProcessor IP = new InputProcessor(); } }I'm wondering if there might be a command I can execute at the end of the keyReleased() method, something like this:
public void keyReleased (KeyEvent KE) { switch (KE.getKeyCode()) { case LEFT: left_arrow_down = false; break; case RIGHT: right_arrow_down = false; break; case UP: up_arrow_down = false; break; case DOWN: down_arrow_down = false; break; } // only set key_pressed to false if all keys have been released if (!left_arrow_down && !right_arrow_down && !up_arrow_down && !down_arrow_down) key_pressed = false; checkForKeyPressed(); }Let me know if you'd like a SSCCE.
Thanks
Comments
-
Not sure if this will help, but try to consume() the events.
-
Note: This thread was originally posted in the [Java Programming|http://forums.sun.com/forum.jspa?forumID=31] forum, but moved to this forum for closer topic alignment.
-
It's this second-long delay which is the anomoly. I'm guessing it results from the fact that the KeyEvent is not being fired constantly or immediately after the release of the first key.That's the usual key-repeat behavior on most OSs.I'm wondering if there's a way to get rid of the delayProbably by setting booleans in the keyPressed(...) / keyReleased(...) and polling those booleans at equal intervals using a Timer.
db -
>
Probably by setting booleans in the keyPressed(...) / keyReleased(...) and polling those booleans at equal intervals using a Timer.
>
I did something similar to this. Thanks.
This discussion has been closed.