Re: Key-press detection



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

.



Relevant Pages

  • Re: Key-press detection
    ... to develop a simple computer game. ... detect which key the user is pressing, eg, if the user is pressing 'a', ... if (!init) { ... pipe =!GetConsoleMode; ...
    (comp.programming)
  • Re: Key-press detection
    ... to develop a simple computer game. ... detect which key the user is pressing, eg, if the user is pressing 'a', ... pipe =!GetConsoleMode; ... be displayed inside the window. ...
    (comp.programming)
  • Re: Key-press detection
    ... to develop a simple computer game. ... detect which key the user is pressing, eg, if the user is pressing ... pipe =!GetConsoleMode; ... should be displayed inside the window. ...
    (comp.programming)
  • Re: Key-press detection
    ... to develop a simple computer game. ... detect which key the user is pressing, eg, if the user is pressing 'a', ... pipe =!GetConsoleMode; ... should be displayed inside the window. ...
    (comp.programming)
  • filling the bowl
    ... Thanks Rick. ... They one problem I am having is like putting some in the pipe ... and pressing on it and some more and pressing on it...... ...
    (alt.smokers.pipes)