In Java, what is the best way to capture a keystroke?

前端 未结 3 1135
生来不讨喜
生来不讨喜 2020-12-21 05:13

I\'m having trouble capturing the keystroke in my Java command-line application. Using System.in.read() I don\'t seem to get anything when hitting the tab key. W

3条回答
  •  遥遥无期
    2020-12-21 05:48

    Have a look at JLine. I have not used it myself. It uses a windows DLL (using JNI) and it has linux support to switch the console to character/raw mode instead of buffered mode. I have never used this before so use at your own risk. I am also not 100% sure if it will address your issue, but its worth a shot :)

    EDIT: I can confirm it does work

    ConsoleReader cr = new ConsoleReader();
    while (cr.readVirtualKey() != 0x09){
      //loop till Tab is pressed
    }
    

    EDIT AGAIN: The library does contain autocomplete (by pressing tab) for the command line... Enjoy :)

提交回复
热议问题