3

I'm trying to build an input form with several checks. One of those is to check if CapsLock is active. It works if I try to build this function together with Java Swing, see code below. But in JavaFX, it DOES not work at all. I get the same state every time I check; it seems like my application just asks for the initial state, and then saves it, and present it further...

JavaSwing (Works just fine)

frame.addKeyListener(new KeyListener() {
        public void keyTyped(KeyEvent e) {
            if(e.getKeyCode() == KeyEvent.VK_CAPS_LOCK){
                System.out.println(Toolkit.getDefaultToolkit().getLockingKeyState(KeyEvent.VK_CAPS_LOCK));
            } else if(e.isShiftDown()){
                System.out.println("SHIFT");
            }
        }

        public void keyPressed(KeyEvent e) {
            if(e.getKeyCode() == KeyEvent.VK_CAPS_LOCK){
                System.out.println(Toolkit.getDefaultToolkit().getLockingKeyState(KeyEvent.VK_CAPS_LOCK));
            } else if(e.isShiftDown()){
                System.out.println("SHIFT");
            }
        }

        public void keyReleased(KeyEvent e) {
            if(e.getKeyCode() == KeyEvent.VK_CAPS_LOCK){
                System.out.println(Toolkit.getDefaultToolkit().getLockingKeyState(KeyEvent.VK_CAPS_LOCK));
            } else if(e.isShiftDown()){
                System.out.println("SHIFT");
            }
        }
});

JavaFX (Always present the same state)

scene.setOnKeyReleased(new EventHandler<javafx.scene.input.KeyEvent>() {
    @Override
    public void handle(javafx.scene.input.KeyEvent event) {
        if(event.getCode() == KeyCode.CAPS){
            System.out.println("CAPS");
            System.out.println(Toolkit.getDefaultToolkit().getLockingKeyState(KeyEvent.VK_CAPS_LOCK));
        }
    }
});

Does anyone know why? What can i do?

Gary
  • 13,303
  • 18
  • 49
  • 71
user2734182
  • 1,835
  • 4
  • 17
  • 22

1 Answers1

2

Edit: It seems like the issue is a windows related one. This question has an answer that might work for you

This works for me with the following console output when pressing caps lock repeatedly:

Capslock pressed

Capslock state: true

Capslock pressed

Capslock state: false

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.input.KeyCode;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;

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

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {

        Pane root = new Pane();
        Scene scene = new Scene(root, 500, 500);

        primaryStage.setScene(scene);
        primaryStage.show();

        //scene.setOnKeyPressed( event -> {
        scene.setOnKeyReleased( event -> {
            if ( event.getCode() == KeyCode.CAPS ) {
                System.out.println("Capslock pressed");
                System.out.println("Capslock state: " + Toolkit.getDefaultToolkit().getLockingKeyState(KeyEvent.VK_CAPS_LOCK));
            }
        });

    }

    public static void main(String[] args) {
        launch(args);
    }
}

I'm not sure what the issue is?

Community
  • 1
  • 1
JohnRW
  • 748
  • 7
  • 22
  • It's very interesting. Which Java version are you using? Do you use OpenJDK? Tested your code and it does not work as you declares :( – user2734182 Mar 22 '16 at 10:38
  • from your question i didn't realise you were using openjdk so maybe its a problem with openjdk - any particular reason why you are using it instead of oracle's jdk? – JohnRW Mar 22 '16 at 10:43
  • No, I'm not using OpenJDK... I using JDK 1.8.0_71... I thought you might use OpenJDK then you got it to work ... just a thought =) – user2734182 Mar 22 '16 at 10:54
  • Did you try to run your compiled jar on another machine? Just to see if it's your code/libraries or the machine/keyboard messing up? – JohnRW Mar 22 '16 at 10:57
  • I have tried to build a snapshot, and run it on a laptop... Same result... I have also tried with options of JDK-versions... No progress at all.. Still the same ... Very strange... The question is why do you got a different result? ;) – user2734182 Mar 22 '16 at 11:31
  • That is indeed weird. Could you try it with _66 or _72 to make sure its not a library issue? If that does not change anything, it is not a library issue. Do all the systems you tested with run the same os? [proof that it works for me :P](http://i.imgur.com/7vpqFJe.png) – JohnRW Mar 22 '16 at 11:35
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/107026/discussion-between-user2734182-and-johnrw). – user2734182 Mar 22 '16 at 11:59
  • It seems like the issue is a windows related one. [This question has an answer that might work for you](http://stackoverflow.com/questions/12020835/how-do-i-check-if-the-caps-lock-key-is-pressed) – JohnRW Mar 22 '16 at 13:50