Java-check if control key is being pressed

后端 未结 4 1342
孤街浪徒
孤街浪徒 2020-12-21 00:47

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.

相关标签:
4条回答
  • 2020-12-21 01:16

    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.

    0 讨论(0)
  • 2020-12-21 01:22

    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

    0 讨论(0)
  • 2020-12-21 01:28

    use the "isControlDown()" boolean:

    public void keyPressed (KeyEvent e)
    {
         System.out.println(e.isControlDown());
    }
    
    0 讨论(0)
  • 2020-12-21 01:30

    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.

    0 讨论(0)
提交回复
热议问题