Solved

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

Posted on 2007-12-02
10
296 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
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 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

Suggested Solutions

Windows Script Host (WSH) has been part of Windows since Windows NT4. Windows Script Host provides architecture for building dynamic scripts that consist of a core object model, scripting hosts, and scripting engines. The key components of Window…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

740 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