JDK-4414164 : Toolkit.getLockingKeyState() does not return caps lock state
  • Type: Bug
  • Component: client-libs
  • Sub-Component: java.awt
  • Affected Version: 1.3.0,1.4.1,1.4.2,5.0
  • Priority: P4
  • Status: Open
  • Resolution: Unresolved
  • OS: linux,windows_2000,windows_xp
  • CPU: x86
  • Submitted: 2001-02-11
  • Updated: 2020-02-25
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
Other
tbdUnresolved
Related Reports
Relates :  
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
- this is an issue reported against 7(7u), - there are now affected version 9 filed for this issue - 7u issues are transferred to Sustaining Nevertheless if someone have a report against 9 - please reopen and add affectedVersion 9 or 7u specific escalations might be reopen to Sustaining
10-08-2014

- this is an issue reported against 7(7u), - there are now affected version 9 filed for this issue - 7u issues are transferred to Sustaining Nevertheless if someone have a report against 9 - please reopen and add affectedVersion 9 or 7u specific escalations might be reopen to Sustaining
10-08-2014

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
11-02-2001