Solved

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

Posted on 2000-02-24
6
649 Views
Last Modified: 2008-03-17
Is there a way to detect if the "Shift" key is pressed while clicking on a JButton?  If so, how can I do this?
0
Comment
Question by:bjg
  • 4
  • 2
6 Comments
 
LVL 1

Expert Comment

by:billy_98_1
ID: 2555733
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.
0
 
LVL 1

Accepted Solution

by:
billy_98_1 earned 100 total points
ID: 2555742
Sorry, I clicked 'comment' instead of 'answer'.
0
 
LVL 5

Expert Comment

by:Jod
ID: 2555766
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() {

    add(b);
    b.addMouseListener(this);
   
  }

  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.setBackground(Color.blue);
        b.setText("Shift click to Turn me Grey");
        setColour = true;
      } else {
        b.setBackground(Color.lightGray);
        setColour = false;
        b.setText("Shift click to Turn me Blue");
      }
    }
  }

}
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 5

Expert Comment

by:Jod
ID: 2555783
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...
0
 
LVL 5

Expert Comment

by:Jod
ID: 2555788
Finally, use this HTML file to open the above code as an Applet in appletviewer:

<html>
<head>
</head>
<body>
<applet codebase="." code=tt.java width=500 height=500>
</applet>
</body>
</html>
0
 
LVL 5

Expert Comment

by:Jod
ID: 2634342
I'm sorry you clicked answer instead of comment...
0

Featured Post

ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
eclipse argument 14 60
why cannot we forward request once the response is committed 2 42
javap bin 2 34
check java version using powershell 13 91
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

770 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