Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I detect if caps lock is toggled in Swing?

Tags:

java

swing

I'm trying to build a better username/password field for my workplace and would like to be able to complain when they have their caps lock on.

Is this possible? And if so I'd like to have it detected before the client types their first letter.

Is there a non-platform specific way to do this?

like image 585
Allain Lalonde Avatar asked Sep 17 '08 22:09

Allain Lalonde


People also ask

How do you check whether Caps Lock is on?

The getModifierState() method returns true if a modifier is active; otherwise, it returns false . The event. getModifierState('CapsLock') can be used to detect if the caps lock is on.


1 Answers

Try this, from java.awt.Toolkit, returns a boolean:

Toolkit.getDefaultToolkit().getLockingKeyState(KeyEvent.VK_CAPS_LOCK)
like image 103
Nick Craver Avatar answered Oct 28 '22 04:10

Nick Craver