Connecting an event with a key on keyboard

May 17, 2013 at 5:53pm
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:02pm
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.
May 17, 2013 at 6:04pm
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:25pm
What this does ?
1
2
3

char key = char();
May 17, 2013 at 6:27pm
sets key to an empty character. char() just constructs an empty char.
Last edited on May 17, 2013 at 6:34pm
Topic archived. No new replies allowed.