Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

KeyListener

Posted on 2004-09-08
4
Medium Priority
?
348 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to implement Singleton Design Pattern in Java.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

609 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