Getting the current state of a drop in Java

Using the following code:

Toolkit.getDefaultToolkit().getLockingKeyState(KeyEvent.VK_CAPS_LOCK) 

Unfortunately, only false is returned.

According to the docs this should work: http://docs.oracle.com/javase/8/docs/api/java/awt/Toolkit.html#getLockingKeyState-int-

Runnable example:

 import java.awt.Toolkit; import java.awt.event.KeyEvent; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class Foo { public static void main(String[] args) { ExecutorService executorService = Executors.newFixedThreadPool(1); executorService.execute(new Runnable() { public void run() { while(true) { try{ Thread.sleep(250); }catch(Exception ignored) { } System.out.println("Capslock state: " + Toolkit.getDefaultToolkit().getLockingKeyState(KeyEvent.VK_CAPS_LOCK)); } } }); executorService.shutdown(); } } 

It should be added that I use Java SE 1.8 for Windows and that my Caplock key works fine

Screenshot of console output:

enter image description here

+5
source share
2 answers

Your code works fine for me. I just pasted your code, compiled it, launched it and pressed the lock button during operation. Here is the result:

 $ javac Foo.java $ java Foo Capslock state: false Capslock state: false Capslock state: false Capslock state: false Capslock state: true Capslock state: true Capslock state: true Capslock state: true Capslock state: true Capslock state: true Capslock state: true Capslock state: true Capslock state: false Capslock state: false Capslock state: false ^C 

I am using java 8 like you.

 $ java -version java version "1.8.0_25" Java(TM) SE Runtime Environment (build 1.8.0_25-b17) Java HotSpot(TM) 64-Bit Server VM (build 25.25-b02, mixed mode) 

Does the lock key on your caps work?

+2
source

Your code works fine on java 1.7.0_25 and 6. Just copy it and run using netbeans.

Here is the result

 Capslock state: true Capslock state: true Capslock state: true Capslock state: true Capslock state: true Capslock state: true Capslock state: true Capslock state: true pslock state: true Capslock state: true 

It should work on java 8, have you tried using the IDE?

I am using windows 7.

0
source

Source: https://habr.com/ru/post/1210088/


All Articles