home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / win_lrn / i_o / getasyks.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-08-11  |  2.7 KB  |  106 lines

  1. /*
  2.  * GetAsyncKeyState
  3.  *
  4.  * This function determines whether a key is up or down, and
  5.  * whether the key was pressed after a previous call to the
  6.  * GetAsyncKeyState function.
  7.  *
  8.  */
  9.  
  10. #include <windows.h>
  11.  
  12. long FAR PASCAL AsyncWndProc(HWND, unsigned, WORD, LONG);
  13.  
  14. int PASCAL WinMain(hInstance, hPrevInstance, lpszCmdLine, nCmdShow)
  15. HANDLE hInstance, hPrevInstance;
  16. LPSTR  lpszCmdLine;
  17. int    nCmdShow;
  18. {
  19.   HWND     hWnd;
  20.   WNDCLASS wndclass;
  21.   MSG      msg;
  22.  
  23.   if (!hPrevInstance) 
  24.     {
  25.     wndclass.style         = CS_HREDRAW | CS_VREDRAW;
  26.     wndclass.lpfnWndProc   = AsyncWndProc;
  27.     wndclass.cbClsExtra    = 0;
  28.     wndclass.cbWndExtra    = 0;
  29.     wndclass.hInstance     = hInstance;
  30.     wndclass.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
  31.     wndclass.hCursor       = LoadCursor(NULL, IDC_ARROW);
  32.     wndclass.hbrBackground = GetStockObject(WHITE_BRUSH);
  33.     wndclass.lpszMenuName  = NULL;
  34.     wndclass.lpszClassName = "getasyks";
  35.  
  36.     if (!RegisterClass(&wndclass))
  37.          return FALSE;
  38.     }
  39.  
  40.   hWnd = CreateWindow ((LPSTR)"getasyks",
  41.                        (LPSTR)"GetAsyncKeyState()",
  42.                        WS_OVERLAPPEDWINDOW,
  43.                        CW_USEDEFAULT, 0,
  44.                        CW_USEDEFAULT, 0,
  45.                        NULL, NULL,
  46.                        hInstance, NULL);
  47.  
  48.   ShowWindow(hWnd, nCmdShow);
  49.   UpdateWindow(hWnd);
  50.  
  51.   while (GetMessage(&msg, NULL, 0, 0))
  52.     {
  53.      TranslateMessage(&msg);
  54.      DispatchMessage(&msg);
  55.     }
  56.  
  57.   return (msg.wParam);     
  58. }
  59.  
  60. long FAR PASCAL AsyncWndProc(hWnd, iMessage, wParam, lParam)
  61. HWND     hWnd;
  62. unsigned iMessage;
  63. WORD     wParam;
  64. LONG     lParam;
  65. {
  66.  switch(iMessage)
  67.    {
  68.    int Keydown;
  69.    PAINTSTRUCT ps;
  70.    HDC hDC;
  71.  
  72.    case WM_CHAR:
  73.      Keydown = GetAsyncKeyState(VK_F12);
  74.      if ((Keydown & 0x8000) == 0x8000)
  75.        MessageBox(hWnd, (LPSTR)"'F12' is down.",
  76.                   (LPSTR)"GetAsyncKeyState()", MB_OK);
  77.      else if ((Keydown & 0x0001) == 0x0001)
  78.        MessageBox(hWnd,
  79.                   (LPSTR)"'F12' pressed since last GetAsyncState() call.",
  80.                   (LPSTR)"GetAsyncKeyState()", MB_OK);
  81.      else
  82.        MessageBox(hWnd,
  83.                   (LPSTR)"'F12' not pressed since last GetAsyncState() call.",
  84.                   (LPSTR)"GetAsyncKeyState()", MB_OK);
  85.      break;
  86.  
  87.   case WM_PAINT:
  88.      hDC = BeginPaint(hWnd, (LPPAINTSTRUCT)&ps);
  89.      TextOut(hDC, (short)10, (short)10,
  90.              (LPSTR)"Hit space bar while pressing/not pressing 'F12' to test",
  91.              (short)55);
  92.      EndPaint(hWnd, (LPPAINTSTRUCT)&ps);
  93.      break;
  94.     
  95.   case WM_DESTROY:
  96.     PostQuitMessage(0);
  97.     break;
  98.  
  99.   default:
  100.     return DefWindowProc (hWnd, iMessage, wParam, lParam);
  101.     break;
  102.   }
  103.   return (0L); 
  104. }
  105.  
  106.