Solved

how to write a loop to detect keyed input without stopping program

Posted on 2007-12-02
10
297 Views
Last Modified: 2011-10-03
What C++ code could I use to detect certain keypresses in a while loop. I want to write a calibration routine (simple : )) that will adjust offset other variables by a certain amount when I press right arrow, left arrow, up arrow and down arrow. I don't want the system to stop or pause for input; the other routines need to continue reunning. I am thinking of launching a thread to do this.

So how do I detect arrow keypresses....

0
Comment
Question by:Wanderinglazyeye
[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
  • 3
  • 3
  • 2
  • +2
10 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20392325
Which OS?
Which Envirnoment? console, window
Which Compiler?
Which compiler library?
0
 
LVL 11

Expert Comment

by:cup
ID: 20392340
Which OS are you using?  Different OSs generate different sequences for arrow keys.

If you're using windows, you could use _kbhit to detect whether the key had been pressed.  Saves having to launch a task.

On Unix, it is some ioctl sequence for the equivalent of windows _kbhit.

Don't know about other OSs.
0
 
LVL 86

Expert Comment

by:jkr
ID: 20392348
Use 'GetKeyState()' (http://msdn2.microsoft.com/en-us/library/ms646301.aspx), e.g.

int nState = GetKeyState('a');

if (0x80000000 & nState) {

  // 'a' is pressed
}

or 'GetKeyboardState() (http://msdn2.microsoft.com/en-us/library/ms646299.aspx)

BYTE keys[256];

GetKeyboardState(keys);

to get the whole keyboard state.
0
Industry Leaders: 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 5

Expert Comment

by:abith
ID: 20393850
here is a small exp code which runs till pressing ESC key
for windows:
while(1)
{
   if (_kbhit())
   {
          c = _getch();
          if(c==27) // ESC
                 break;
    }
}

for unix
while(1)
{
    c = kb_getc()  ;
   if (c) // key is pressed
   {
         if(c==27)
           break;
   }
}
0
 
LVL 11

Expert Comment

by:cup
ID: 20397321
Source for kb_getc - this isn't a std unix call but the code is simple enough

unsigned char kb_getc(void)
{
    unsigned char ch;
    size_t size;

   size = read (STDIN_FILENO, &ch, 1);
   return size == 0? 0: ch;
}
0
 
LVL 11

Expert Comment

by:cup
ID: 20397357
Just a thought - it needs raw mode.  Might be better if you look at http://www.pwilson.net/kbhit.html for kb_getc if you're using unix.  It is pretty complex.
0
 

Author Comment

by:Wanderinglazyeye
ID: 20406600
No errors, but it doesn't detect the "a":


int i = 1;
	////////////////////////////////////////////////////////////////
	// Start Life Cycle
	////////////////////////////////////////////////////////////////
	while (i){
int nState = GetKeyState('a');
if (0x80000000 & nState) {
MessageBox(NULL,"A","Error",MB_OK);
  // 'a' is pressed
}
}

Open in new window

0
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 20406649
Try a capital 'A', the following works fine:

#include <windows.h>

void main () {

int i = 1;
        ////////////////////////////////////////////////////////////////
        // Start Life Cycle
        ////////////////////////////////////////////////////////////////
        while (i){
int nState = GetKeyState('A');
if (0x80000000 & nState) {
MessageBox(NULL,"A","Error",MB_OK);
  // 'a' is pressed
}
}
}
0
 

Author Comment

by:Wanderinglazyeye
ID: 20406918
What codes would I use for up arrow,right arrow, down arrow, left arrow, etc. More intelligently phrased, where can I find the map to codes I could select for keys from the keyboard?

Code works fine now.

WLE
0
 
LVL 86

Expert Comment

by:jkr
ID: 20407424
These are defined as VK_UP, VK_DOWN, VK_LEFT and VK_RIGHT. You will find all of them in winuser.h or in the table at http://www.codeproject.com/KB/system/keyboard.aspx
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

729 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