Detecting "Shift" + "MouseClick" on a JButton ....

Is there a way to detect if the "Shift" key is pressed while clicking on a JButton?  If so, how can I do this?
Who is Participating?
billy_98_1Connect With a Mentor Commented:
Sorry, I clicked 'comment' instead of 'answer'.
use the keyUp method to detect when shift is pressed. Set a boolean to true when shift is pressed down in keyDown and false then it is let up again.
I don't know if seeing if shift will work when pressing it using the variable key, but you could try evt.SHIFT
So you might want something like:

public boolean keyUp(Event evt, int key) {
if (evt.SHIFT) Shiftdown=true;

public boolean keyDown(Event evt, int key) {
if (evt.SHIFT) Shiftdown=false;

then in the action listener for the button, do what ever the button makes happen only if Shiftdown=true.

If evt.SHIFT doesn't work, make sure the form has the focus (put requestFocus() in the init() statement) and if that doesn't work, try seeing if the variable key changes when you hit shift.
Tho do that put a line like:
System.out.println(""+key); in the keyUp or keyDown method.
This will capture the shift click...

import javax.swing.*;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import java.awt.*;
import java.applet.Applet;

public class tt extends Applet implements MouseListener {
  boolean setColour = false;

  JButton b = new JButton("Shift click to Turn me Blue");

  public void start() {


  public void mousePressed(MouseEvent e) { }
  public void mouseEntered(MouseEvent e) { }
  public void mouseExited(MouseEvent e) { }
  public void mouseReleased(MouseEvent e) { }
  public void mouseClicked(MouseEvent e) {
    if (e.isShiftDown()) {
      if (!setColour) {
        b.setText("Shift click to Turn me Grey");
        setColour = true;
      } else {
        setColour = false;
        b.setText("Shift click to Turn me Blue");

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

This is a slightly different approach. The JButton registers itself with a mouselistener (in this case the tt class) and then the processing to deal with the shift click is done in there in the mouseClicked event.

In this case a normal click does nothing, but a shift click changes the button colour.

If you do it the way billy_98_1 suggests then it is more difficult to figure out when the shift key is pressed and mouse button are pressed together.

This way is just a bit easier...
Finally, use this HTML file to open the above code as an Applet in appletviewer:

<applet codebase="." width=500 height=500>
I'm sorry you clicked answer instead of comment...
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.