Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2006

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Angus - 17 Nov 2006 11:42 GMT
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 - 17 Nov 2006 14:54 GMT
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 - 17 Nov 2006 17:01 GMT
I am very new to Java.  What is the normal way?

Angus

> Angus schrieb:
>
[quoted text clipped - 6 lines]
> Any reason why you do not want to use the normal way of ending terminal
> input?
Gordon Beaton - 18 Nov 2006 14:33 GMT
> 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

Signature

[ 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 - 18 Nov 2006 15:01 GMT
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

> > I am very new to Java.  What is the normal way?
>
[quoted text clipped - 9 lines]
> [ 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 - 18 Nov 2006 16:19 GMT
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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.