home *** CD-ROM | disk | FTP | other *** search
/ Programming Windows 95 / Programming Windows 95.iso / code / CHAP05 / KEYLOOK.C next >
Encoding:
C/C++ Source or Header  |  1996-01-01  |  5.5 KB  |  164 lines

  1. /*-------------------------------------------------------
  2.    KEYLOOK.C -- Displays Keyboard and Character Messages
  3.                 (c) Charles Petzold, 1996
  4.   -------------------------------------------------------*/
  5.  
  6. #include <windows.h>
  7. #include <stdio.h>
  8.  
  9. LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
  10.  
  11. RECT rect ;
  12. int  cxChar, cyChar ;
  13.  
  14. int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
  15.                     PSTR szCmdLine, int iCmdShow)
  16.      {
  17.      static char szAppName[] = "KeyLook" ;
  18.      HWND        hwnd ;
  19.      MSG         msg ;
  20.      WNDCLASSEX  wndclass ;
  21.  
  22.      wndclass.cbSize        = sizeof (wndclass) ;
  23.      wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
  24.      wndclass.lpfnWndProc   = WndProc ;
  25.      wndclass.cbClsExtra    = 0 ;
  26.      wndclass.cbWndExtra    = 0 ;
  27.      wndclass.hInstance     = hInstance ;
  28.      wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
  29.      wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  30.      wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
  31.      wndclass.lpszMenuName  = NULL ;
  32.      wndclass.lpszClassName = szAppName ;
  33.      wndclass.hIconSm       = LoadIcon (NULL, IDI_APPLICATION) ;
  34.  
  35.      RegisterClassEx (&wndclass) ;
  36.  
  37.      hwnd = CreateWindow (szAppName, "Keyboard Message Looker",
  38.                           WS_OVERLAPPEDWINDOW,
  39.                           CW_USEDEFAULT, CW_USEDEFAULT,
  40.                           CW_USEDEFAULT, CW_USEDEFAULT,
  41.                           NULL, NULL, hInstance, NULL) ;
  42.  
  43.      ShowWindow (hwnd, iCmdShow) ;
  44.      UpdateWindow (hwnd) ;
  45.  
  46.      while (GetMessage (&msg, NULL, 0, 0))
  47.           {
  48.           TranslateMessage (&msg) ;
  49.           DispatchMessage (&msg) ;
  50.           }
  51.      return msg.wParam ;
  52.      }
  53.  
  54. void ShowKey (HWND hwnd, int iType, char *szMessage,
  55.               WPARAM wParam, LPARAM lParam)
  56.      {
  57.      static char *szFormat[2] = { "%-14s %3d    %c %6u %4d %3s %3s %4s %4s",
  58.                                   "%-14s    %3d %c %6u %4d %3s %3s %4s %4s" } ;
  59.      char        szBuffer[80] ;
  60.      HDC         hdc ;
  61.  
  62.      ScrollWindow (hwnd, 0, -cyChar, &rect, &rect) ;
  63.      hdc = GetDC (hwnd) ;
  64.  
  65.      SelectObject (hdc, GetStockObject (SYSTEM_FIXED_FONT)) ;
  66.  
  67.      TextOut (hdc, cxChar, rect.bottom - cyChar, szBuffer,
  68.               wsprintf (szBuffer, szFormat [iType],
  69.                         szMessage, wParam,
  70.                         (BYTE) (iType ? wParam : ' '),
  71.                         LOWORD (lParam),
  72.                         HIWORD (lParam) & 0xFF,
  73.                         (PSTR) (0x01000000 & lParam ? "Yes"  : "No"),
  74.                         (PSTR) (0x20000000 & lParam ? "Yes"  : "No"),
  75.                         (PSTR) (0x40000000 & lParam ? "Down" : "Up"),
  76.                         (PSTR) (0x80000000 & lParam ? "Up"   : "Down"))) ;
  77.  
  78.      ReleaseDC (hwnd, hdc) ;
  79.      ValidateRect (hwnd, NULL) ;
  80.      }
  81.  
  82. LRESULT CALLBACK WndProc (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
  83.      {
  84.      static char szTop[] =
  85.                "Message        Key Char Repeat Scan Ext ALT Prev Tran";
  86.      static char szUnd[] =
  87.                "_______        ___ ____ ______ ____ ___ ___ ____ ____";
  88.      HDC         hdc ;
  89.      PAINTSTRUCT ps ;
  90.      TEXTMETRIC  tm ;
  91.  
  92.      switch (iMsg)
  93.           {
  94.           case WM_CREATE :
  95.                hdc = GetDC (hwnd) ;
  96.  
  97.                SelectObject (hdc, GetStockObject (SYSTEM_FIXED_FONT)) ;
  98.  
  99.                GetTextMetrics (hdc, &tm) ;
  100.                cxChar = tm.tmAveCharWidth ;
  101.                cyChar = tm.tmHeight ;
  102.  
  103.                ReleaseDC (hwnd, hdc) ;
  104.  
  105.                rect.top = 3 * cyChar / 2 ;
  106.                return 0 ;
  107.  
  108.           case WM_SIZE :
  109.                rect.right  = LOWORD (lParam) ;
  110.                rect.bottom = HIWORD (lParam) ;
  111.                UpdateWindow (hwnd) ;
  112.                return 0 ;
  113.  
  114.           case WM_PAINT :
  115.                InvalidateRect (hwnd, NULL, TRUE) ;
  116.                hdc = BeginPaint (hwnd, &ps) ;
  117.  
  118.                SelectObject (hdc, GetStockObject (SYSTEM_FIXED_FONT)) ;
  119.  
  120.                SetBkMode (hdc, TRANSPARENT) ;
  121.                TextOut (hdc, cxChar, cyChar / 2, szTop, (sizeof szTop) - 1) ;
  122.                TextOut (hdc, cxChar, cyChar / 2, szUnd, (sizeof szUnd) - 1) ;
  123.                EndPaint (hwnd, &ps) ;
  124.                return 0 ;
  125.  
  126.           case WM_KEYDOWN :
  127.                ShowKey (hwnd, 0, "WM_KEYDOWN", wParam, lParam) ;
  128.                return 0 ;
  129.  
  130.           case WM_KEYUP :
  131.                ShowKey (hwnd, 0, "WM_KEYUP", wParam, lParam) ;
  132.                return 0 ;
  133.  
  134.           case WM_CHAR :
  135.                ShowKey (hwnd, 1, "WM_CHAR", wParam, lParam) ;
  136.                return 0 ;
  137.  
  138.           case WM_DEADCHAR :
  139.                ShowKey (hwnd, 1, "WM_DEADCHAR", wParam, lParam) ;
  140.                return 0 ;
  141.  
  142.           case WM_SYSKEYDOWN :
  143.                ShowKey (hwnd, 0, "WM_SYSKEYDOWN", wParam, lParam) ;
  144.                break ;        // ie, call DefWindowProc
  145.  
  146.           case WM_SYSKEYUP :
  147.                ShowKey (hwnd, 0, "WM_SYSKEYUP", wParam, lParam) ;
  148.                break ;        // ie, call DefWindowProc
  149.  
  150.           case WM_SYSCHAR :
  151.                ShowKey (hwnd, 1, "WM_SYSCHAR", wParam, lParam) ;
  152.                break ;        // ie, call DefWindowProc
  153.  
  154.           case WM_SYSDEADCHAR :
  155.                ShowKey (hwnd, 1, "WM_SYSDEADCHAR", wParam, lParam) ;
  156.                break ;        // ie, call DefWindowProc
  157.  
  158.           case WM_DESTROY :
  159.                PostQuitMessage (0) ;
  160.                return 0 ;
  161.           }
  162.      return DefWindowProc (hwnd, iMsg, wParam, lParam) ;
  163.      }
  164.