Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java-check if control key is being pressed

Tags:

java

swing

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.

like image 726
Mika Avatar asked Jul 25 '12 22:07

Mika


People also ask

How do you check if a key is being pressed in Java?

Use KeyEvent. getKeyChar() and KeyEvent. getKeyCode() to find out which key the user pressed.

How do I check if the user is pressing a key?

The Windows on-screen keyboard is a program included in Windows that shows an on-screen keyboard to test modifier keys and other special keys. For example, when pressing the Alt , Ctrl , or Shift key, the On-Screen Keyboard highlights the keys as pressed.


1 Answers

use the "isControlDown()" boolean:

public void keyPressed (KeyEvent e)
{
     System.out.println(e.isControlDown());
}
like image 173
ChrisSannar Avatar answered Oct 13 '22 10:10

ChrisSannar