Re: Key-press detection
- From: "Jim Langston" <tazmaster@xxxxxxxxxxxxxx>
- Date: Thu, 30 Aug 2007 18:26:26 -0700
"David Sweeney" <David.Sweeney8@xxxxxxxxxxxxxx> wrote in message
news:jpOdnTVFQOADhErbnZ2dneKdnZydnZ2d@xxxxxxxxx
"user923005" <dcorbit@xxxxxxxxx> wrote in message
news:1188350784.789790.29510@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
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
Thanks to everyone for their help. However, I am having trouble getting
any of these techniques to work. I am writing a program that diaplays a
window, and then when a key is pressed, the corresponding character should
be displayed inside the window. But I'll keep trying until it works...
What type of window? win32 window? The problem may be in your output, not
sure. Perhaps you should put a breakpoint on where you assign the key
pressed and run the program and examine the contents afterward.
.
- Follow-Ups:
- Re: Key-press detection
- From: David Sweeney
- Re: Key-press detection
- References:
- Key-press detection
- From: David Sweeney
- Re: Key-press detection
- From: user923005
- Re: Key-press detection
- From: David Sweeney
- Key-press detection
- Prev by Date: Re: Any progress yet? (was Re: Fast pi program?)
- 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
|