Archived

This topic is now archived and is closed to further replies.

misterags

GetAsyncKeyState() and the Win32 console

Recommended Posts

Thanks for the sarcasm, but could you explain a little better, because that doesn''t seem to work right, unless I''m not using it in the right context. This doesn''t work:

#include <windows.h>

main()
{
if(GetAsyncKeyState(VK_SPACE))
{
cout <<"nicely done.";
}

or this.

main()
{
while(1)
{
if(GetAsyncKeyState(VK_SPACE))
{
cout <<"nicely done.";
}
}
}

what am I doing wrong?

Misterags™

Share this post


Link to post
Share on other sites
hmm i could have sworn i used it in console b4...anyways, here is a c++ method to get a key. ....i got this offa a MSDN example, which you should look up if ur goinu do something in console. its called: "console", or at least thats what it is in my msdn examples folder. ( i may have changed it?) if you want i can send u a zip of it.

  
#include <windows.h>
#include <windowsx.h>
#include <iostream.h>


int main(void)
{
BOOL bSuccess;
HANDLE hStdIn, hStdOut; /* standard input, output handles */
DWORD dwMode;
/* array of console input event records */
INPUT_RECORD inputBuffer;
DWORD dwInputEvents; /* number of events actually read */
CONSOLE_SCREEN_BUFFER_INFO csbi; /* used to get cursor position */

bSuccess = GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE),
&csbi);
hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
hStdIn = GetStdHandle(STD_INPUT_HANDLE);
bSuccess = GetConsoleMode(hStdIn, &dwMode);
bSuccess = SetConsoleMode(hStdIn, (dwMode & ~(ENABLE_LINE_INPUT |
ENABLE_ECHO_INPUT)) | ENABLE_WINDOW_INPUT | ENABLE_MOUSE_INPUT);
do
{

bSuccess = ReadConsoleInput(hStdIn, &inputBuffer, 1, &dwInputEvents);
switch (inputBuffer.EventType)
{
case KEY_EVENT:
if (inputBuffer.Event.KeyEvent.bKeyDown)
{
cout << inputBuffer.Event.KeyEvent.wVirtualKeyCode << endl
<< inputBuffer.Event.KeyEvent.uChar.AsciiChar << endl;
}
break;


} /* switch */
/* when we receive an esc down key, drop out of do loop */

} while (!(inputBuffer.EventType == KEY_EVENT &&
inputBuffer.Event.KeyEvent.wVirtualKeyCode == VK_ESCAPE &&
inputBuffer.Event.KeyEvent.bKeyDown));
return(0);
}

Share this post


Link to post
Share on other sites
hmm the source scriptor sure did some funny color stuff on that... just cut and paste, and see if u can figure it out.
wow it sure looks nasty huh.

Share this post


Link to post
Share on other sites
It’s been a while since I did this but try this:

#include

void main()
{
int oldtime = GetTickCount();
while(!GetAsyncKeyState(65)) // if the letter A is pressed the programe ends
{
if (GetTickCount() – oldtime > 1000) //checks every 1s
{
if(GetAsyncKeyState(“The key you want to check”))
{
cout <<” the key was pressed”;
}

}

Share this post


Link to post
Share on other sites
you could always resort to old c stuff.

  
#include <conio.h>
#include <stdio.h>

void main()
{
char TestForKey();
char k = 0;
enum KEYS {KEY_ESCAPE = 27};

while((k = TestForKey()) != KEY_ESCAPE)
{
if(k != 0)
printf("%c",k);
}
}

char TestForKey()
{ //if key hit, return >0

char k = 0;
if(kbhit())
if (!(k = getch())) k = getch(); //test for extended if = 0

return k;
}

Share this post


Link to post
Share on other sites
<offtopic>Hmf - three void main and one main without a return specifier.</offtopic<

Fantastic doctrines (like Christianity or Islam or Marxism or Microsoft-bashing) require unanimity of belief. One dissenter casts doubt on the creed of millions. Thus the fear and hate; thus the torture chamber, the iron stake, the gallows, the labor camp, the psychiatric ward - Edward Abbey

Share this post


Link to post
Share on other sites
<offtopic>Hmf - three void main and one main without a return specifier.</offtopic>

Fantastic doctrines (like Christianity or Islam or Marxism or Microsoft-bashing) require unanimity of belief. One dissenter casts doubt on the creed of millions. Thus the fear and hate; thus the torture chamber, the iron stake, the gallows, the labor camp, the psychiatric ward - Edward Abbey

Share this post


Link to post
Share on other sites
quote:
Hmf - three void main and one main without a return specifier


Strange... the only correct one was based on some MSDN code. Last time I checked it''s all void main() there.

Share this post


Link to post
Share on other sites