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





PostPosted: 2006-11-17 19:42:00 Top

java-programmer, How to intercept Ctrl key - eg Ctrl E being pressed on Java console program 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);
}
}
}


 
Ingo Menger





PostPosted: 2006-11-17 22:55:00 Top

java-programmer >> How to intercept Ctrl key - eg Ctrl E being pressed on Java console program
Angus schrieb:

> 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?

 
Angus





PostPosted: 2006-11-18 1:02:00 Top

java-programmer >> How to intercept Ctrl key - eg Ctrl E being pressed on Java console program I am very new to Java. What is the normal way?

Angus



"Ingo Menger" <email***@***.com> wrote in message
news:email***@***.com...
>
> Angus schrieb:
>
> > 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?
>


 
 
Gordon Beaton





PostPosted: 2006-11-18 22:34:00 Top

java-programmer >> How to intercept Ctrl key - eg Ctrl E being pressed on Java console program On Fri, 17 Nov 2006 17:01:46 -0000, Angus wrote:
> 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

--
[ don't email me support questions or followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e
 
 
Angus





PostPosted: 2006-11-18 23:02:00 Top

java-programmer >> How to intercept Ctrl key - eg Ctrl E being pressed on Java console program 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




"Gordon Beaton" <email***@***.com> wrote in message
news:455f19bd$0$8092$email***@***.com...
> On Fri, 17 Nov 2006 17:01:46 -0000, Angus wrote:
> > 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
>
> --
> [ don't email me support questions or followups ]
> g o r d o n + n e w s @ b a l d e r 1 3 . s e


 
 
Michael Rauscher





PostPosted: 2006-11-19 0:20:00 Top

java-programmer >> How to intercept Ctrl key - eg Ctrl E being pressed on Java console program Angus schrieb:
> 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