?
Solved

KeyEvents

Posted on 2004-03-22
11
Medium Priority
?
241 Views
Last Modified: 2010-03-31
I have a class that implements a KeyPresses interface that is defined like this:

import java.awt.event.*;

public interface KeyPresses
{
    void keyTyped(KeyEvent e);
}

In the class that implements this interface, here is the code:

public void keyTyped(KeyEvent e)
    {
        switch(e.getKeyChar())
        {
            case 'j':
                  ....

        }
    }

However when a key is typed, there is no response. Anyone have an idea as to what is wrong?
0
Comment
Question by:potatoes44
[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
  • 4
  • 3
  • 2
  • +1
11 Comments
 
LVL 6

Expert Comment

by:dorothy2
ID: 10654894
I'm not sure what you are trying to do. Are you trying to handle the keyTyped event? If so, why don't you either extend KeyAdapter or implement the KeyListener class? Then you can tie the subclassed KeyAdapter or KeyListener to the code which contains your GUI.



If you implement KeyListener in the GUI class, you have three methods to implement - keyPressed, keyReleased, and keyTyped. You can write stub code if you are not interested in capturing the keyPressed or keyReleased events. By stubbing the code, I mean something like the following --

public void keyReleased(KeyEvent event) {}

public void keyPressed(KeyEvent event) {}

public void keyTyped(KeyEvent event) {
// This is the one you are interested in
 switch(event.getKeyChar())
        {
            case 'j':
                  ....

        }
}

This is a lot easier than trying to write your own version of key event handling.
0
 

Author Comment

by:potatoes44
ID: 10655338
I implemented the changes Dorothy suggested, my class now implements the KeyListener interface. However, the key events still produce no response. I looked at the documentation for KeyListener and it said that in order to process key events, a component must have keyboard focurs. However the setFocusable() methods pertain to JComponents, which I do not use in my class. How can I work this out?
0
 
LVL 13

Expert Comment

by:Webstorm
ID: 10655911
Hi potatoes44,

call addKeyListener()
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 30

Expert Comment

by:Mayank S
ID: 10656118
Can you post your updated code?
0
 

Author Comment

by:potatoes44
ID: 10659951
This is my updated code

public class Board implements ActionListener, KeyListener
{
....
public void keyPressed(KeyEvent e) {}
   
    public void keyReleased(KeyEvent e) {}
   
    public void keyTyped(KeyEvent e)
    {
        switch(java.lang.Character.toUpperCase(e.getKeyChar()))
        {
            case 'J':
                .....

    }

I also tried what webstorm suggested. I changed Board to subclass of Component and then to a subclass of JComponent, and used the methods addKeyListener(this); and setFocusable(true); , respectively. But still there is no response.
0
 
LVL 13

Accepted Solution

by:
Webstorm earned 75 total points
ID: 10661649
You should also request the focus on the component :

    comp.addKeyListener(new Board());
    comp.requestFocus();

0
 

Author Comment

by:potatoes44
ID: 10661950
Thank you, but I tried your suggestion WebStorm and still no result.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10664382
>> addKeyListener(this);

On what component did you add the KeyListener? Can we see that part of the code? Are you adding it to a JTextField? Perhaps if you could post the whole code, it would be better. If you want to capture KeyPressed, then you have to code something in keyPressed ( KeyEvent ke ).
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10860113
Well, after that comment, you actually said yourself that:

>> but I tried your suggestion WebStorm and still no result

- then why did you accept it?
0
 

Author Comment

by:potatoes44
ID: 10860582
After altering another portion of my code, WebStorm's suggestion worked.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
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 third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses
Course of the Month11 days, 7 hours left to enroll

752 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