getch problem

This is a discussion on getch problem within the C++ Programming forums, part of the General Programming Boards category; Hi can any one plaase tell me the VC++ (or Dev C++) equivalent of the getch() function (Turbo C++ under ...

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    5

    Talking getch problem

    Hi
    can any one plaase tell me the VC++ (or Dev C++) equivalent of the getch() function (Turbo C++ under DOS). I am converting an old DOS based project to VC++ and Dev C++ consols application. but i found that the function getch() is showing different behavior. In Turbo C++ (under DOS) when i press Alt + F it was resulting in to a sequence of two numbers i.e ZERO and another numeric value. but in VC++ and Dev C++ it is returning the ascii value of 'F'.

    so in short i want a function in VC++/Dev C++, which can detect and return values for such key combinations like Alt+KEY, Ctrl+KEY.

    For more information please look at the code

    Can any one change this function “int getKey()” below so that it can identify Key combination like Alt+f, Ctrl+f etc.

    int isPrintable(int ch)
    {
    // Returns true if ch is a printable character
    if(ch>126 || ch<32)
    return false;
    else
    return true;
    }

    int getKey()
    {
    /*

    This function need change as in case of Dev C++, Alt+Key, Ctrl+Key are producing
    the same ASCII values as thet of the Key itself. Example Alt + f = f =102.
    */
    int ch;
    ch=getch();
    if(isPrintable(ch)) // Ordinary key
    return ch;
    else
    {
    if(ch==224 || ch==0) // Special key
    ch=getch();
    ch+=1000; // Adding 1000 for special keys so that they can be identified
    return ch;
    }
    }

    Target:
    Windows, VC++/Dev C++





    Thanks
    Last edited by rajesh1978; 07-04-2008 at 06:59 AM.

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    I think you are going to have to use a different function to get the modifier keys, but I couldn't tell you which one without looking it up myself.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Did you read the MSDN description of _getch()
    None of these functions can be used to read CTRL+C. When reading a function key or an arrow key, each function must be called twice; the first call returns 0 or 0xE0, and the second call returns the actual key code.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,822
    Was your answer to avoid putting code tags in your post to turn OFF Javascript?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Jul 2008
    Posts
    5
    Hi
    strangely, i found that the _getch() function is not returning 0 or 0xE0 as stated in the MSDN reference, how ever it is [ getch() ] doing so in Turbo C++ under DOS. Kindly check the following code
    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <ctype.h>
    
    int getKey() 
    {
    	/*
    		This function need change as in case of Dev C++, Alt+Key, Ctrl+Key are producing
            the same ASCII values as thet of the Key itself. Example Alt + f = f =102.
    	*/
        int ch;
        ch=_getch();
        
    	if(ch==0xE0)// Special key
    	{
    		ch=_getch();
        	ch+=1000; // Adding 1000 for special keys so that they can be identified
    	}
    	
    	return ch;
    }
    
    int main(void)
    {
    
    	int ch = 0;
    
    	while(true)
    	{
    		ch = getKey();
    		printf("\n&#37;d", ch);
    	}
    	
    	return 0;
    }

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    When compiled with gcc on Windows XP, it works as I would expect. If I press "home" on the keypad, it gives 1079, uparrow gives 1072. Pressing arrow-keys on the numeric portion of the keyboard gives 0 and some other number (e.g 72 for uparrow).

    However, Windows appear to handle ALT before the _getch() can see it. You would need to use some other method to identify that ALT is pressed. GetAsyncKeyState(VK_MENU) would give you the state of the ALT key itself.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Words and lines count problem
    By emo in forum C Programming
    Replies: 1
    Last Post: 07-12-2005, 04:36 PM
  2. Problem with my file opener
    By kzar in forum C Programming
    Replies: 7
    Last Post: 04-20-2005, 05:20 PM
  3. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 09:37 AM
  4. binary tree problem - help needed
    By sanju in forum C Programming
    Replies: 4
    Last Post: 10-16-2002, 06:18 AM
  5. Problem with getch() and fgetc()
    By Krupux in forum C Programming
    Replies: 3
    Last Post: 10-02-2002, 12:38 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21