Solved

KeyEvents

Posted on 2004-03-22
11
222 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
Comment Utility
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
Comment Utility
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
Comment Utility
Hi potatoes44,

call addKeyListener()
0
 
LVL 30

Expert Comment

by:mayankeagle
Comment Utility
Can you post your updated code?
0
 

Author Comment

by:potatoes44
Comment Utility
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 13

Accepted Solution

by:
Webstorm earned 25 total points
Comment Utility
You should also request the focus on the component :

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

0
 

Author Comment

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

Expert Comment

by:mayankeagle
Comment Utility
>> 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:mayankeagle
Comment Utility
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
Comment Utility
After altering another portion of my code, WebStorm's suggestion worked.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
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…
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…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now