In Java, what is the best way to capture the <tab> key press?

I am having trouble capturing a <tab> keystroke in my Java command line application. Using System.in.read () It seems I don’t see anything when I press the tab key. What is the best way to approach this?

To give some context, I am trying to allow the user to hit the middle of the tab command so that it automatically populates this command (as you could do in the bash shell). I am open to suggestions if there are more efficient approaches to achieve this (maybe using System.in.read () is not the best angle for this?).

+4
source share
3 answers

Take a look at JLine . I did not use it myself. It uses the Windows DLL (using JNI) and has linux support for switching the console to character mode / raw instead of buffered mode. I have never used this before to use it at my own risk. I am also not 100% sure if he will consider your problem, but it is worth doing :)

EDIT: I can confirm that it works

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

EDIT AGAIN: The library has autocomplete (by clicking a tab) for the command line ... Enjoy :)

+7
source

I would suggest that your shell captures a tab and prevents it from accessing your application. Perhaps you can do nothing about it ...

0
source

Data from an InputStream only becomes available when the user presses the enter button. I think you will find that if you press Enter after a tab, a tab will appear.

There is something similar in c-applications: the shell sets the input mode to raw from cooked, since it needs raw keystrokes. Readline is the usual library used for this on Linux. I'm not sure what is common in Windows. As far as I can tell, something using JNI is the only option here.

0
source

Source: https://habr.com/ru/post/1334420/


All Articles