Re: Key-press detection



David Sweeney said:

I am using C++ (with the Visual C++ compiler) on Windows XP, and I am
hoping to develop a simple computer game. I would like to know if
there is a way to detect which key the user is pressing, eg, if the
user is pressing 'a', can that be transferred into a character? Can
you write:

char c = GetKeyPress();

or something similar?

If you're programming for the Win32 API (i.e. if your entry point is
WinMain and you have window procedures and stuff), then you just
intercept and handle WM_CHAR messages. If all that is Greek to you,
then you're probably writing a console program, in which case you'll
want to #include <conio.h> and use kbhit() to detect whether a key has
been pressed (fortunately, without blocking if it hasn't), and getch()
to return the key value itself. If getch() returns 0, that means a
"special" key has been pressed (e.g. a function key), and calling it
again will give you a code that lets you find out /which/ special key.

You would do well to subscribe to comp.os.ms-windows.programmer.win32 in
case you have any further Windows programming questions - they're a
great bunch of folks there.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
.



Relevant Pages

  • How to read arrow key in C
    ... How to read arrow key input in C programming? ... The only way i know is by using getch() method. ... Regards, ... Paramesh. ...
    (comp.os.msdos.programmer)
  • Re: How to read arrow key in C
    ... >>How to read arrow key input in C programming? ... >>The only way i know is by using getch() method. ... > If getchreturns 0, the key has no ASCII character code, however, ...
    (comp.os.msdos.programmer)
  • Re: How to read arrow key in C
    ... >How to read arrow key input in C programming? ... If getch() returns 0, the key has no ASCII character code, however, ...
    (comp.os.msdos.programmer)