Solved

listening to keyboard (not cin) c++

Posted on 2004-09-22
6
1,154 Views
Last Modified: 2013-11-17
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
0
Comment
Question by:disrupter
6 Comments
 
LVL 2

Accepted Solution

by:
mcanti earned 125 total points
ID: 12124362
Add to your Form the following event handlers: OnShortCut and OnKeyUp
Then do this:

void __fastcall TForm1::FormShortCut(TWMKey &Msg,
      bool &Handled)
{
    Keyboard((unsigned char)Msg.CharCode);
    Handled = true;
}

void __fastcall TForm1::FormKeyUp(TObject *Sender, WORD &Key,
        TShiftState Shift)
{
  KeybUp((unsigned char)Key);
}
0
 

Author Comment

by:disrupter
ID: 12124411
Im using visual c++ 6.. and its a console program, no form
0
 
LVL 2

Expert Comment

by:mcanti
ID: 12124459
hmm, I found this question in the C++Builder area...

Please ask a page editor to move your question to Visual C++
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 11

Expert Comment

by:Jase-Coder
ID: 12125014
check out the API  getKeyState()
0
 
LVL 11

Expert Comment

by:Jase-Coder
ID: 12125032
0
 
LVL 1

Expert Comment

by:joe-boo
ID: 14563117
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
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deletiā€¦
Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question