Reputation: 1955
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?
Upvotes: 3
Views: 2307
Reputation: 778
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?
Upvotes: 2