I made a program...
now i have to put one validation.
"while program is executed, if i press "Esc" button (hardly situated in top-left corner) i will be out of my program, means its exicutation directly takes end."

i tried by taking its ASCII value but i requires also ENTER key for ending the exicutation as i have to directly end the program...
i also tried by using "kbhit()" function..

now what should i do for it?

kbhit() is useful, but using getch() is also required. Understand what both functions do, and you should be able to figure out how to use them for your program.

kbhit() is useful, but using getch() is also required. Understand what both functions do, and you should be able to figure out how to use them for your program.

sorry, WaltP

I had already tried with both function "Kbhit()" and "getch()"..
but not working...
when i press "Esc" i want to directly end the executed program, but it requires ENTER key to end the exicutation.
hope you understood my qvery!!!!

remember that getch() and getchar() are not the same functions...
If you did use getch and kbhit, show some code so we can see what you did wrong.

remember that getch() and getchar() are not the same functions...

ya..I know there are lot difference between getch() and getchar() ..
and i also tried with getche() ..
but it can't

If you did use getch and kbhit, show some code so we can see what you did wrong.

I am talking about general any programs..
specially i had not made but In general when any program has been executed if we press "Esc" button the program suddenly goes to end. It should not require ENTER key for ending it..
hope you understood my qvery...

This worked for me, using vc++ 2008 Express. But also note that conio.h is non-standard and not supported by all compilers.

#include <windows.h>
#include <conio.h>
#include <stdio.h>
int main()
{
    int c;
    printf("Enter something\n");
    while( !_kbhit() )
        Sleep(100);
    while( _kbhit() )
    {
        c = _getch();
        printf("%d\n", c);
    }
}

>I had already tried with both function "Kbhit()" and "getch()"..
but not working...

You did it the wrong way, can you show us the code?
BTW, you've already said three times that you've tried it, but how can we believe you?
You didn't post your tries.

First you should try running the program that Ancient Dragon has posted, press the escape key.
After pressing the escape key, his program will report the equivalent key code for the escape key (this also works for other keys of course).
When you've obtained the correct key-code for the escape key, you can in your program compare against that value at the moment when a key is pressed.
When the comparison yields true, the escape key is pressed, and your program will shut down (in case you've implemented it correctly).
Edit:: I assumed here a comparison using the == operator.
Edit:: But in a do-while loop, you probably want to do something like this:

do {
    // Put other code here if necessary
} while( key != ESC );

(Here I assume that key is a variable which contains the value/code of read key, and that ESC should be replaced by the keycode you get by running AD's program and pressing the escape key)

Edit::
Forgot to mention that if AD's code won't compile correctly (only in case it isn't compiling, otherwise you don't change anything), then you'll have to try renaming _kbhit() to just kbhit(), because this function's name can differ from what implementation you're using.
(Same applies to getch())

This version of the same program will also catch special keys, such as <F1>. I make such keys negative to distinguish them from normal keys, but some people also just add 255 for the same purpose.

int main()
{
    int c;
    printf("Enter something\n");
    while( !_kbhit() )
        Sleep(100);
    while( _kbhit() )
    {
        c = _getch();
        if( c == 0 || c == 224)
        {
            c = - _getch();
        }
        printf("%d\n", c);
    }
}

This version of the same program will also catch special keys, such as <F1>. I make such keys negative to distinguish them from normal keys, but some people also just add 255 for the same purpose.

int main()
{
    int c;
    printf("Enter something\n");
    while( !_kbhit() )
        Sleep(100);
    while( _kbhit() )
    {
        c = _getch();
        if( c == 0 || c == 224)
        {
            c = - _getch();
        }
        printf("%d\n", c);
    }
}

As you told i tried your logic..
see your edited program...
it shows one error C:\TC\BIN\nirav\CPP1.CPP(12) : error C2065: 'sleep' : undeclared identifier

// And I am using Microsoft Visual C++ 6.0 

#include <conio.h>
#include <stdio.h>
#include <dos.h>

int main(void)
{
    int c;
    printf("Enter something\n");
    while( !kbhit() )
    sleep(100);
    while( kbhit() )
    {
        c = getch();
        if( c == 0 || c == 224)
        {
            c = - getch();
        }
        printf("%d\n", c);
    }

	return 0;
}

>As you told i tried your logic.. see your edited program... it shows one error
You have to write sleep() as [B]S[/B]leep() (it's written with an uppercase 'S').
And you'll have to add the following include directive at the top of your program as well: #include <windows.h> .

>And I am using Microsoft Visual C++ 6.0
Get a decent compiler.

>As you told i tried your logic.. see your edited program... it shows one error
You have to write sleep() as [B]S[/B]leep() (it's written with an uppercase 'S').
And you'll have to add the following include directive at the top of your program as well: #include <windows.h> .

With an uppercase on window compilers.

If you're going to send him on the windows.h route, perhaps this might be of relevance: http://msdn.microsoft.com/en-us/library/ms646293(VS.85).aspx

With an uppercase on window compilers.

If you're going to send him on the windows.h route, perhaps this might be of relevance: http://msdn.microsoft.com/en-us/library/ms646293(VS.85).aspx

Well, he said he used a Windows compiler, so windows.h is available.
Also: he just wanted to run a modified example of AD's code, I just said him how to fix his code.
Probably because he started talking about getch() and kbhit() , AD has proposed him a code example using those functions instead of using GetAsyncKeyState, but this function will do fine as well :)

>With an uppercase on window compilers.
Yes, I forgot to specify that, on *nix systems it is written in lowercase.

This article has been dead for over six months. Start a new discussion instead.