Solved

KeyListener

Posted on 2004-09-08
4
321 Views
Last Modified: 2011-09-20
Im having a problem with my KeyListener.

In the "public void keyTyped(KeyEvent ke)" method, I am trying to detect when the user presses the non-keypad down button. I used the following code:

if(ke.getKeyChar() == KeyEvent.VK_DOWN)
{
      System.out.println("Down Pressed!");
}

This works for nearly every key. The exceptions are the up, down, left, and riight, and a few others that aren't relevant to my problem.

I also tried using ke.getKeyCode() but that didn't work either. Does the KeyListener ignore key events on these keys?
0
Comment
Question by:krispin
  • 2
4 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 125 total points
ID: 12011718
Try putting it in keyPressed too
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12011765
You can get the key that was pressed either as a key character (which is a Unicode character) or as a key code (a special value representing a particular key on the keyboard).

   
component.addKeyListener(new MyKeyListener());
   
    public class MyKeyListener extends KeyAdapter {
        public void keyPressed(KeyEvent evt) {
            // Check for key characters.
            if (evt.getKeyChar() == 'a') {
                process(evt.getKeyChar());
            }
   
            // Check for key codes.
            if (evt.getKeyCode() == KeyEvent.VK_HOME) {
                process(evt.getKeyCode());
            }
       
0
 

Author Comment

by:krispin
ID: 12011784
Thanks, worked in there with ke.getKeyCode()
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12011862
8-)
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question