Solved

KeyListener

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

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

Suggested Solutions

Title # Comments Views Activity
jboss 7.1 start up error 1 61
running on tomcat not jboss eap 7.0 3 30
spring maven example issues 3 40
passing enum to a method 4 35
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 This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

749 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