KB release

Hey I am currently making a main menu for a console app. It tests whether the user has hit the keys: 1,2 or 3 and then performs the relevant task.

Only problem is that the user can hold down the keys for the task to repeat endlessly. How can i test for on release of the key so you can't see an endless amount of spamming?

My code so far is:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
int main(int argc, char *argv[])
{
    bool game = false; //whether to reset info and screen or not
    bool show_numbers = true; //whether to show numbers or - instead
    
    //computer_game();
    
    while(1)
    {
        system("cls");
        cout<<"Welcome to Tic-Tac-Toe, created by Charlie Edmunds.\nPlease select from the following options:\n\n1) Play against a friend\n2) Play against computer\n3) Currently showing ";
        if (show_numbers) {cout<<"numbers instead of -";}
        else {cout<<"- instead of numbers";}
        cout<<". Press 3) to change";
        cout<<"\n\n";
        game = false;
        
        //start games options
        while (game == false)
        {
            if(kbhit())
            {
               int m = getch()-48;
               if (m == 1)
               {
                   player_game();
                   //cout<<endl<<"Test";
                   break;
                   game = true;
               }
               else if (m == 2)
               {
                   computer_game();
                   //cout<<"\nGame mode "<<m<<" not yet active please wait whilst its being built"; 
               }
               else if (m == 3)
               {
                    show_numbers = !show_numbers;
                    //redraw
                    system("cls");
                    cout<<"Welcome to Tic-Tac-Toe, created by Charlie Edmunds.\nPlease select from the following options:\n\n1) Play against a friend\n2) Play against computer\n3) Currently showing ";
                    if (show_numbers) {cout<<"'numbers' instead of '-'";}
                    else {cout<<"'-' instead of 'numbers'";}
                    cout<<". Press 3) to change";
                    cout<<"\n\n";     
               }
            }
        }
    }
    system("PAUSE");
    return EXIT_SUCCESS;
}


Thank you for any help you can provide :)
Respond to the event only when the user either presses or depresses a key, not each time the keyboard sends a message.
But how exactly would i do that XD
One thing you can do is have a bool variable check for keypress. That way, if a key is pressed, the bool will stay true until you change it.

EDIT: This does not actually work as desired. its stuck in a continuous loop :| i though kbhit() only returned true if a key was pressed down while it was called? There are way to many needed scripts not included in standard c libraries :|


new code is:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
    bool game = false; //whether to reset info and screen or not
    bool show_numbers = true; //whether to show numbers or - instead
    bool can_change = true; //whether to be able to change 
    
    //computer_game();
    
    while(1)
    {
        system("cls");
        cout<<"Welcome to Tic-Tac-Toe, created by Charlie Edmunds.\nPlease select from the following options:\n\n1) Play against a friend\n2) Play against computer\n3) Currently showing ";
        if (show_numbers) {cout<<"'numbers' instead of '-'";}
        else {cout<<"'-' instead of 'numbers'";}
        cout<<". Press 3) to change";
        cout<<"\n\n";
        game = false;
        
        
        //start games options
        while (game == false)
        {
            if(kbhit())
            {
               int m = getch()-48;
               if (m == 1)
               {
                   player_game(show_numbers);
                   //cout<<endl<<"Test";
                   //break;
                   //game = true;
               }
               else if (m == 2)
               {
                   computer_game(show_numbers);
                   //cout<<"\nGame mode "<<m<<" not yet active please wait whilst its being built"; 
               }
               else if (m == 3)
               {
                    do
                    { 
                       if (can_change == true)
                       {
                            show_numbers = !show_numbers;
                            //redraw
                            system("cls");
                            cout<<"Welcome to Tic-Tac-Toe, created by Charlie Edmunds.\nPlease select from the following options:\n\n1) Play against a friend\n2) Play against computer\n3) Currently showing ";
                            if (show_numbers) {cout<<"'numbers' instead of '-'";}
                            else {cout<<"'-' instead of 'numbers'";}
                            cout<<". Press 3 to change";
                            cout<<"\n\n"; 
                            can_change = false;
                        }    
                    }    
                    while(kbhit());
               }
            }
            can_change = true;
        }
    }


if i press the button '3' it will change but if held down after a short time it spams out and is rendered useless :| how can i solve this?
Last edited on
BUMP
Topic archived. No new replies allowed.