Solved

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

Posted on 2007-12-02
10
289 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
  • 3
  • 3
  • 2
  • +2
10 Comments
 
LVL 55

Expert Comment

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

Expert Comment

by:cup
Comment Utility
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
Comment Utility
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
 
LVL 5

Expert Comment

by:abith
Comment Utility
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
Comment Utility
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
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:cup
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article will show, step by step, how to integrate R code into a R Sweave document
The purpose of this article is to demonstrate how we can use conditional statements using Python.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

762 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

7 Experts available now in Live!

Get 1:1 Help Now