1

I want to use getch() or something similar to register a keystroke in a while() function.

    while()
    {
        .
        .
        .
        if(kbhit()) k=getch();
        else cout<<"no input";
        cout<<k<<endl;
        k=0;
        Sleep(1200);
        .
        .
        .
    }

If i hold a key, the function will keep displaying that key for a while. I will use a similar code to implement movement for a worm game. If a key will not be pressed the worm will keep going int he direction it's facing (but i don't need help with this i already have it sorted out).

I just need to know how do i register just 1 key press for a while cycle. Using Codeblocks.

Zadak Leader
  • 29
  • 11
  • Try this: http://stackoverflow.com/questions/13018150/detecting-keydown-and-keyup-events-on-linux-c. If you follow some of the links, you will be pointed to the ncurses library. – Trenin Nov 26 '13 at 21:31
  • So basically i need either a way to flush the buffer after i get a key(or not) so it doesn't register next time or use a different function. – Zadak Leader Nov 26 '13 at 21:58
  • Sleeping for 1.2 seconds makes this problem a *lot* worse. Just don't, the key repeats you get just mean "worm up" again when it is already moving up so make no difference. Use the clock to update game state, Sleep(1) to avoid burning 100% core. – Hans Passant Nov 26 '13 at 22:43

2 Answers2

1

Simple. Like this!!!.

#include <iostream>
#include <windows.h> 

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <conio.h>
using namespace std;


#define KB_UP 72
#define KB_DOWN 80
#define KB_LEFT 75
#define KB_RIGHT 77
#define KB_ESCAPE 27
#define KB_F8 66



bool QuitGame=false;

char KB_code;

void simple_keyboard_input();  


int main(void)
{


  while(!QuitGame)
  {
      /* simple keyboard input */
      simple_keyboard_input();



  }



   return 0;
}



void simple_keyboard_input()
{
    if (kbhit())
      {
            KB_code = getch();


            switch (KB_code)
            {

                case KB_ESCAPE:

                    QuitGame=true;

                break;



            }        

      }

}
Software_Designer
  • 8,490
  • 3
  • 24
  • 28
0

Personally I would use something like this:

int support_code = 0;      //global variable

void input()
{
   if (GetKeyState('W') & 0x8000)
   {  int helpful_button = "1";    // unique number for every button!
      if (helpful_button != support_code)
      {  support_code = helpful_button; 
         // do something
      }
   }
   else if (...)
   ...
   // End of "key registering" "if's"
}

// And then somewhere else in your code you can "reset" 'support_code' for it to be 0
// (so you can use the same button again)
// But if you don't want a worm to be able to change direction from "UP" to "UP"
// Then you don't even have to implement "support_code = 0" part from it

int main()
{
   ...
   while (!gameover)
   {
      input();
      calculate();
      draw();
      ...
      support_code = 0;
   }
...
}

For a "worm game" you won't probably need lot of keystrokes (I guess <10) so you can easily implement it this way. And as you can see, it will only "register" each key once.

(Worked for all of the games I coded so far)

Gaben
  • 345
  • 1
  • 12