Solved

KeyListener

Posted on 2004-09-08
4
317 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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

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…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

706 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now