Connecting an event with a key on keyboard

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 :)
I am not sure what you mean by:

"For movement of cursor on screen I want to connect arrow keys."

If you're going to make your game in the console then it's best you probably use something like the curses lib. It works on both Windows and Linux.

http://sourceforge.net/projects/pdcurses/?source=navbar

If you're making you're game using the WinAPI please say so.
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
What this does ?
1
2
3

char key = char();
sets key to an empty character. char() just constructs an empty char.
Last edited on
Topic archived. No new replies allowed.