Re: Key-press detection



"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.


.



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 ... 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)
  • Re: Key-press detection
    ... to develop a simple computer game. ... detect which key the user is pressing, eg, if the user is pressing 'a', can ... if (!init) { ... pipe =!GetConsoleMode; ...
    (comp.programming)
  • Re: fileevent
    ... Turns out to have been a typo on my part,. ... that the pipe is ready to read when there is no data to read (the file ... I have a script that reads a tailed file via a pipe, ... it and puts up the info in a HLIST window. ...
    (comp.lang.perl.tk)