Thread: Having getche() recognize tabs, enter, and backspace.

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    2

    Having getche() recognize tabs, enter, and backspace.

    I'm trying to write a program that will recognize when you press the tab, enter or backspace keys, and display the name of the key pressed, instead of actually doing a tab, enter or backspace. I've got everything figured out except how to recognize the keys. Can somebody help me out?


    Note: I'm extremely new to C.

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    247
    Hi there,

    first of all, you can use a conditional statement to recognize if any of the above keys have been entered....

    Code:
      ch = getche();
      if(ch == '\t') { /*do something*/ };
    secondly, as tab, backspace, are whitespace characters you will not be able to display on the screen these characters using the conventional method of printf("%c", ch); Alternatively you could use their hexadecimal equivalent to display the character entered.

    Code:
      ch = getche();
      if(ch == '\t') { 
         printf("%x", ch)
       };
    lastly, if your compiler fully implements the conio.h libraray you could use the function getch() instead of getche(); getch doesnot echo the input character to the display.

    Code:
       ch = getch();
      if(ch == '\t' || ch == '\b' || ch == '\r' || ch == '\n')
     { 
         printf("%x", ch)
       };
    Last edited by bigtamscot; 11-30-2001 at 01:55 AM.
    hoping to be certified (programming in c)
    here's the news - I'm officially certified.

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    2
    Thanks for your help

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You could also use isspace() to check for these characters, with the exception of '\b', which is not considered whitespace.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed