View Single Post
  #1 (permalink)  
Old April 26th, 2009, 03:41 PM
ha9258 ha9258 is offline
Registered User
 
Join Date: Mar 2009
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default problem with _getch

Hello

I am having trouble figuring out why the _getch() function read some extended keys as printable chars. I wrote a function that read chars from the keyboard and store them in a string variable. The user can use backspace to delete a character and the enter key to end input and return the string.

I taught that everything was fine until i accidentally press one of the Left Arrow keys and the letter 'K' was displayed. I explored further and found that other extended keys displayed printable chars:

Left Arrow: K, Right Arrow: M, Up Arrow: H, Down Arrow: P
PrtScn: R, Home: G, End: O, Delete: S
PgUp: I, PgDn: Q

If anyone can help me figure this out, i would appreciate it.

Thanks

Here is a code snippet of the function:
Code:
void getString( char * buffer, int size )
{
    const int Max = size - 1 ;
    char return = '\r' ;
    char backspace = '\b' ;
 
    while (( Ch = _getch() ) != return )
    {
         if (Ch == 0 ) {
             // ignore extended keys
             Ch = _getch() ;
         }
        else if ( Ch == backspace ) {
             // remove char at end, update string
        }
        else if ( ( Ch >= 32 ) && ( Ch <= 122) ) {
             if ( strlen( buffer) < Max ) {
                // echo char and add it to buffer
             }
        }
 
    } // end while
    
 } // end function
I
Reply With Quote