?
Solved

KeyEvents

Posted on 2004-03-22
11
Medium Priority
?
244 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

Independent Software Vendors: 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!

Question has a verified solution.

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

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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…
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses

650 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