home *** CD-ROM | disk | FTP | other *** search
/ The Mother of All Windows Books / CD-MOM.iso / cd_mom / newsletr / vbz / vbz1-3 / dll_src.exe / HOTKEY.PAS < prev    next >
Pascal/Delphi Source File  |  1993-02-24  |  3KB  |  139 lines

  1. {HOTKEY.DLL - HotKey Support DLL for Visual Basic}
  2. {by Jonathan Zuck}
  3. {Copyright 1992-3 Jonathan Zuck and User Friendly, Inc.}
  4.  
  5. library HotKey;
  6.  
  7. {$C Fixed Permanent}
  8.  
  9. {$R HOTKEY}
  10.  
  11. uses WinTypes, WinProcs;
  12.  
  13. type
  14.     HotRec = Record
  15.         VKCODE:integer;
  16.       MASK:integer;
  17.       hWnd:hWnd;
  18.       MyKey:integer;
  19.   end;
  20.  
  21. var
  22.     HotArray: array [1..1024] of HotRec;
  23.   TKeys:integer;
  24.     NextProc:TFarProc;
  25.   SaveExit:Pointer;
  26.  
  27. function KBProc (nCode, wParam:integer;lParam:Longint):longint;export;
  28.     Var
  29.        TempMask, El:integer;
  30.     Match:Bool;
  31.     Begin
  32.        if (NOT lParam < 0) and (wParam <> VK_SHIFT)
  33.         and (wParam <> VK_CONTROL) and (wParam <> VK_MENU) and (nCode >= 0) then
  34.           Begin
  35.           TempMask := 0;
  36.             if GetKeyState (VK_SHIFT) < 0 then TempMask := TempMask + 1;
  37.             if GetKeyState (VK_CONTROL) < 0 then TempMask := TempMask + 2;
  38.             if GetKeyState (VK_MENU) < 0 then TempMask := TempMask + 4;
  39.         El := 0;
  40.         Match := FALSE;
  41.         While (Match = FALSE) and (El <> TKeys) do
  42.             Begin
  43.               Inc (El);
  44.             if (HotArray[El].VKCODE = wParam)
  45.                 And (HotArray[El].MASK = TempMask) then Match := TRUE;
  46.           End;
  47.           if Match then
  48.               Begin
  49.                 PostMessage (HotArray[El].HWND, WM_KEYDOWN, HotArray[El].MyKey, lParam);
  50.                   KBProc := 1;
  51.             End
  52.           Else
  53.                       KBProc := DefHookProc (nCode, wParam, lParam, @NextProc);
  54.               End
  55.              Else
  56.                  Begin
  57.                       KBProc := DefHookProc (nCode, wParam, lParam, @NextProc);
  58.                 End;
  59.   end;
  60.  
  61. function CreateHK (KeyCode, Shift, hWindow, UKeyCode:integer):longint;export;
  62.     Var
  63.        El:integer;
  64.     Match:Bool;
  65.     ID:^LongInt;
  66.     Begin
  67.       if TKeys = 0 then
  68.         NextProc := SetWindowsHook (WH_KEYBOARD, @KBProc);
  69.         if TKeys < 1024 then
  70.             Begin
  71.         El := 0;
  72.         Match := FALSE;
  73.         While (Match = FALSE) and (El <> TKeys) do
  74.             Begin
  75.               Inc(El);
  76.             if (HotArray[El].VKCODE = KeyCode)
  77.                 and (HotArray[El].MASK = Shift) then Match := TRUE;
  78.             end;
  79.             if Not Match then
  80.                 Begin
  81.                           Inc (TKeys);
  82.                          HotArray[TKeys].VKCODE := KeyCode;
  83.                          HotArray[TKeys].MASK := Shift;
  84.                              HotArray[TKeys].HWND := hWindow;
  85.                     HotArray[TKeys].MyKey := UKeyCode;
  86.                 ID := @HotArray[TKeys].HWND;
  87.                          CreateHK := ID^;
  88.               end
  89.             Else CreateHK := -1;
  90.               end
  91.        else
  92.            Begin
  93.           CreateHK := -2;
  94.         end;
  95.     end;
  96.  
  97. procedure KillHK (hHK:longint);export;
  98.     var
  99.        El:integer;
  100.       ID:^longint;
  101.     Match:Bool;
  102.  
  103.     Begin
  104.        El := 0;
  105.     Match := FALSE;
  106.     While (Match = FALSE) and (El <> TKeys) do
  107.          Begin
  108.                Inc(El);
  109.         ID := @HotArray[El].HWND;
  110.         if ID^ = hHK then MATCH := TRUE;
  111.       end;
  112.       if Match then
  113.           Begin
  114.               Move (HotArray[El + 1].VKCODE, HotArray[El].VKCODE,
  115.               SizeOf (HotRec) * (TKeys - El));
  116.           Dec (TKeys);
  117.           if TKeys = 0 then
  118.               UnHookWindowsHook (WH_KEYBOARD, @KBProc);
  119.         end;
  120.   end;
  121.  
  122. procedure LibExit; far;
  123.     begin
  124.     if TKeys > 0 then
  125.            UnHookWindowsHook (WH_KEYBOARD, @KBProc);
  126.     ExitProc := SaveExit;
  127.   end;
  128.  
  129. Exports
  130.     KBProc,
  131.   CreateHK resident,
  132.   KillHK resident;
  133.  
  134. begin
  135.     TKeys := 0;
  136.   SaveExit := ExitProc;
  137.   ExitProc := @LibExit;
  138. end.
  139.