Solved

listening to keyboard (not cin) c++

Posted on 2004-09-22
6
1,162 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 11

Expert Comment

by:Jase-Coder
ID: 12125014
check out the API  getKeyState()
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

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

In our object-oriented world the class is a minimal unit, a brick for constructing our applications. It is an abstraction and we know well how to use it. In well-designed software we are not usually interested in knowing how objects look in memory. …
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.

724 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