7

I have a Java function in which I want to test if the control key is being held down. How can I do that?

Edit: I am using swing for gui.

Leonardo Alves Machado
  • 2,747
  • 10
  • 38
  • 53
Mika
  • 1,195
  • 6
  • 22
  • 34
  • You'll need to provide more information. Is this necessary in a GUI based program, i.e. with windows, or a console application? [This](http://stackoverflow.com/questions/160315/how-to-check-for-key-being-held-down-on-startup-in-java) may be useful, and [this](http://stackoverflow.com/questions/9545388/how-can-i-detect-arrow-keys-in-java-console-not-in-gui). – pb2q Jul 25 '12 at 22:51

4 Answers4

13

use the "isControlDown()" boolean:

public void keyPressed (KeyEvent e)
{
     System.out.println(e.isControlDown());
}
ChrisSannar
  • 141
  • 1
  • 5
6

The code above works only if the only thing pressed is the control key. If they have ctrl and some other button is pressed (perhaps) accidently, it won't capture.

You can check exactly for just the ctrl key

// Are just the CTRL switches left on
if(evt.getModifiers() == InputEvent.CTRL_MASK) {
    System.out.println("just the control key is pressed");
}

When simulating multiple keys pressed, you use the or bit operator. To simulate holding both the left button and ctrl key, look for this.

// Turn on all leftButton and CTRL switches
int desiredKey = InputEvent.BUTTON1_MASK | InputEvent.CTRL_MASK;  

When checking if the ctrl key is down you may do this

// If we turn off all switches not belonging to CTRL, are all the CTRL switches left on
if((evt.getModifiers() & InputEvent.CTRL_MASK) == InputEvent.CTRL_MASK) {
    System.out.println("Control Key is pressed and perhaps other keys as well");
}

You can also check if both the left button and ctrl mask are pressed

// If we turn off all switches not belonging to leftButton or CTRL, are all the leftButton and CTRL switches left on
if((evt.getModifiers() & desiredKey) == desiredKey) {
    System.out.println("left button and control keys are pressed and perhaps others as well");
}

Suppose you have this:

A | B

You should think of it like this. A has a control panel with a bunch of switches on. B also has a control panel with a bunch of switches on. The job of "| B" is to do the minimum work necessary to make sure all B's switches get turned on.

Suppose you have this:

A & B

The job of "& B" is to do the minimum work necessary to turn off any switches which aren't B's.

Krystian G
  • 2,842
  • 3
  • 11
  • 25
Scott Izu
  • 2,229
  • 25
  • 12
2

It depends on several things.

If you're running the Java program as a console program (text based) you have to test for approriate bits in the received chatracter.

Otherwise, you should look at InputEvents for the appropriate GUI classes, eg, http://docs.oracle.com/javase/6/docs/api/java/awt/event/InputEvent.html.

Have a look at this tutorial: http://docs.oracle.com/javase/tutorial/uiswing/events/keylistener.html

Afshin Moazami
  • 2,092
  • 5
  • 33
  • 55
Charlie Martin
  • 110,348
  • 25
  • 193
  • 263
2

I found one solution which solves my problem: I declare a global variable

boolean controlStatus=false;

Then in the event for keyPressed on the jTextField:

if(evt.getKeyCode()==KeyEvent.VK_CONTROL)
      controlStatus=true;

In the event for keyReleased:

if(evt.getKeyCode()==KeyEvent.VK_CONTROL)
      controlStatus=false;

Then I can access the global variable to check if the control key is being held down.

Mika
  • 1,195
  • 6
  • 22
  • 34
  • 1
    Yup, that's along the same lines as what I have above. but now let's do some OO thinking: you dno't want to have global flags flying about like this. Go take a look at anonymous inner classes and figure out how to invert your program so you don't need that flag. – Charlie Martin Jul 26 '12 at 01:49