changeset 4658:53427895abd7

7124368: UnsupportedOperationException is thown on getLockingKeyState() Reviewed-by: anthony
author leonidr
date Mon, 16 Jan 2012 15:49:14 +0300
parents e6af2dd87c5b
children ffe93c01d5be
files src/macosx/classes/sun/lwawt/macosx/LWCToolkit.java src/macosx/native/sun/awt/LWCToolkit.m
diffstat 2 files changed, 40 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/src/macosx/classes/sun/lwawt/macosx/LWCToolkit.java	Fri Jan 13 09:25:10 2012 -0800
+++ b/src/macosx/classes/sun/lwawt/macosx/LWCToolkit.java	Mon Jan 16 15:49:14 2012 +0300
@@ -31,6 +31,7 @@
 import java.awt.dnd.peer.DragSourceContextPeer;
 import java.awt.event.InputEvent;
 import java.awt.event.InvocationEvent;
+import java.awt.event.KeyEvent;
 import java.awt.im.InputMethodHighlight;
 import java.awt.peer.*;
 import java.lang.reflect.*;
@@ -376,11 +377,31 @@
         return new CRobot(target, (CGraphicsDevice)screen);
     }
 
+    private native boolean isCapsLockOn();
+
     /*
-     * TODO: take a look at the Cocoa API for this method, return false for now.
+     * NOTE: Among the keys this method is supposed to check,
+     * only Caps Lock works as a true locking key with OS X.
+     * There is no Scroll Lock key on modern Apple keyboards,
+     * and with a PC keyboard plugged in Scroll Lock is simply
+     * ignored: no LED lights up if you press it.
+     * The key located at the same position on Apple keyboards
+     * as Num Lock on PC keyboards is called Clear, doesn't lock
+     * anything and is used for entirely different purpose.
      */
     public boolean getLockingKeyState(int keyCode) throws UnsupportedOperationException {
-        return false;
+        switch (keyCode) {
+            case KeyEvent.VK_NUM_LOCK:
+            case KeyEvent.VK_SCROLL_LOCK:
+            case KeyEvent.VK_KANA_LOCK:
+                throw new UnsupportedOperationException("Toolkit.getLockingKeyState");
+
+            case KeyEvent.VK_CAPS_LOCK:
+                return isCapsLockOn();
+
+            default:
+                throw new IllegalArgumentException("invalid key for Toolkit.getLockingKeyState");
+        }
     }
 
     /*
--- a/src/macosx/native/sun/awt/LWCToolkit.m	Fri Jan 13 09:25:10 2012 -0800
+++ b/src/macosx/native/sun/awt/LWCToolkit.m	Mon Jan 16 15:49:14 2012 +0300
@@ -352,6 +352,23 @@
 }
 
 /*
+ * Class:     sun_lwawt_macosx_LWCToolkit
+ * Method:    isCapsLockOn
+ * Signature: ()Z
+ */
+JNIEXPORT jboolean JNICALL Java_sun_lwawt_macosx_LWCToolkit_isCapsLockOn
+(JNIEnv *env, jobject self)
+{
+    __block jboolean isOn = JNI_FALSE;
+    [JNFRunLoop performOnMainThreadWaiting:YES withBlock:^(){
+        NSUInteger modifiers = [NSEvent modifierFlags];
+        isOn = (modifiers & NSAlphaShiftKeyMask) != 0;
+    }];
+
+    return isOn;
+}
+
+/*
  * Class:     sun_awt_SunToolkit
  * Method:    closeSplashScreen
  * Signature: ()V