• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Problem Reading CTRL+C Using Jline API

 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a simple, console-based application that has a command/repsonse interface that uses jline. My code looks like this:

<blockquote>code:
<pre name="code" class="core">import jline.*

ConsoleReader reader = new ConsoleReader();
String response1 = reader.readLine("enter first response").trim();
String response2 = reader.readLine("enter second response").trim();
</pre>
</blockquote>

Pretty easy stuff. The problem is that I would like to be able to send a kill message to my script using the "CTRL+C" key combination. Jline "swallows" this virtual key, and doesn't act on it unless you tell it to do so.

I therefore threw together some test code that does that:

<blockquote>code:
<pre name="code" class="core">ConsoleReader reader = new ConsoleReader();
char ctrlC = reader.readVirtualKey();
if (ctrlC == ConsoleOperations.CTRL_C) { System.exit(1); }
</pre>
</blockquote>

The problem is that I don't know how to "merge" these two code snippets together. Has anyone done something like this before? If so, how did you do it?

Thanks in advance!
 
If you have a bad day in October, have a slice of banana cream pie. And this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic