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.
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.
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; }
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...
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):
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.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; }
- 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
hmm, that's a ton of code just for a key test, what i use in my projects is something like so:
-edit-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; }
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.
>and i tried the FAQ's
You didn't try very hard, did you?
My best code is written with the delete key.