MouseEvent consumes KeyEvent?!

K

k0m0r0w5k1

Hi. I got stucked on the following:
This is a panel that allows line drawing.
I want the line to be green if Ctrl is pressed down.

public MyPanel extends JPanel implements MouseListener, KeyListener {

boolean ctrl = false;
Point fromPoint, toPoint;

(...)

public void paint(Graphics g) {
if (ctrl) {
g.setColor(Color.green);
} else {
g.setColor(Color.red);
}
g.drawLine(fromPoint.x, fromPoint.y, toPoint.x, toPoint.y);
}

public void mouseMoved(MouseEvent e) {
toPoint = e.getPoint();
repaint();
}

public void keyPressed(KeyEvent e) {
ctrl = true;
repaint();
}

public void keyReleased(KeyEvent e) {
ctrl = false;
repaint();
}
}

And... it doesn't work :(
It looks like MouseEvent consumes KeyEvent, beacuse keyPressed() is NOT
called here :(
The listeners are set up correctly.
Changing this to:

public void mouseMoved(MouseEvent e) {
if (e.isControlDown) {
(...)
}
(...)
}

is not a solution, because it will only change the color after mouse
was moved.
And I want it to happen instantly on every key press/release.
What's wrong here?
How to make it work?

Thx a lot in advance,

k0m0r
 
V

Vova Reznik

try to add KeyListener to a JFrame
that contains your MyPanel
or setFocusable true for your MyPanel
 
B

Bart Rider

Hi. I got stucked on the following:
This is a panel that allows line drawing.
I want the line to be green if Ctrl is pressed down.

public MyPanel extends JPanel implements MouseListener, KeyListener {

boolean ctrl = false;
Point fromPoint, toPoint;

(...)

public void paint(Graphics g) {
if (ctrl) {
g.setColor(Color.green);
} else {
g.setColor(Color.red);
}
g.drawLine(fromPoint.x, fromPoint.y, toPoint.x, toPoint.y);
}

public void mouseMoved(MouseEvent e) {
toPoint = e.getPoint();
repaint();
}

public void keyPressed(KeyEvent e) {
ctrl = true;
repaint();
}

public void keyReleased(KeyEvent e) {
ctrl = false;
repaint();
}
}

And... it doesn't work :(
It looks like MouseEvent consumes KeyEvent, beacuse keyPressed() is NOT
called here :(
The listeners are set up correctly.
Changing this to:

public void mouseMoved(MouseEvent e) {
if (e.isControlDown) {
(...)
}
(...)
}

is not a solution, because it will only change the color after mouse
was moved.
And I want it to happen instantly on every key press/release.
What's wrong here?
How to make it work?

Thx a lot in advance,

k0m0r

For modifier keys like shift, ctrl, alt the keypressed,
keyreleases events will not be called.
If your mouseMoved method is called, the MouseEvent object
has the info on modifiers pressed, just call
e.getModifiersEx();

Have a look for the masks on the return value:
http://java.sun.com/j2se/1.5.0/docs/api/java/awt/event/InputEvent.html#getModifiersEx()

Best regards
Bart
 
K

k0m0r

For modifier keys like shift, ctrl, alt the keypressed,
keyreleases events will not be called.
If your mouseMoved method is called, the MouseEvent object
has the info on modifiers pressed, just call
e.getModifiersEx();

Yes, I understand that, but it's not solving my problem :(
I need to cause the reaction when the Shift is pressed, wether or not
the mouse was moved. Any ideas?

Thx,

k0m0r
 

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,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top