?
Solved

listening to keyboard (not cin) c++

Posted on 2004-09-22
6
Medium Priority
?
1,165 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 250 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

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.

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. …
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org Go to that link and select download selenium in the right hand column That will then direct you to their download page. From that p…
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.
Suggested Courses
Course of the Month15 days, 2 hours left to enroll

771 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