Package org.jdesktop.swingx.plaf.basic
Class CapsLockSupport
- java.lang.Object
-
- org.jdesktop.beans.AbstractBean
-
- org.jdesktop.swingx.plaf.basic.CapsLockSupport
-
- All Implemented Interfaces:
java.awt.KeyEventDispatcher
public final class CapsLockSupport extends AbstractBean implements java.awt.KeyEventDispatcher
A class for determining the state of theCAPS LOCK key
. It also supports notification when the locking state changes.Although it is possible to use
Toolkit.getLockingKeyState(int)
to determine the current state of the CAPS LOCK key, that method is not guaranteed to work on all platforms. This class attempts to handle those shortfalls and provide an easy mechanism for listening to state changes.CapsLockSupport cls = CapsLockSupport.getInstance(); // for get the current state of the caps lock key boolean currentState = cls.isCapsLockEnabled(); // for listening to changes in the caps lock state cls.addPropertyChangeListener("capsLockEnabled", myListener);
There is one special case to be aware of. IfCapsLockSupport
is not able to determine the state of the CAPS LOCK key, thenisInitialized()
will returnfalse
until it is able to introspect aKeyEvent
and determine the current locking state. IfCapsLockSupport
must use delayed initialization, it will fire a property change to notify listeners that it is now in an accurate state.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static class
CapsLockSupport.SingletonHolder
-
Field Summary
Fields Modifier and Type Field Description private boolean
capsLockeEnabled
private boolean
updateViaKeyEvent
private boolean
useToolkit
-
Constructor Summary
Constructors Modifier Constructor Description private
CapsLockSupport()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
dispatchKeyEvent(java.awt.event.KeyEvent e)
This is an implementation detail and should not be considered public.static CapsLockSupport
getInstance()
Gets the only instance ofCapsLockSupport
.boolean
isCapsLockEnabled()
Determines the current state of theCAPS LOCK key
.boolean
isInitialized()
Determines ifCapsLockSupport
is accurately synchronized with the state of the CAPS LOCK key.(package private) void
setCapsLockEnabled(boolean capsLockEnabled)
-
Methods inherited from class org.jdesktop.beans.AbstractBean
addPropertyChangeListener, addPropertyChangeListener, addVetoableChangeListener, addVetoableChangeListener, clone, fireIndexedPropertyChange, firePropertyChange, firePropertyChange, fireVetoableChange, fireVetoableChange, getPropertyChangeListeners, getPropertyChangeListeners, getVetoableChangeListeners, getVetoableChangeListeners, hasPropertyChangeListeners, hasVetoableChangeListeners, removePropertyChangeListener, removePropertyChangeListener, removeVetoableChangeListener, removeVetoableChangeListener
-
-
-
-
Method Detail
-
getInstance
public static CapsLockSupport getInstance()
Gets the only instance ofCapsLockSupport
.- Returns:
- the
CapsLockSupport
instance
-
isInitialized
public boolean isInitialized()
Determines ifCapsLockSupport
is accurately synchronized with the state of the CAPS LOCK key. When not initialized,isCapsLockEnabled()
will always returnfalse
.CapsLockSupport
will fail to initialize only ifToolkit#getLockingKeyState(int)
throws an exception; in that case, it will initialize as soon as it receives a valid key event (that can be used to determine the current locking state).- Returns:
true
ifCapsLockSupport
accurately knows the state of the CAPS LOCK key
-
isCapsLockEnabled
public boolean isCapsLockEnabled()
Determines the current state of theCAPS LOCK key
.- Returns:
true
if CAPS LOCK is enabled;false
otherwise
-
setCapsLockEnabled
void setCapsLockEnabled(boolean capsLockEnabled)
-
dispatchKeyEvent
public boolean dispatchKeyEvent(java.awt.event.KeyEvent e)
This is an implementation detail and should not be considered public.- Specified by:
dispatchKeyEvent
in interfacejava.awt.KeyEventDispatcher
-
-