Solved

listening to keyboard (not cin) c++

Posted on 2004-09-22
6
1,150 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
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 synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

919 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now