using keystrokes on the fly

I'm trying to write a program that continues to process weather or not a key is pressed on the keyboard, but that key's pressed will change variables in the program while its running. Anyway, I've pulled out from another program what I thing will do the trick, (can't find anything about it in any tutorials), and on a more basic level,the compiler says "ISO C++ forbids comparison between pointer and integer" at lines 41 through 44. I thought that when I ran changedir it would return a char -result, into dir, which should be compared to the constants "n","s","e","w","q".where am I wrong in my logic? Also, I would appreciate any input on my logic for using keystrokes without stopping the program to wait for input. Thanks in advance!!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <iostream>
#include <windows.h>
using namespace std;

char changedir(char *prompt){
     DWORD           mode;
     HANDLE          hstdin;
     INPUT_RECORD    inrec;
     DWORD           count;
     char            result='\0';
     
     hstdin = GetStdHandle( STD_INPUT_HANDLE );
     if (hstdin == INVALID_HANDLE_VALUE
     || !GetConsoleMode( hstdin, &mode )
     || !SetConsoleMode( hstdin, 0))
      return result;
     
    
WriteConsole(
GetStdHandle( STD_OUTPUT_HANDLE ),
prompt,
lstrlen( prompt ),
&count,
NULL
);
FlushConsoleInputBuffer( hstdin );

ReadConsoleInput( hstdin, &inrec, 1, &count );
result = inrec.Event.KeyEvent.uChar.AsciiChar;
return result;
}

bool iskeypressed()
  {
  return WaitForSingleObject(
    GetStdHandle( STD_INPUT_HANDLE ),
    0
    ) == WAIT_OBJECT_0;
  }

int main(){
    int x=0,y=0,z=1;
    bool keypress = false;
    char dir='\0';
    while(z == 1 ){
                keypress = iskeypressed();
                if (keypress == true)
                   {
                   dir = changedir(" ");
                   if (dir == 'n') y++;
                   else if (dir == 's') y--;
                   else if (dir == 'e') x++;
                   else if (dir == 'w') x--;
                   else if (dir == 'q') z++;
                   keypress = false;
                   }
                cout << x <<"/" << y << '\n';
                }
    return 0;
}

    
Last edited on
Replace double quotes (") with single quotes (') in lines 40 (I'm surprised the compiler didn't complain) to 44. Double quote strings are pointers to static C arrays. Single quotes strings can contain just one 8-bit character and are, in fact integers. There's also L"" and L'', but that's Unicode. You may need to use the former to interact with some WinAPI functions.
Last edited on
Thanks for the input helios, the compiler was complaining about the quotes, I just didn't understand what or why it was complaining.... Now that I can run the program, can you help me with one more thing??? I had hoped that the program would continue cycling through the "while" block weather or not I pressed a key, but it only seems to run when I am pressing a key.??? Any ideas how to continue running without waiting for a keystroke???
From the MSDN Library:

"The function does not return until at least one input record has been read."

If you want to use this particular function, you'll need to wait for keys in a separate thread. I give a stripped down example of WinAPI threading here: http://www.cplusplus.com/forum/windows/3301/

You should also change this: every time changedir() is called, you're calling GetStdHandle(). The proper way to do it is calling it just once in main() and passing the handle to all functions that need it.
You don't necessarily have to create a separate thread. You can specify the input console in any one of the wait functions.

A simple poll:
1
2
3
4
5
6
7
8
9
#include <windows.h>

bool iskeypressed()
  {
  return WaitForSingleObject(
    GetStdHandle( STD_INPUT_HANDLE ),
    0
    ) == WAIT_OBJECT_0;
  }


You can also use any of the alertable wait functions and just forget it until you get a "there is input waiting" messages.

Hope this helps.

[edited to indicate that you must #include <windows.h> 2008-8-17]
Last edited on
Thanks guys, I tried helios' new thread method, and was having trouble getting it to work last night. I'll have to look at it again today, and try the "wait" technique. Is there a link I can go to to find out more about wait statements??
and some of the other ways they can be used??
http://msdn.microsoft.com/en-us/library/ms686353%28VS.85%29.aspx

Don't let it get over your head. It's a huge, scary category, but MS provides some nice examples and there is plenty of stuff elsewhere on the web too.
Thanks for all your help!! I've edited the original post to show the completed program, and it works like a charm!! Now to use it in a simulation.....

Edit:
hmmmm. I tried to put a delay(Sleep(500)) in the while loop to slow down output, and stopped cycling??? any ideas??
Last edited on
Topic archived. No new replies allowed.