United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
Bug ID: JDK-4414164 Toolkit.getLockingKeyState() does not return caps lock state
JDK-4414164 : Toolkit.getLockingKeyState() does not return caps lock state

Details
Type:
Bug
Submit Date:
2001-02-11
Status:
Open
Updated Date:
2009-05-27
Project Name:
JDK
Resolved Date:
Component:
client-libs
OS:
linux,windows_xp,windows_2000
Sub-Component:
java.awt
CPU:
x86
Priority:
P4
Resolution:
Unresolved
Affected Versions:
1.3.0,1.4.1,1.4.2,5.0
Targeted Versions:

Related Reports
Relates:

Sub Tasks

Description

Name: boT120536			Date: 02/11/2001


java version "1.3.0"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0)
Java HotSpot(TM) Client VM (build 1.3.0, mixed mode)

Calling toolkit.getLockingKeyState(KeyEvent.VK_CAPS_LOCK) returns whether or not
caps lock is currently held down, not whether or not caps lock is currently
enabled. This makes it pretty much useless, and I can't seem to find another way
to check for caps lock buing enabled.

Try compiling the sample program; every second it prints the caps lock state.
Try pressing caps lock; not that getLockingKeyState only returns true if you are
holding caps lock while it is called, if you press caps lock once (to enable it)
and release, then you get false back.

import java.awt.Toolkit;
import java.awt.event.KeyEvent;

public class CapsProb {
  public static void main(String args[]) {
    for (;;) {
      try {
	Thread.sleep(1000);
      } catch(InterruptedException excep) {}
      System.out.println("Caps lock on? " +
			 Toolkit.getDefaultToolkit().getLockingKeyState(
			   KeyEvent.VK_CAPS_LOCK));
    }
  }
}
(Review ID: 116730) 
======================================================================

Name: rmT116609			Date: 11/05/2003


FULL PRODUCT VERSION :
java version "1.4.2"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2-b28)
Java HotSpot(TM) Client VM (build 1.4.2-b28, mixed mode)

FULL OS VERSION :
Microsoft Windows XP [Version 5.1.2600]

A DESCRIPTION OF THE PROBLEM :
Under Windows, Toolkit.getLockingKeyState() works only if your application has created a Frame, and it may still return an incorrect result if the state changed when the focus was on a window other than the Frame.

I believe this is because the native Toolkit implementation uses the Win32 GetKeyboardState() API. If it used GetAsyncKeyState() instead, it would not be sensitive to whether the app has focus, and in fact wouldn't even require a Frame to be present in order to work correctly.

I consider this a bug, not an enhancement request. The documentation makes no mention of either of these prerequisites.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Compile and run the program attached below.


EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
The program should output "true" if the caps lock key is pressed.
ACTUAL -
The program emits an infinite series of "false"s.

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------

import java.awt.*;
import java.awt.event.*;

public class test
{
	public static void main(String[] args) throws Exception
	{
		for (;;)
		{
			System.out.println(Toolkit.getDefaultToolkit().getLockingKeyState(KeyEvent.VK_CAPS_LOCK));
			Thread.sleep(1000);
		}
	}
}

---------- END SOURCE ----------
(Review ID: 191863)
======================================================================

                                    

Comments
EVALUATION

Sounds like 4320043.  
eric.hawkes@eng 2001-02-11

Hmmm, actually I'm not sure they are related.  
eric.hawkes@eng 2001-02-11

Probably ought to be using GetKeyState instead of GetKeyboardState for win32.  
###@###.### 2002-09-09
                                     
2001-02-11



Hardware and Software, Engineered to Work Together