Arrow keys?

I want to make a game where you move around (still within the command line) how do I take the immediate key press (I do know the ascii value of the keys) I don't even know where to start

btw I made the matrix(no not the movie either :P) *sigh
I tried AsyncKeyState but it outputs 0 for all keys Microsoft gave the following reasons for that:
Windows NT/2000/XP: The return value is zero for the following cases:

* The current desktop is not the active desktop
* The foreground thread belongs to another process and the desktop does not allow the hook or the journal record.


I'm about to try ReadConsoleInput now
I just tried it and it didn't compile

By the way I use Borland for a compiler
Last edited on
Thank you Duoas but I finally gave up and used getch:

using the w a s d keys

1
2
3
4
5
6
7
8
9
10
11
12
int c=getch();
    string key;    

    if (c == 119){
       key = "up";
    }else if (c == 100){
       key = "right";
    }else if (c == 115){
       key = "down";
    }else if (c == 97){
       key = "left";
    }
Last edited on
That is the worst way....
What does the compiler say when you try ReadConsoleInput ?
Yea, the way to go is with the Win32 Kernel console functions. I believe SetConsoleCursorPosition() will be needed too.
sorry I didn't respond till a while

I tried this on the msdn website (except bool was all caps)

1
2
3
4
5
6
    bool WINAPI ReadConsoleInput(
  __in   HANDLE hConsoleInput,
  __out  PINPUT_RECORD lpBuffer,
  __in   DWORD nLength,
  __out  LPDWORD lpNumberOfEventsRead
  );


it errors me with:

WINAPI does not name a type


which confused me :( but oh well
Are you including windows.h? I think you need that (although I think WINAPI is just defined __stdcall)
This is meh code:

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <windows.h>

int main (){
    
    bool WINAPI ReadConsoleInput(
         __in   HANDLE hConsoleInput,
         __out  PINPUT_RECORD lpBuffer,
         __in   DWORD nLength,
         __out  LPDWORD lpNumberOfEventsRead
    );
    
 return 0; 
}


I added windows.h this time and it returned this error code:
`__stdcall__' attribute only applies to function types
`__in' undeclared (first use this function)
(Each undeclared identifier is reported only once for each function it appears in.)
`__out' undeclared (first use this function)
initializer expression list treated as compound expression
Ah, that is just the description of the function and the parameters...it's not how you call it.

http://www.cplusplus.com/doc/tutorial/functions/
Hmm I didn't know it was a function :( why would you store it in a boolean anyway? Shouldn't it return ASCII values?

So it would be like this? :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <cstdlib>
#include <iostream>
#include <windows.h>

using namespace std;

int testy = ReadConsoleInput(
          __in   HANDLE hConsoleInput,
          __out  PINPUT_RECORD lpBuffer,
          __in   DWORD nLength,
          __out  LPDWORD lpNumberOfEventsRead
);
    
 
int main(int argc, char *argv[])
{
    
    
    return EXIT_SUCCESS;
}


Because it says now that __in and __out were not declared in this scope
Last edited on
Did you read Duoas' link?
Duoas Sep 11, 2009
An example of using ReadConsoleInput()
http://www.cplusplus.com/forum/articles/7312/page1.html#msg33734
Woops ..... sorry I feel stupid now .....

1
2
3
4
5
6
7
8
9
    int c = 0;
    int b;
    const char *i;
    while (c != 4){
       b = keyReader(i); // renamed your original function
       c++; // sorry not intentional
       cout << c << " : " << b << endl;
    }
    cmd("pause"); // hehe new idea that replaces system :) 


The only down side is that I can't update my game because it's on my laptop :(
But thank you for the help :)

By the way ... what is wrong with getch()?
Last edited on
9 cmd("pause"); // hehe new idea that replaces system :)

What!?

Did you read the article I linked?
yes am I missing something :( ?

it is a function that when called checks if it is string pause then runs your code ....
I made the function you made the code :)

1
2
3
4
5
6
int cmd (string typeCommandHere){
    if (typeCommandHere == "pause"){
    cout << "Press ENTER to continue...";
    cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
    }
}
Yes, this:

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
#include <windows.h>

int PressAnyKey( const char *prompt )
  {
  DWORD        mode;
  HANDLE       hstdin;
  INPUT_RECORD inrec;
  DWORD        count;
  char         default_prompt[] = "Press a key to continue...";

  /* Set the console mode to no-echo, raw input, */
  /* and no window or mouse events.              */
  hstdin = GetStdHandle( STD_INPUT_HANDLE );
  if (hstdin == INVALID_HANDLE_VALUE
  || !GetConsoleMode( hstdin, &mode )
  || !SetConsoleMode( hstdin, 0 ))
    return 0;

  if (!prompt) prompt = default_prompt;

  /* Instruct the user */
  WriteConsole(
    GetStdHandle( STD_OUTPUT_HANDLE ),
    prompt,
    lstrlen( prompt ),
    &count,
    NULL
    );

  FlushConsoleInputBuffer( hstdin );

  /* Get a single key RELEASE */
  do ReadConsoleInput( hstdin, &inrec, 1, &count );
  while ((inrec.EventType != KEY_EVENT) || inrec.Event.KeyEvent.bKeyDown);

  /* Restore the original console mode */
  SetConsoleMode( hstdin, mode );

  return inrec.Event.KeyEvent.wVirtualKeyCode;
  }

Oh, OK, I thought you were passing it off to system()...
Sorry about that.
Topic archived. No new replies allowed.