• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

About KeyListener

 
Ritesh Somani
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Friends

I am developing Snake Game application, I succeeded also only one problem is I am using two dimensional button array and using arrow keys for movement. I have to press arrow keys multiple times for moving only in one direction. So I want to know which function I have to use for moving only in one direction by pressing arrow key only once.

Thanks & Regards
-Ritesh Somani
 
Darryl Burke
Bartender
Posts: 5148
11
Java Netbeans IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You didn't tell us what GUI toolkit is involved. Swing? AWT? SWT? Something else?

I'm moving this to the GUIs section, but to get better help sooner, you need to post a SSCCE (Short, Self Contained, Correct (Compilable), Example) that demonstrates the problem.
 
Ritesh Somani
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bro I am using SWING
 
Darryl Burke
Bartender
Posts: 5148
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In keyPressed(...) start a javax.swing.Timer and stop the Timer in keyReleased(...).

Note that the sequence of key pressed / released events when a key is held down differs in Windows and Linux.

You could also look into using Key Bindings, which is more robust.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic