?
Solved

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

Posted on 2007-12-02
10
Medium Priority
?
300 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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
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 2000 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

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

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

Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
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…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.
Suggested Courses

801 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