By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,779 Members | 1,078 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,779 IT Pros & Developers. It's quick & easy.

How to catch Ctrl+C in console app ?

P: n/a
I have console program, which runs infinite cycle in one thread. This thread
makes output to stdout with System.out().
In main() I have:

public static void main(String args[])
{
// ... starting new thread, which writes to stdout

int read = 0;

do
{
try
{
read = System.in.read();
}
catch (IOException e)
{
break;
}
} while (read != 120); // 120 means letter 'x'

System.out.println("Done !");
}

But the thread, that I run doesn't write anything in stdout.
And when I press 'x', "Done !" not appears and the application not finishes.

I think the problem is in System.in.read().
Maybe some another methods exist to handle keyboard events from console.

Help me plz.

Thanx.
Jul 17 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
"Alex Ostrikov" <ao*****@hotmail.com> wrote in message news:<qAevb.146$Yt4.55@lakeread05>...
I have console program, which runs infinite cycle in one thread. This thread
makes output to stdout with System.out().
In main() I have:

public static void main(String args[])
{
// ... starting new thread, which writes to stdout

int read = 0;

do
{
try
{
read = System.in.read();
}
catch (IOException e)
{
break;
}
} while (read != 120); // 120 means letter 'x'

System.out.println("Done !");
}

But the thread, that I run doesn't write anything in stdout.
And when I press 'x', "Done !" not appears and the application not finishes.

I think the problem is in System.in.read().
Maybe some another methods exist to handle keyboard events from console.

Help me plz.

Thanx. doesn't write anything

Because you don't call print/write method for variable read.
Jul 17 '05 #2

P: n/a
Alex Ostrikov wrote:
I have console program, which runs infinite cycle in one thread. This
thread makes output to stdout with System.out().
In main() I have:

public static void main(String args[])
{
// ... starting new thread, which writes to stdout

int read = 0;

do
{
try
{
read = System.in.read();
}
catch (IOException e)
{
break;
}
} while (read != 120); // 120 means letter 'x'

System.out.println("Done !");
}

But the thread, that I run doesn't write anything in stdout.
And when I press 'x', "Done !" not appears and the application not
finishes.

I think the problem is in System.in.read().
Maybe some another methods exist to handle keyboard events from console.

Help me plz.

Thanx.

Dear Alex

I am a computer science student and I have tried your program. If you press
ENTER after entering 'x' the loop will be terminated and the program exits.
I think this is because the input stream is flushed.
Jul 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.