getch() help

This is a discussion on getch() help within the Windows Programming forums, part of the Platform Specific Boards category; I wanted some code to get keyboard input and stumbled across getch(), to test it i wrote this code: Code: ...

  1. #1
    Registered User
    Join Date
    Mar 2005
    Location
    Bangkok
    Posts
    3

    getch() help

    I wanted some code to get keyboard input and stumbled across getch(), to test it i wrote this code:

    Code:
    #include <cstdlib>
    #include <iostream>
    #include <conio.h>
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
        int y;
        while (1)
        {
              y=getch();
              cout << y << "\n";
        }
    }
    when you run the program and press a key such as the arrow keys, it displays 2 numbers instead of one. The first number is always 224, and the second is unique to each key. I know getch() returns the ascii value, and the buttons that return 2 numbers don't have an ascii character equated to them. My question is how can you read the second of the two numbers in something like an "if" statement? For example sensing the arrow keys through getch().

  2. #2
    Registered User
    Join Date
    Jul 2005
    Posts
    69
    This should do. Of course I don't claim it's standard, portable, or anything else.
    Code:
              y = getch();
    	  if(y == 0 || y == 224)
    		  y = getch( );
    Regards,
    Brian

  3. #3
    Registered User
    Join Date
    Mar 2005
    Location
    Bangkok
    Posts
    3
    thanks! works perfect

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. Pls repair my basketball program
    By death_messiah12 in forum C++ Programming
    Replies: 10
    Last Post: 12-11-2006, 04:15 AM
  3. Pause a C program without getch()
    By swgh in forum C Programming
    Replies: 4
    Last Post: 02-20-2006, 10:24 AM
  4. Clearing input buffer after using getch()
    By milkydoo in forum C++ Programming
    Replies: 3
    Last Post: 07-21-2003, 11:04 PM
  5. Problems with getch()
    By GrNxxDaY in forum C++ Programming
    Replies: 14
    Last Post: 08-12-2002, 02:11 AM

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