How to write an equivalent of KeyListener with JLine?

一笑奈何 提交于 2019-12-22 13:49:40

问题


I am working on a console application written in Java. What I have to do is handle user keyboard input. When a long process in launched with our program in a terminal, the user must have the possibility to press 'q' at anytime to stop the process (which is running on a separate thread).

I've tried several things :

  • Running in another different thread something that read user input and throws an InterruptedException to the process' thread
  • Using JLine and its ConsoleReader
  • Using JLine and add a TriggeredAction linked to a keyboard key

But each time, I face the same problem : the user has to press ENTER key, and I don't want that.

Thanks for help if you have any ideas, or the actual solution of my problem.

PS : Please, if you think this is impossible, don't answer. I know it's possible.


回答1:


Perhaps you could use one of these libraries,

http://sourceforge.net/projects/jxgrabkey/ for linux

https://code.google.com/p/jintellitype/ for windows

Less related with your question, but I think can help you,

http://sourceforge.net/projects/javacurses/

The problem, with all of this, is that you will lost the platform independence.



来源:https://stackoverflow.com/questions/17089546/how-to-write-an-equivalent-of-keylistener-with-jline

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!