Solved

KeyEvents

Posted on 2004-03-22
11
230 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
  • 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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 25 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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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
jar file executable 12 58
replace a word with other 1 45
fibonacci ten numbers 4 45
running on tomcat not jboss eap 7.0 3 17
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…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This video teaches viewers about errors in exception handling.

789 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