home *** CD-ROM | disk | FTP | other *** search
/ Softwarová Záchrana 3 / Softwarova-zachrana-3.bin / ArsClip / source.zip / UnitKeyboardQuery.pas < prev    next >
Pascal/Delphi Source File  |  2003-07-30  |  2KB  |  102 lines

  1. unit UnitKeyboardQuery;
  2.  
  3. {
  4.     Purpose:
  5.         Query if a key is pressed or a lock key is on/off
  6.  
  7.     Updates:
  8.         - new functionality
  9.         - Mouse clickes added
  10.  
  11.     NOTES:
  12.         Mouse clicks may not seem like a Keyboard function, but
  13.         they are retreived from GetAsyncKeyState
  14. }
  15.  
  16.  
  17. {///////////}
  18. {}interface{}
  19. {///////////}
  20. uses Windows, Dialogs, SysUtils;
  21.  
  22.  
  23. type MouseButton = (leftButton, RightButton);
  24. type TKeyboardQuery = class(TObject)
  25.     private
  26.     public
  27.         function IsPressed(VKConstant : integer) : boolean;
  28.         function LockOn(VKConstant : integer) : boolean;
  29.         procedure WaitUntilRelease(VKConstant : integer); overload;
  30.         procedure WaitUntilRelease(bi : MouseButton); overload;
  31.         function IsClicked( bi : MouseButton ) : boolean;
  32. end;
  33.  
  34.  
  35. var KeyboardQuery : TKeyboardQuery;
  36.  
  37. {////////////////}
  38. {}implementation{}
  39. {////////////////}
  40.  
  41. uses Forms;
  42.  
  43.  
  44.  
  45. function TKeyboardQuery.IsClicked( bi : MouseButton ) : boolean;
  46. var VK : integer;
  47. begin
  48.     Windows.SetLastError(ERROR_SUCCESS);
  49.  
  50.     //
  51.     // The Physical and Logical "Left Button" may not be the same
  52.     //
  53.     if (bi = LeftButton) and
  54.         (GetSystemMetrics(SM_SWAPBUTTON) = 0) then begin
  55.         vk := VK_LBUTTON;
  56.     end else begin
  57.         vk := VK_RBUTTON;
  58.     end;
  59.     result := (Windows.GetAsyncKeyState(vk) and $8000) > 0;
  60. end;
  61.  
  62.  
  63. function TKeyboardQuery.IsPressed(VKConstant : integer) : boolean;
  64. begin
  65.     Windows.SetLastError(ERROR_SUCCESS);
  66.  
  67.     result := (Windows.GetKeyState(VKConstant) and 128) <> 0;
  68. end;
  69.  
  70.  
  71. function TKeyboardQuery.LockOn(VKConstant : integer) : boolean;
  72. begin
  73.     Windows.SetLastError(ERROR_SUCCESS);
  74.  
  75.     result := (Windows.GetKeyState(VKConstant) and 1) <> 0;
  76. end;
  77.  
  78.  
  79. procedure TKeyboardQuery.WaitUntilRelease(VKConstant: integer);
  80. begin
  81.     While self.IsPressed(VKConstant) do begin
  82.         Application.ProcessMessages;
  83.     end;
  84. end;
  85.  
  86. procedure TKeyboardQuery.WaitUntilRelease(bi: MouseButton);
  87. begin
  88.     while IsClicked(bi) do begin
  89.         Application.ProcessMessages;
  90.     end;
  91. end;
  92.  
  93.  
  94. {////////////////}
  95. {}initialization{}
  96. {////////////////}
  97. Begin
  98.     KeyboardQuery := TKeyboardQuery.Create;
  99. end;
  100.  
  101. end.
  102.