Thread: Keyboard input problem

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    80

    Keyboard input problem

    This code does exactly what I've been looking for, for some time
    but, what does this code do. It doesn't ask for input at the
    second getch(). Could somebody please take theris time and
    explain this code quite detailed? Thanks in advance.
    Code:
    #include <iostream.h>
    #include <conio.h>
    
    int main()
    {
    	int c = getch();
    	if (c == 0 || c == 224)
    	{
    		c = 256 + getch();
    		if ( c == (256+72))
    			cout << "UP";
    	}
    }

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    The reason is that the if statement fails, so that is why you never reach the second getch().

    Code:
    int c = getch();
    The above line gets a character as input from the console. If the ASCII value returned is equal to 0 or 224, then execution enters the if block.

    This might be a better example to show you how it works:
    Code:
    #include <stdio.h>
    
    int main()
    {
      int c = getchar();
      if(c == 'a')
        printf("A was pressed\n");
      else if(c == 'b')
        printf("B was pressed\n");
      else
        printf("Some other key was pressed\n");
      return 0;
    }
    As you can see in the above code, I used getchar() instead of getch(). The reason is that getchar() is a portable version of getch(). The only difference is that getchar() requires you to press the enter key after entering in a character.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Some keys - for example cursor arrow keys like "UP" are transmitted as TWO bytes (hence two calls to getch() )
    The clue that byte 2 is coming is based on the two magic values of byte 1 (namely 0 or 224)

  4. #4
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    Pressing these keys places 2 bytes in the input buffer
    UP ARROW,PAGE UP,LEFT ARROW,RIGHT ARROW,END,DOWN ARROW,PAGE DOWN,INSERT,DELETE
    getch() when called then twice returns 224 in the first time, and another value key-dependant the 2nd time... Try to guess what 72 is...

    And.. ahumm
    Code:
    c = 256 + getch();
    if ( c == (256+72))
    How nice!
    Code:
    //Lets see if this works!
    int main(){
        int x;//let it have a random number!
        if(x+256 == x+256){
            std::cout << "Hey! you're right! You could this be??\n";
        }else{
            std::cout << "Get or buy a new compiler :P\n";
        }
    }

  5. #5
    Registered User
    Join Date
    Sep 2004
    Posts
    80
    Thanks for your replies, I understand this alot better now. All help for a noob like me is
    very appreciated.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem getting the input from a temp variable into the Array
    By hello_moto in forum C++ Programming
    Replies: 3
    Last Post: 03-16-2006, 01:50 AM
  2. problem : input and calculation working together
    By itay390 in forum C Programming
    Replies: 13
    Last Post: 07-30-2005, 12:32 PM
  3. Need help with keyboard input
    By wiramu in forum C++ Programming
    Replies: 2
    Last Post: 11-28-2003, 02:44 PM
  4. FAQ Keyboard Input ? (C++)
    By Malikive in forum FAQ Board
    Replies: 6
    Last Post: 11-07-2001, 09:30 PM
  5. Keyboard input ?
    By Malikive in forum Game Programming
    Replies: 4
    Last Post: 11-06-2001, 11:14 PM