lundi 10 février 2020

How to use jnativehook with a String/Key that I can redefine at any time?

I'm working on a program that has a changeable hotkey. In the GUI you define the start/stop hotkey in a JTextField, then the if statement will work if the JTextField matches the key pressed. However it doesn't seem to work.

@Override
public void nativeKeyPressed(NativeKeyEvent e) {

    runStrt0 = start0.getText(); //JTextFile String (I was using "X" for testing)

    if (NativeKeyEvent.getKeyText(e.getKeyCode()) == runStrt0) { //If The key I pressed matches my JTextField
        System.out.println(runStrt0); //Print out the JTextField (Just for testing)
    } else { //If it doesn't match
        System.out.println("'" + e.getKeyCode() + "'"); //Print the key I pressed
        System.out.println("'" + runStrt0 + "'"); //Print JTextField string
        }
}

This code returns this.

X
X

This seems right to me, as it works when I define the string in another one of my programs with a predefined hotkey.

    @Override
public void nativeKeyPressed(NativeKeyEvent e) {
    String ifState = "P";
    if (NativeKeyEvent.getKeyText(e.getKeyCode()) == ifState) {
        Thread thing2run = new Thread(new RunnableClass());
        thing2run.start();
    }
}

That's just a sample code from a different program of mine. It works very well when I click "P". So what am I doing wrong with the first code? What do I need to change, so that I can change my hotkey to whatever I want by simply typing something new in the JTextField?

Aucun commentaire:

Enregistrer un commentaire