Wrox Programmer Forums
|
C++ Programming General discussions for the C++ language. For questions specific to Microsoft's Visual C++ variant, see the Visual C++ forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C++ Programming section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old April 26th, 2009, 03:41 PM
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









Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.