I add a KeyListener on my JPanel (really this is for any component), but whenever I press the F10 action key, it blocks every other action key until I either press F10 again or another key or mouse click.
I want to turn this behaviour off, but I have no yet found a way. I tried to override the processEvent and processKeyEvent methods which handle the event before the listeners, but without any luck.
What I really want is full control of what happens when a key is pressed and released!
What I also notice is that when the F-keys are not blocked, and I press F10, it will send a key pressed and key release event, BUT when it is in a blocked state and I press F10, it will only send a key released event.
This is the code I tried testing with
import java.awt.AWTEvent;
import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class MyFrame extends JFrame {
public static void main(String[] args) {
new MyFrame();
}
public MyFrame() {
MyCanvas c = new MyCanvas();
c.setFocusTraversalKeysEnabled(false);
setVisible(true);
setSize(200, 200);
setDefaultCloseOperation(EXIT_ON_CLOSE);
add(c, BorderLayout.CENTER);
c.requestFocus();
}
private class MyCanvas extends JPanel implements KeyListener {
public MyCanvas() {
super();
setFocusTraversalKeysEnabled(false);
setFocusable(true);
addKeyListener(this);
}
@Override
protected void processKeyEvent(KeyEvent e) {
super.processKeyEvent(e);
}
//Handles the events. Delegates the events to the correct process Event methods
@Override
protected void processEvent(AWTEvent e) {
super.processEvent(e);
System.out.println("Process event: " + e.getID());
}
@Override
public void keyPressed(KeyEvent e) {
System.out.println("key pressed");
}
@Override
public void keyReleased(KeyEvent e) {
}
@Override
public void keyTyped(KeyEvent e) {
}
}
}

New Topic/Question
Reply



MultiQuote




|