C Program - Escape Key as an input string

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • animeprogrammer
    New Member
    • Jan 2014
    • 13

    C Program - Escape Key as an input string

    hello fellow programmers, Im having problems regarding this code, if this is impossible please let me know.

    What i have here is a code that inputs as string. If the user press the "escape key" the program will go to a user-defined function.

    Code:
    #include "maidcafe.h" ///user-defined..it has all the header files i can imagine xD///
    .....
    if (strcmp(string, (escape key)) == 0){
    fn_menu(); // this will make the program go back to the menu (user-defined function)
    }
    else{
    /////do nothing
    }
    Can you guys tell me what should i put in the "(escape key)"

    Thanks in Advance,
    AnimeProgrammer :)
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    The first problem you have is that scanf requires you press the enter key so you can't leave scanf by pressing ESC.

    This applies to all keyboard input. Your program stops and the keyboard code executes until you press enter. What you see in your variable is the enter key as the last key pressed.

    Just have the user enter a character and test that character to make your jump to another function.

    The only way out is to write your own keyboard code that does not rely on the enter key. Everybody has done that. The scanf sort of thing is just to get you started.

    Comment

    • mikele
      New Member
      • May 2014
      • 6

      #3
      Do like this and it will work (at least with gcc)
      Code:
      int main()
      {
      	if(getche() == '\e' )
                     fn_menu(); // if 1 instruction, no need for brackets
      	else; // do nothing
      	return 0;
      }
      You don't even need to press enter :D

      Comment

      • donbock
        Recognized Expert Top Contributor
        • Mar 2008
        • 2427

        #4
        getche is not a standard function. Your code is more portable if you use getchar.

        Whether you need to press enter to get your entry recognized depends not on what get function you call, but on whether or not the input stream is buffered. Whether or not stdin is buffered is implementation-dependent. Refer to setvbuf for details.

        Comment

        Working...