Link to home
Start Free TrialLog in
Avatar of disrupter
disrupter

asked on

listening to keyboard (not cin) c++

Hi, Im trying to create a gearbox simulator, its almost done.

How can I listen to the key that is being pressed in order to use this switch: (c++ console program)

void Keyboard(unsigned char key)
{
      switch (key)
      {
            case 'u' :
                  //upgrade
                  break;
            
            case 'd' :
                  //down grade
                  break;

            case 'a' :
                  //accelerate
                  break;

            case 'b' :
                  //break
                  break;      
    }

}

and also this one

void KeybUp(unsigned char key) //when the key is up
{

      switch (key)
      {
            case 'a' :
            is_acc = false;
            break;
            
            case 'b' :
            is_break =false;
            break;
    }
}

sry for my english
ASKER CERTIFIED SOLUTION
Avatar of mcanti
mcanti

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of disrupter
disrupter

ASKER

Im using visual c++ 6.. and its a console program, no form
hmm, I found this question in the C++Builder area...

Please ask a page editor to move your question to Visual C++
check out the API  getKeyState()
Here's a short example of what you may want to do.

#include <stdio.h>
#include <conio.h>
void main(){
      while(1){
            if(_kbhit()){
                  printf("%c", _getch() );
            }
            //do the processing you want
      }
}

_kbhit() just tests if a key has been pressed.  You have to use _getch() after it though, to clear the true state of a key being hit.  If you don't read the key with _getch() regardless of whether or not you use it, _kbhit() will remain true.

Tim