Re: Key-press detection
- From: user923005 <dcorbit@xxxxxxxxx>
- Date: Tue, 28 Aug 2007 18:26:24 -0700
On Aug 28, 9:35 am, "David Sweeney" <David.Sween...@xxxxxxxxxxxxxx>
wrote:
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?
This is from the chess game OliThink:
#ifndef WIN32
int bioskey(void)
{
fd_set readfds;
FD_ZERO(&readfds);
FD_SET(fileno(stdin), &readfds);
tv.tv_sec = 0;
tv.tv_usec = 0;
select(16, &readfds, 0, 0, &tv);
return (FD_ISSET(fileno(stdin), &readfds));
}
#else
#include <windows.h>
#include <conio.h>
int bioskey(void)
{
int i;
static int init = 0,
pipe;
static HANDLE inh;
DWORD dw;
#if defined(FILE_CNT)
if (stdin->_cnt > 0)
return stdin->_cnt;
#endif
if (!init) {
init = 1;
inh = GetStdHandle(STD_INPUT_HANDLE);
pipe = !GetConsoleMode(inh, &dw);
if (!pipe) {
SetConsoleMode(inh, dw & ~(ENABLE_MOUSE_INPUT |
ENABLE_WINDOW_INPUT));
FlushConsoleInputBuffer(inh);
}
}
if (pipe) {
if (!PeekNamedPipe(inh, NULL, 0, NULL, &dw, NULL)) {
return 1;
}
return dw;
} else {
GetNumberOfConsoleInputEvents(inh, &dw);
return dw <= 1 ? 0 : dw;
}
}
#endif
.
- Follow-Ups:
- Re: Key-press detection
- From: David Sweeney
- Re: Key-press detection
- References:
- Key-press detection
- From: David Sweeney
- Key-press detection
- Prev by Date: Re: Problem Debugging C Program
- Next by Date: Re: efficient comparison
- Previous by thread: Re: Key-press detection
- Next by thread: Re: Key-press detection
- Index(es):
Relevant Pages
|