-2

we have a project on school, coding a volleyball - match as console application. We should make input-output for teams and score etc, and bind keys to Team1-score+1, Team2-score-1 etc. That's fine. We use getch() to bind these keys. My problem: If you for example press R and R is Team1-score+1 and hold this key, it will continue adding the score+1, and we should stop that so you need to press R again if you want to add another point. I didn't find this solution anywhere! Can anyone give an example how to fix it? I am new on coding so I am inexperienced. Thanks for help guys! EDIT: I am using Windows 10, important part of my code:

while (true)
{
    _kbhit();
    char L = _getch();
    if (L == 114)
    {
        score1 = score1 + 1;
    }
    else if (L == 99)
    {
        score1 = score1 - 1;
    }
    else if (L == 117)
    {
        score2 = score2 + 1;
    }
    else if (L == 110)
    {
        score2 = score2 - 1;
    }
    else if (L == 113)
    {

L=="number" is the ascii-code for buttons r,u,c,n,q

  • 1
    The smart way to ask a question like this would be to include the code that you currently have for handling keyboard input, and ask how it should be modified to support the desired behavior. See also [MCVE]. – Cody Gray - on strike Nov 27 '16 at 11:14

2 Answers2

2

getch() operates on stdio which effectively is just a bytestream coming/going from/into the OS. It doesn't have the concept of "key presses/releases". When you keep a key pressed the OS will start to autorepeat the pressed character. To make things worse for your use case, if you keep a key pressed, and then press another key, the key repeats of the first will cease.

Now there are ways to get raw key down/up events, but these involve OS specific programming interfaces; you didn't tell us which OS you're using, so it's a bit of guesswork there. Doing what you want to do in the console is somewhat possible, but it's awkward to use. For Windows see C++ console keyboard events, for Linux see Receiving key press and key release events in Linux terminal applications?

However I strongly suggest you don't even try to implement all that OS level key event interfacing yourself, and instead make use of a cross platform library that offers a framework for doing exactly what you want to do there. One of the most widely used frameworks these days is SDL http://libsdl.org/ – it will move you out of the console though (when it comes to games: good riddance, except if you're working on a rouge-like game).

Community
  • 1
  • 1
datenwolf
  • 159,371
  • 13
  • 185
  • 298
0

If you are using the console then most operating systems will wait a second after you press a key before registering that key press again. You can try it now: press a key on your keyboard and see what the response is.

Making sure someone doesn't accidentally press a key twice is a problem that the OS takes care of, because otherwise every application taking keyboard input would need to code their own solution to the problem.

If you want to go further than what the OS does, you can use a timer in your program with the chrono library, and wait n seconds before allowing input to be registered again.

Nonanon
  • 560
  • 3
  • 10
  • How can I use this timer to allow getting user input after n seconds again? My code looks like this: while (true) { _kbhit(); char L = _getch(); if (L == 114) { score1 = score1 + 1; } and so on.. – DONT SPAM CRINGE Nov 27 '16 at 11:28
  • Firstly, use `L = 'r'`, as it's easier to understand than `L = 114`, and maybe choose a better name than `L`. Secondly, to wait 4 seconds, you can do: `std::this_thread::sleep_for( 4s );`. Also you can just write `score1++` or `score1 += 1` rather than `score1 = score1 + 1` – Nonanon Nov 27 '16 at 22:34
  • thanks for your suggestions, but std::this_thread::sleep_for( 4s ); stops the whole _getch() and you have to wait 4 seconds to give another instruction, doesnt matter if you want to add the score+1 or -1 etc, and it doesnt debounce the key pressed – DONT SPAM CRINGE Nov 29 '16 at 15:56
  • Thats why you use it in your while loop after you've processed the getch. – Nonanon Nov 30 '16 at 01:43
  • I dont understand what you mean, can you give an example? – DONT SPAM CRINGE Dec 04 '16 at 12:08