我正在努力寻找一种在控制台窗口实现基本箭头键移动的方法。我已经找到了一个使用switch语句和一些变量的C#脚本,但我的老师坚持使用Java。
从其他线程中得出的答案似乎都表明,在Java中不可能实现,除非安装某些(如果我错了,请纠正我)像JNA和/或Jline这样的“框架”,但作为初学者,我甚至不知道那些东西是什么。
在你说我的老师是白痴之前,他从来没有说过必须要有箭头键移动,我只是觉得这很酷 :)
我正在努力寻找一种在控制台窗口实现基本箭头键移动的方法。我已经找到了一个使用switch语句和一些变量的C#脚本,但我的老师坚持使用Java。
从其他线程中得出的答案似乎都表明,在Java中不可能实现,除非安装某些(如果我错了,请纠正我)像JNA和/或Jline这样的“框架”,但作为初学者,我甚至不知道那些东西是什么。
在你说我的老师是白痴之前,他从来没有说过必须要有箭头键移动,我只是觉得这很酷 :)
InputStream in = System.in;
int next = 0;
do {
next = in.read();
System.out.println("Got " + next);
} while (next != -1);
现在,有两个问题:
There is no ascii code for the arrow keys, instead there are so called escape sequences that depend on the terminal emulator used, so on my Mac if I run the above code and hit Arrow-Up and then the return key I get the following output:
Got 27 // escape
Got 91
Got 65
Got 10 // newline
如果你只针对Unix平台,那么在这方面没有一个好的跨平台解决方案,javacurses 可以提供帮助。