Arrow key input?????

This is a discussion on Arrow key input????? within the C++ Programming forums, part of the General Programming Boards category; How do you get input for the arrow keys??? I tried getche() and it gave me a weird a that ...

  1. #1
    Registered User
    Join Date
    Jan 2004
    Posts
    8

    Question Arrow key input?????

    How do you get input for the arrow keys??? I tried getche() and it gave me a weird a that kinda looked like a fish sign. and i tried the FAQ's. (That search tool is so unhelpfull that it has no purpose) Thank in advance.

  2. #2
    Registered User
    Join Date
    Aug 2003
    Posts
    1,209
    This piece of code should work:
    Code:
    int test;
    for(int i=1;i<250;i++)
    {
        test = getch();
        if ( test == 0 || test == 224 )
            test = 256 + getch();
        cout << test << endl;
    }

  3. #3
    ...
    ... is offline
    Registered User
    Join Date
    Feb 2002
    Posts
    465
    if you are programming in a windows environment, one easy method is to use GetAsyncKeyState( VK_CODE )

    if you are checking for letter input, just check the capitol letter of the key you are checking for in single quotes, such as

    GetAsyncKeyState( 'A' );

    otherwise you need to know the virtual key codes. some useful ones are

    VK_UP
    VK_DOWN
    VK_LEFT
    VK_RIGHT
    VK_RETURN
    VK_SHIFT
    VK_F1
    ...
    VK_F12
    VK_PREVIOUS // this one isnt obvious, but its pg up
    VK_NEXT // like the last one, this one is hard to figure out, its pg down
    VK_CONTROL
    VK_MENU // another not-so-obvious one. this is the alt key

    you can probably find all of these somewhere if you do a search for virtual-key codes, or if you are using MSVC, they are in the help files.

    to get this function, just include <windows.h>
    I came up with a cool phrase to put down here, but i forgot it...

  4. #4
    Rabite SirCrono6's Avatar
    Join Date
    Nov 2003
    Location
    California, US
    Posts
    269
    Probably functions to do this for you (like above), but on the FAQ Board (bottom of General Programming Boards, I go there rarely though), I found the following code (written by BMJ but I have it formatted to my liking):

    Code:
    #include <iostream>
    #include <windows.h>
    
    bool Keypress( char &Key );
    
    bool Keypress( char &Key )
    {
      INPUT_RECORD Event;
      DWORD NumberOfEvents, EventsRead, EventCounter;
      GetNumberOfConsoleInputEvents( GetStdHandle( STD_INPUT_HANDLE ), &NumberOfEvents );
      if( NumberOfEvents == 0 )
      {
        return false;
      }
      for( EventCounter = 0; EventCounter < NumberOfEvents; EventCounter++ )
      {
        PeekConsoleInput( GetStdHandle( STD_INPUT_HANDLE ), &Event, 1, &EventsRead );
        if( ( Event.EventType == KEY_EVENT ) && ( ( Event.Event.KeyEvent.bKeyDown ) ) )
        {
          ReadConsoleInput( GetStdHandle( STD_INPUT_HANDLE ), &Event, 1, &EventsRead );
          Key = Event.Event.KeyEvent.wVirtualKeyCode;
          if( !( FlushConsoleInputBuffer( GetStdHandle( STD_INPUT_HANDLE ) ) ) )
          exit( 0 );
          return true;
        }
        else
        {		
          ReadConsoleInput( GetStdHandle( STD_INPUT_HANDLE ), &Event, 1, &EventsRead);
        }
      }
      return false;
    }
    
    int main( void )
    {
      char Key;
      for( ; ; )
      {
        if( Keypress( &Key )
        {
          if( Key == VK_UP )
          {
            std::cout << "You pressed up.";
          }
          else if( Key == VK_DOWN )
          {
            std::cout << "You pressed down.";
          }
          else if( Key == VK_RIGHT )
          {
            std::cout << "You pressed right.";
          }
          else if( Key == VK_LEFT )
          {
            std::cout << "You pressed left.";
          }
          else if( Key == VK_ESCAPE )
          {
            break;
          }
        }
      }
      return 0;
    }
    You could switch the if statements for switch cases if you want. I don't know if this is really old, but it works and it's there if you want it.

    - SirCrono6
    From C to shining C++!

    Great graphics, sounds, algorithms, AI, pathfinding, visual effects, cutscenes, etc., etc. do NOT make a good game.
    - Bubba

    IDE and Compiler - Code::Blocks with MinGW
    Operating System - Windows XP Professional x64 Edition

  5. #5
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    University of Waterloo
    Posts
    1,904
    hmm, that's a ton of code just for a key test, what i use in my projects is something like so:


    Code:
    if(kbhit())
    {
    	int keyHit=getch();
    	cout << keyHit;
    	if(keyHit==224)	// Sent when a special key is sent,
    	{		// such as an arrow key or f# keys
    		int subKey=getch();
    		cout << "\nspecial char: "<< subKey;
    	}
    	cout << endl;
    }
    -edit-
    oh, the kbhit function is something that i used because without it, the game locks up, and I actually used this in some of my very first games on the dos console (an asteroids game and a platform game, both drawn completely with letters and numbers )

    if you would like to see those original games, you can get them from my site:

    Jeff Verkoeyen's Portfolio
    alldosgames.zip

    or just go to:
    direct link
    Last edited by jverkoey; 03-03-2004 at 06:55 PM.

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >and i tried the FAQ's
    You didn't try very hard, did you?
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Arrow Key
    By peckitt99 in forum Windows Programming
    Replies: 2
    Last Post: 03-24-2007, 06:00 AM
  2. Menu Bar(using arrow key)
    By SweeLeen in forum C++ Programming
    Replies: 1
    Last Post: 09-29-2005, 12:23 AM
  3. Arrow key reading
    By sufthingol in forum C++ Programming
    Replies: 5
    Last Post: 03-19-2005, 04:08 PM
  4. key input definition
    By hkmixxa in forum C++ Programming
    Replies: 3
    Last Post: 08-09-2004, 06:39 PM
  5. single key input
    By SPiRiToFCaT in forum C++ Programming
    Replies: 14
    Last Post: 11-15-2002, 02:23 PM

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