how to get KeyPressed event inside JWindow

W

William Zumwalt

A JWindow class is my main class (w/o all the title, min, max controls,
etc...) and I've implemented a KeyListener to it. I have a MouseListener
that works fine. But when I add a KeyListener, nothing happens when I
press the keys ... keyPressed(KeyEvent ke) catches nothing.

Inside the JWindow I have a JPanel which I set the focus to true (cause
I think you have to have focus on a component to get the key).
panel.setFocusable(true), but I still don't see any KeyPressed events
happening. Anyone have any ideas what's going on here.

Any help much appreciated.

Will
 
W

William Zumwalt

I did this, actually, I did requestFocusInWindow() since it was
suggested in the api docs over requestFocus(), and it still didn't work.
requestFocusInWindow() returned false.

My component is displayable and focusable so I'm not sure what is wrong
here.
 
G

Geli J. Crick

William said:
I did this, actually, I did requestFocusInWindow() since it was
suggested in the api docs over requestFocus(), and it still didn't work.
requestFocusInWindow() returned false.

My component is displayable and focusable so I'm not sure what is wrong
here.


I had a similar problem (though I was using a JDesktopPane with
subclasses of JInternalFrame). This may not be the best solution, but I
finally got it working by adding the keyListener to each of the
JInternalFrames (in your case, JPanels). Also, I had to re-implement the
isFocusTraversable() method in each JInternalFrame to return true.

I also found that you can implement the KeyEventDispatcher class, and
add it to the KeyBoardFocusManager
KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(new
KeyEventDispatcherImplementation());

This way you can intercept all keyboard events before they are sent to
any components. This requires quite a bit of care though, as you could
effectively disable all built in key actions (ex. TAB to move between
components). It seems to me that it is only useful in a few rare cases,
but I thought I would mention it in case it helps.
 
M

Michael Dunn

: A JWindow class is my main class (w/o all the title, min, max controls,
: etc...) and I've implemented a KeyListener to it. I have a MouseListener
: that works fine. But when I add a KeyListener, nothing happens when I
: press the keys ... keyPressed(KeyEvent ke) catches nothing.
:
: Inside the JWindow I have a JPanel which I set the focus to true (cause
: I think you have to have focus on a component to get the key).
: panel.setFocusable(true), but I still don't see any KeyPressed events
: happening. Anyone have any ideas what's going on here.


works OK using an undecorated JFrame, instead of a JWindow

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class Testing extends JFrame implements KeyListener
{
JLabel lbl = new JLabel();
public Testing()
{
setSize(400,300);
setLocation(200,100);
setUndecorated(true);
JPanel jp = new JPanel();
jp.add(lbl);
jp.setFocusable(true);
jp.addKeyListener(this);
getContentPane().add(jp);
}
public void keyPressed(KeyEvent ke)
{
if(ke.getKeyCode() == KeyEvent.VK_X) System.exit(0);//press x to exit
else lbl.setText(""+ke.getKeyChar());
}
public void keyTyped(KeyEvent ke){}
public void keyReleased(KeyEvent ke){}
public static void main(String[] args){new Testing().setVisible(true);}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top