Solved

KeyEvents

Posted on 2004-03-22
11
233 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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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
How to get all the API from website? 11 129
SHA2 certs for IIS AND Java? 2 122
Java string replace 11 64
Eclipse for Java EE development 2 45
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This video teaches viewers about errors in exception handling.

730 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