Solved

key listener

Posted on 1997-04-03
2
142 Views
Last Modified: 2010-04-16
When I run the following code, a is always 0 regardless of what key is pressed. It doesn't crash, so I assume the syntax is correct. I've tried every combination and permutation of the 1.1.1 options for keys (getKeyChar, getKeyText, etc) and nothing works. Any advice? Really, all I want to do is refresh the screen each time a key is pressed. The keyCode() is just for debugging.

addKeyListener (new KeyAdapter() {
public void keyTyped(KeyEvent e)
{
a=e.getKeyCode();
repaint();
}
});
0
Comment
Question by:rbixby
2 Comments
 
LVL 1

Accepted Solution

by:
fadl earned 50 total points
ID: 1219416
Are you sure you have registered the right object as a keyListener ?
If yes then you must include more source code it is
nearly impossible to say what's wrong by seeing on such a small
piece of code {:)
How was 'a' variable declared ?

0
 

Author Comment

by:rbixby
ID: 1219417
a is an int in this case, though when I was listening for a char, it was a char and when I was listening for text, it was a string.
I guess it's setting up the key listener that's confusing me. It sounds as if I need additional code to actually get key input. I'll read the chapters I have again and review some code I downloaded from javasoft yesterday. Maybe the answer is in there. If you have any more advice, I'm all ears.
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone 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
varialbe initialization 11 37
difference between sorce folder and folder in eclipise 3 29
printf performancy 11 73
Need Help! Getting a syntax error and don't understand why 3 31
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…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …

828 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