Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I get the caps lock state?

Status
Not open for further replies.

LordOfCode

Programmer
Feb 26, 2000
66
US
I am building a swing application and currently I am working in a status bar for my app.

In this status bar I would like to tell the user what is the caps lock status.

How do I do that?

Thanks in advance!

edalorzo@hotmail.com
 
Well "LordOfCode" ... Look at the KeyListener class ...
 
It's more complicated than that. I already added a keylistener to the MDI form.

How ever I added a keylistener, it just could tell me somebody pressed Caps Lock key, but it cannot tell me if caps lock is on or off.

Any idea how to do so?

edalorzo@hotmail.com
 
I think what you are looking for this getLockingKeyState() method...

i.e...
Code:
Toolkit.getDefaultToolkit().getLockingKeyState(KeyEvent.VK_CAPS_LOCK);

it will return:
Returns whether the given locking key on the keyboard is currently in its "on" state. Valid key codes are VK_CAPS_LOCK, VK_NUM_LOCK, VK_SCROLL_LOCK, and VK_KANA_LOCK.


Hope this helps.
 
I wrote an exemple but the getLockingKeyState does not work on my machine, FreeBSD5.1.

Don't know about other OS.
 
The following code worked for me:

Code:
import java.awt.*;
import java.awt.event.*;

class Test
{

	public static void main(String[] args)
	{ 
		System.out.println( "Is CAPS LOCK on: " +
		Toolkit.getDefaultToolkit().getLockingKeyState (KeyEvent.VK_CAPS_LOCK) );
		System.out.println( "Is NUM LOCK on: " +
		Toolkit.getDefaultToolkit().getLockingKeyState (KeyEvent.VK_NUM_LOCK) );

	}
}
 
Thank you very much.
That worked just perfect.

[thumbsup]

edalorzo@hotmail.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top