How to intercept Ctrl key - eg Ctrl E being pressed on Java console program

A

Angus

Hello

I am writing console programs and my program waits streaming in text input
from the screen. But I need a way for the user to abort - I thought Ctrl
E - but how can I know when Ctrl E has been pressed.

I am roughly doing this sort of thing:

import java.io.*;

public class blah
{
public static void main(String[] args)
{
// declaration section:
BufferedReader cin = null; // console input
String conline;

try
{
cin = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter strings to remove last character");

System.out.println("waiting... Press Ctrl E to end session");
while ((conline = cin.readLine()) != null)
{
// If Ctrl E detected abort - break out - BUT HOW???

System.out.println("processing: " + conline);
int len = conline.length();
String strRemovedot = conline.substring(0,len-1);
System.out.println(strRemovedot);

}

cin.close();

}
catch (IOException e)
{
System.err.println("IOException: " + e);
}
}
}
 
I

Ingo Menger

Angus said:
Hello

I am writing console programs and my program waits streaming in text input
from the screen. But I need a way for the user to abort - I thought Ctrl
E - but how can I know when Ctrl E has been pressed.

Any reason why you do not want to use the normal way of ending terminal
input?
 
G

Gordon Beaton

I am very new to Java. What is the normal way?

These have nothing to do with Java:

Ctrl-C: interrupt
Ctrl-D: EOF on stdin (Unix etc)
Ctrl-Z: EOF on stdin (Dos/Windows)

/gordon
 
A

Angus

Maybe I should explain what I want to do more fully. My Java command line
program waits for data to be received from a socket server.

But user needs to be able to stop program waiting - and I need to cleanup.
So Ctrl C works but I don't get a chance to cleanup and report successful
closedown to user.

My code so far is a bit like this:

BufferedReader cin = new BufferedReader(new InputStreamReader(System.in));
String conline;
String responseLine;

for(;;)
{
// check not null stream and check user hasn't said quitread line from
user
if (((responseLine = bis.readLine()) != null) && (conline =
cin.readLine().equals("quit") == true)))
break;

System.out.println("Server said: " + responseLine);
}

Problem is that on the if line system is waiting for something to be typed
on command line. If nothing is typed it doesn't go onto display data from
server.

So I want it to continually display the data from server but have a way for
user to break out of for

By using this:

while ((responseLine = bis.readLine()) != null)
{
System.out.println("Server said: " + responseLine);
}

code worked but now way for user to break out of while loop.

Hopefully that explains what I need to achieve. Any help would be warmly
appreciated.

Angus
 
M

Michael Rauscher

Angus said:
Maybe I should explain what I want to do more fully. My Java command line
program waits for data to be received from a socket server.

But user needs to be able to stop program waiting - and I need to cleanup.
So Ctrl C works but I don't get a chance to cleanup and report successful
closedown to user.

Have a look at Runtime#addShutdownHook.

Bye
Michael
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top