May 17, 2013 at 5:53pm May 17, 2013 at 5:53pm UTC
I am gonna make a simple game in c++. I need a way how to use a particular key on the keyboard ,when I hit the key certain function should be called.
For movement of cursor on screen I want to connect arrow keys.
Please post some links if available
Thanks in advance :)
May 17, 2013 at 6:04pm May 17, 2013 at 6:04pm UTC
You can use the windows api function I believe it is asynchkey or something like that not sure the exact name or you can do something like
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
#include <iostream>
#include <conio.h>
int main( int argc , char **argv )
{
bool play = true ;
char key = char ();
do
{
if ( kbhit() )
{
if ( key == 27 )
{
play = false ; //this or
return ( 0 ); //this
}
key = getch();
std::cout << '\r' << key << " has been pressed!" << std::flush;
}
} while ( play == true );
return ( 0 );
}
Last edited on May 17, 2013 at 6:11pm May 17, 2013 at 6:11pm UTC
May 17, 2013 at 6:27pm May 17, 2013 at 6:27pm UTC
sets key to an empty character. char() just constructs an empty char.
Last edited on May 17, 2013 at 6:34pm May 17, 2013 at 6:34pm UTC