Thread: How to trap ESC key pressed without getch()?

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    14

    How to trap ESC key pressed without getch()?

    Friends i m doing a program in which user enters as many numbers as he want
    until he press ESC key.
    Do you know any function like kbhit() to get know whether ESC is pressed or
    not. plz help me.
    Also hint me if there is another way to do this program.
    thanks in adavance.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    There are ways of doing what you ask, but they may be beyond the difficulty of what you need.

    Take for example, the user is entering numbers:

    125 <enter>
    7934 <enter>
    81 <enter>

    Now the user is done entering numbers and wants to quit. What would be more natural than:

    ESC or q <enter> /* ESC or q to quit */

    ?

    That's what I do for my (hobby) programs these days.

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    > What would be more natural
    Actually I think what he wants makes some sense. q has an underlying integer value of it's own after all. As does ESC, but ESC can be easier to recognize since it is a functional key.

    The OP should read the parts about text in our FAQ. There's a section in there that explains why/how to use GetAsyncKey() from the Win32 API for this. kbhit() isn't the right function for this though: it doesn't capture any input.
    Last edited by whiteflags; 08-08-2007 at 01:30 AM.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    kbhit() does do this, however.
    Do you know any function like kbhit() to get know whether ESC is pressed or not.
    i.e., it returns true if there's a keypress waiting in the input buffer.

    For the most sense, I suggest using EOF to indicate the end of the numbers. Assuming that you don't get any other user input afterwards. EOF can be typed from the keyboard with CTRL-Z on windows and CTRL-D on Linux. You detect for it by checking the return value of getchar() or some other line-buffered single character input function against EOF, or the return value of scanf(), among other ways.

    Actually, what I would suggest is using scanf(): that way, the user can type ESCAPE or CTRL-Z or "q" or any invalid number to stop entering numbers.
    Code:
    int x, rv, c;
    
    while((rv = scanf("&#37;d", &x)) == 1) {
        printf("Processing %d\n", x);
    }
    
    if(rv != EOF) {
        while((c = getchar()) != EOF && c != '\n');  /* strip excess characters from stdin */
    }
    Something like that.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 11-23-2007, 01:48 PM
  2. Virtual keys
    By Arkanos in forum Windows Programming
    Replies: 4
    Last Post: 12-12-2005, 10:00 AM
  3. Directional Keys - Useing in Console
    By RoD in forum C++ Programming
    Replies: 38
    Last Post: 10-06-2002, 04:42 PM
  4. FAQ: Directional Keys - Useing in Console
    By RoD in forum FAQ Board
    Replies: 38
    Last Post: 10-06-2002, 04:42 PM