在Java中,捕获< tab>的最佳方法是什么?击键? [英] In Java, what is the best way to capture a <tab> keystroke?

查看:120
本文介绍了在Java中,捕获< tab>的最佳方法是什么?击键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法捕捉< tab>我的Java命令行应用程序中的击键。使用System.in.read()我在点击tab键时似乎没有得到任何东西。有什么方法可以解决这个问题?

I'm having trouble capturing the <tab> keystroke in my Java command-line application. Using System.in.read() I don't seem to get anything when hitting the tab key. What is the best way to approach this?

为了给出一些上下文,我试图允许用户点击tab键命令,让它自动完成命令(就像你在bash shell中做的那样)。如果有更好的方法来实现这一点,我愿意接受建议(也许使用System.in.read()不是解决这个问题的最佳方法吗?)。

To give some context, I'm trying to allow a user to hit the tab key mid-command to have it autocomplete the command (much like you might do in a bash shell). I'm open to suggestions if there are better approaches to achieving this (perhaps using System.in.read() isn't the best angle to approach this?).

推荐答案

查看 JLine 。我自己没用过。它使用Windows DLL(使用JNI)并且它支持linux以将控制台切换到字符/原始模式而不是缓冲模式。我以前从未使用过这个,所以使用风险自负。我也不是100%肯定它是否会解决你的问题,但值得一试:)

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 :)

编辑:我可以确认它确实如此work

I can confirm it does work

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

再次编辑:该库确实包含命令行的自动填充功能(通过按Tab键)...享受:)

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

这篇关于在Java中,捕获&lt; tab&gt;的最佳方法是什么?击键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆