GetAsyncKeyState() and the Win32 console

Started by
8 comments, last by misterags 22 years, 3 months ago
Is it possibe to use GetAsyncKeyState() w/ the win32 console? If so, how? Misterags™
Advertisement
yep...
how do u normally use it? hmm lets see...

#include <windows.h>

void main()
{
if(GetAsyncKeyState(mekey))...
}
...
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™

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);}  
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.
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”;
}

}
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; }  
<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
--AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [IRC channel] [Blog]
<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
--AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [IRC channel] [Blog]
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.

This topic is closed to new replies.

Advertisement