Solved

listening to keyboard (not cin) c++

Posted on 2004-09-22
6
1,148 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
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: …
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 synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

707 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

15 Experts available now in Live!

Get 1:1 Help Now