Hey, I have a problem.
I want that when the user presses the ESC Key, my program will send him a message if he sure he wants to exit the program.

Basicaly what I need help with is how to do the "IF Esc is pressed"


Thanks

Has that worked for you? How?

Yes, it has. At least for the console applications in Windows.

#include <conio.h>
#include <iostream>

int main(int argc, char *argv[])
{
    char k;
    
    k = getch();
    
    if (k==27)
       printf("ESC pressed.\n");
       
    return EXIT_SUCCESS;
}

Try it. :)

Edited 6 Years Ago by shah1248: n/a

AFAIK ESC can not be detected using standard i/o, such as getchar() and fgets(). Same with other special keys such as function and arrow keys.

AFAIK ESC can not be detected using standard i/o, such as getchar() and fgets(). Same with other special keys such as function and arrow keys.

Not so. From the standard IO perspective, escape is just a regular character. getchar is perfectly happy to read it, and putchar - to print. You can easily verify that with a help of a trivial cat-like program

while((ch = getchar()) != EOF)
        putchar(ch)

feeding it a file with escape in it.

The problem lies outside the stdio. The offending party is either a console driver, or a framework, or whatever else, which intercepts the escape and interprets it (in case of Windows console, it is a console driver).

If the application is desperate to see the escape, it must configure the offensive driver, replace it or bypass it somehow. How to do it is not addressed by a programming language.

Edited 6 Years Ago by nezachem: n/a

Sorry but hitting the Esc key does nothing with vc++ 2008 express on Windows 7 Home Premium

int main()
{
    int ch;
    while((ch = getchar()) != EOF)
        printf("%d\n",ch);
}

In Borland 5.5 is erases the input.

Oh yes, vc++ 2008 express does that too. Hadn't noticed that because I had not typed any other characters before Esc key. But I see that behavior now.

I dual booted into Ubuntu and tried the same program. There pressing Esc prints ^[ until ^C is pressed, then it prints 27 for each time Esc was pressed.

Neither MS-Windows nor *nix produces the desired result. The only way I have found to get the desired result is with the functions in conio.h or using something like ncurses.

Sorry but hitting the Esc key does nothing

Now create a file with an escape, and run your program with input redirected from it.

There pressing Esc prints ^[

Yes, that's the way an xterm visualizes escape.

Edited 6 Years Ago by nezachem: n/a

Now create a file with an escape, and run your program with input redirected from it.
.

Why would I want to do that when all I want is to capture the Esc when pressed at the keyboard (and without pressing the Enter key too)?

Why would I want to do that when all I want is to capture the Esc when pressed at the keyboard (and without pressing the Enter key too)?

To make sure that stdio is capable of handling it.

Thanks everybody.. especially to shah1248, your code worked perfect! =]

Thanks!

Yes, it has. At least for the console applications in Windows.

#include <conio.h>
#include <iostream>

int main(int argc, char *argv[])
{
    char k;
    
    k = getch();
    
    if (k==27)
       printf("ESC pressed.\n");
       
    return EXIT_SUCCESS;
}

Try it. :)

#include <conio.h>
#include <iostream>
 
int main(int argc, char *argv[])
{
    char k;
 
    k = getch();
 
    if (k==27)
       printf("ESC pressed.\n");
 
    return EXIT_SUCCESS;
}

Agreed but what are you going to do about this thing -> conio.h Mind you, your code is not portable at this point. (Unless you make an exe out of it)

conio.h is a C header file used ONLY in old MS-DOS compilers to create text user interfaces. It is NOT part of the C standard library, ISO C nor is it required by POSIX.

Source - Wikipedia
So you might wanna look at what the other people have said above
:)

This question has already been answered. Start a new discussion instead.