0

I want to check - either by some method or event handler - if a user has pressed a key in a CLI application.

Something like that:

if(isWPressed)
    System.out.println("'W' is pressed");
if(isAPressed)
    System.out.println("'A' is pressed");

I've tried this answer: How do I check if the user is pressing a key?

But it didn't work and looking at imports I've noticed it uses awt, so it's not useful in CLI, where there are no windows to speak of. I also can't use Scanner for that (as suggested here: Detect a key press in console) as if user would press multiple buttons - like w and a I need to know both of them are pressed.

Miku
  • 567
  • 6
  • 15
  • Do you think this library might help? https://github.com/jline/jline3 – Joseph Chotard Mar 22 '20 at 16:38
  • It might be useful in future, but for this problem - I've glanced over it's javadoc and it doesn't seam to contain a way to achieve what I want. – Miku Mar 22 '20 at 17:08

1 Answers1

-2

YOu need to use keylistener, it permits to get the key

Example 

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package javaapplication7;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

/**
 *
 * @author wilso
 */
public class List implements KeyListener {

    @Override
    public void keyTyped(KeyEvent e) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void keyPressed(KeyEvent e) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void keyReleased(KeyEvent e) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

}

If you dont want to use awt, then you can use scanner or Event

@Override
public void nativeKeyPressed(NativeKeyEvent e) {
    String temp = NativeKeyEvent.getKeyText(e.getKeyCode());
    if (validHotKeys.contains(temp)) {
        if (!pressedHotKeys.contains(temp)) {
            pressedHotKeys.add(temp);
            System.out.println(pressedHotKeys.toString());
        }
    }
    if (validAlphaKeys.contains(temp)) {
        if (!pressedAlphaKeys.contains(temp)) {
            pressedAlphaKeys.add(temp);
            System.out.println(pressedAlphaKeys.toString());
        }
    }
}
Alejandro Gonzalez
  • 1,221
  • 4
  • 15
  • 30
  • He specified that he did not want to use awt in his question – Joseph Chotard Mar 22 '20 at 16:38
  • It's not that I don't want to use awt - I cannot use awt As for the second solution - sign - you need to expand on it. By googling I assume it's form JNativeHook library, which from what I see is poorly documented and does not show how `NativeKeyListener` can be used in console – Miku Mar 22 '20 at 17:04