aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes About KeyListener Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "About KeyListener" Watch "About KeyListener" New topic
Author

About KeyListener

Ritesh Somani
Greenhorn

Joined: May 04, 2012
Posts: 4
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

Joined: May 03, 2008
Posts: 4527
    
    5

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.


luck, db
There are no new questions, but there may be new answers.
Ritesh Somani
Greenhorn

Joined: May 04, 2012
Posts: 4
Bro I am using SWING
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4527
    
    5

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: About KeyListener