Re: Key-press detection
- From: "Jim Langston" <tazmaster@xxxxxxxxxxxxxx>
- Date: Fri, 31 Aug 2007 15:55:59 -0700
"David Sweeney" <David.Sweeney8@xxxxxxxxxxxxxx> wrote in message
news:IO2dndFi3tJa4ErbnZ2dneKdnZydnZ2d@xxxxxxxxx
"Jim Langston" <tazmaster@xxxxxxxxxxxxxx> wrote in message
news:n7KBi.43$d57.11@xxxxxxxxxxxxxxx
"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.
Guess what? I have managed to make it work! If anyone's interested, here
is the code for the method:
void CKeyPressView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
// Deal with key press
char k = (char) nChar;
CClientDC dc(this);
CString s;
s.Format("You pressed %c ", k);
dc.TextOut(10, 420, s);
CView::OnKeyDown(nChar, nRepCnt, nFlags);
}
I think the trick was: to create a project with a single-document window,
and not have a timer. The actual method is very simple, as you can see.
Ahh, you're using MFC. That was yet another way I didn't specify cause I
really don't use MFC. But, yeah, that'll work too.
.
- Next by Date: Re: Key-press detection
- Next by thread: Re: Key-press detection
- Index(es):
Relevant Pages
|