Java-check if control key is being pressed

后端 未结 4 1350
孤街浪徒
孤街浪徒 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: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.

提交回复
热议问题