Solved

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

Posted on 2007-12-02
10
295 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
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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
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: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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

Title # Comments Views Activity
Header of docx file 17 118
Adjust the position 3 65
Excel file not created as expected 7 79
Reccomended programming language for client-server applications 12 131
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
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 …
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

830 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