Re: Key-press detection
- From: Richard Heathfield <rjh@xxxxxxxxxxxxxxx>
- Date: Tue, 28 Aug 2007 17:17:23 +0000
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
.
- References:
- Key-press detection
- From: David Sweeney
- Key-press detection
- Prev by Date: Re: Key-press detection
- Next by Date: Re: Key-press detection
- Previous by thread: Re: Key-press detection
- Next by thread: Re: Key-press detection
- Index(es):
Relevant Pages
|