Solved

KeyListener

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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Suggested Solutions

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…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
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

732 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