線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:1628
推到 Plurk!
推到 Facebook!

如何寫出一個全局的快速鍵??

尚未結案
skya0
一般會員


發表:5
回覆:4
積分:1
註冊:2003-01-05

發送簡訊給我
#1 引用回覆 回覆 發表時間:2003-12-22 17:17:35 IP:218.165.xxx.xxx 未訂閱
我想在windows中的任何地方 按下F1 就自動送出"1234"和滑鼠左鍵一下? 看了一些文章 知道要使用dll 和hook 但是一直沒頭緒 各位大大能否給個 例子 ??謝謝
hagar
版主


發表:143
回覆:4056
積分:4445
註冊:2002-04-14

發送簡訊給我
#2 引用回覆 回覆 發表時間:2003-12-22 17:27:05 IP:202.39.xxx.xxx 未訂閱
How can I create a system wide keyboard hook under Win32: http://www.delphifaq.com/fq/q2108.shtml
Library TheHook;    uses
  Windows, Messages, SysUtils;    {Define a record for recording and passing information process wide}
type
  PHookRec = ^ THookRec;
  THookRec = Packed Record
    TheHookHandle: HHOOK;
    TheAppWinHandle: HWnd;
    TheCtrlWinHandle: HWnd;
    TheKeyCount: DWord;
  end;    var
  hObjHandle : THandle; {Variable for the file mapping object}
  lpHookRec  : PHookRec;
{Pointer to our hook record}
procedure MapFileMemory (dwAllocSize: DWord);
begin { MapFileMemory }
  {Create a process wide memory mapped variable}
  hObjHandle := CreateFileMapping ($FFFFFFFF, Nil, PAGE_READWRITE, 0,
    dwAllocSize, 'HookRecMemBlock');
  if (hObjHandle = 0) then
    begin
      MessageBox (0, 'Hook DLL', 'Could not create file map object', mb_Ok);
      exit
    end { (hObjHandle = 0) };
  {Get a pointer to our process wide memory mapped variable}
  lpHookRec := MapViewOfFile (hObjHandle, FILE_MAP_WRITE, 0, 0, dwAllocSize);
  if (lpHookRec = Nil) then
    begin
      CloseHandle (hObjHandle);
      MessageBox (0, 'Hook DLL', 'Could not map file', mb_Ok);
      exit
    end { (lpHookRec = Nil) }
end; { MapFileMemory }    procedure UnMapFileMemory;
begin { UnMapFileMemory }
  {Delete our process wide memory mapped variable}
  if (lpHookRec <> Nil) then
    begin
      UnMapViewOfFile (lpHookRec);
      lpHookRec := Nil
    end { (lpHookRec <> Nil) };
  if (hObjHandle > 0) then
    begin
      CloseHandle (hObjHandle);
      hObjHandle := 0
    end { (hObjHandle > 0) }
end; { UnMapFileMemory }    function GetHookRecPointer : pointer
  stdcall;
begin { GetHookRecPointer }
  {Return a pointer to our process wide memory mapped variable}
  Result := lpHookRec
end; { GetHookRecPointer }    {The function that actually processes the keystrokes for our hook}
function KeyBoardProc (code: Integer; wParam: Integer; lParam: Integer) :
  Integer;
  stdcall;
var
  KeyUp : bool;
{Remove comments for additional functionability
  IsAltPressed : bool;
  IsCtrlPressed : bool;
  IsShiftPressed : bool;
 } 
begin { KeyBoardProc } 
  Result := 0; 
  
  Case code Of 
  HC_ACTION: 
    begin 
      {We trap the keystrokes here} 
      {Is this a key up message?} 
      KeyUp := ((lParam and (1 shl 31)) <> 0); 
      
      (*Remove comments for additional functionability
     {Is the Alt key pressed}
      if ((lParam and (1 shl 29)) <> 0) then begin
        IsAltPressed := TRUE;
      end else begin
        IsAltPressed := FALSE;
      end;         {Is the Control key pressed}
      if ((GetKeyState(VK_CONTROL) and (1 shl 15)) <> 0) then begin
        IsCtrlPressed := TRUE;
      end else begin
        IsCtrlPressed := FALSE;
      end;         {if the Shift key pressed}
      if ((GetKeyState(VK_SHIFT) and (1 shl 15)) <> 0) then begin
        IsShiftPressed := TRUE;
      end else begin
        IsShiftPressed := FALSE;
      end;
     *) 
      {if KeyUp then increment the key count} 
      if (KeyUp <> false) then 
        begin 
          inc (lpHookRec^.TheKeyCount)
        end { (KeyUp <> false) }; 
      
      Case wParam Of 
      {Was the enter key pressed?} 
      VK_RETURN: 
        begin 
          {if KeyUp} 
          if (KeyUp <> false) then 
            begin 
              {Post a bogus message to the window control in our app} 
              PostMessage (lpHookRec^.TheCtrlWinHandle, WM_KEYDOWN, 0, 0); 
              PostMessage (lpHookRec^.TheCtrlWinHandle, WM_KEYUP, 0, 0)
            end { (KeyUp <> false) }; 
          {if you wanted to swallow the keystroke then return -1} 
          {else if you want to allow the keystroke then return 0} 
          Result := 0; 
          exit
        end; {VK_RETURN} 
      {if the left arrow key is pressed then lets play a joke!} 
      VK_LEFT: 
        begin 
          {if KeyUp} 
          if (KeyUp <> false) then 
            begin 
              {Create a UpArrow keyboard event} 
              keybd_event (VK_RIGHT, 0, 0, 0); 
              keybd_event (VK_RIGHT, 0, KEYEVENTF_KEYUP, 0)
            end { (KeyUp <> false) }; 
          {Swallow the keystroke} 
          Result := -1; 
          exit
        end; {VK_LEFT} 
      end { case wParam }; {case wParam} 
      {Allow the keystroke} 
      Result := 0
    end; {HC_ACTION} 
  HC_NOREMOVE: 
    begin 
      {This is a keystroke message, but the keystroke message} 
      {has not been removed from the message queue, since an} 
      {application has called PeekMessage() specifying PM_NOREMOVE} 
      Result := 0; 
      exit
    end; 
  end { case code }; {case code} 
  if (code < 0) then 
    {Call the next hook in the hook chain} 
    Result := CallNextHookEx (lpHookRec^.TheHookHandle, code, wParam, lParam)
end; { KeyBoardProc }     procedure StartKeyBoardHook 
  stdcall; 
begin { StartKeyBoardHook } 
  {if we have a process wide memory variable} 
  {and the hook has not already been set...} 
  if ((lpHookRec <> Nil) and (lpHookRec^.TheHookHandle = 0)) then 
    begin 
      {Set the hook and remember our hook handle} 
      lpHookRec^.TheHookHandle := SetWindowsHookEx (WH_KEYBOARD, @KeyBoardProc, 
        HInstance, 0)
    end { ((lpHookRec <> Nil) and (lpHookRec^.TheHookHandle = 0)) }
end; { StartKeyBoardHook }     procedure StopKeyBoardHook 
  stdcall; 
begin { StopKeyBoardHook } 
  {if we have a process wide memory variable} 
  {and the hook has already been set...} 
  if ((lpHookRec <> Nil) and (lpHookRec^.TheHookHandle <> 0)) then 
    begin 
      {Remove our hook and clear our hook handle} 
      if (UnHookWindowsHookEx (lpHookRec^.TheHookHandle) <> false) then 
        begin 
          lpHookRec^.TheHookHandle := 0
        end { (UnHookWindowsHookEx (lpHookRec^.TheHookHandle) <> false) }
    end { ((lpHookRec <> Nil) and (lpHookRec^.TheHookHandle <> 0)) }
end; { StopKeyBoardHook }     procedure DllEntryPoint (dwReason: DWord); 
begin { DllEntryPoint } 
  Case dwReason Of 
  Dll_Process_Attach: 
    begin 
      {if we are getting mapped into a process, then get} 
      {a pointer to our process wide memory mapped variable} 
      hObjHandle := 0; 
      lpHookRec := Nil; 
      MapFileMemory (sizeof (lpHookRec^))
    end; 
  Dll_Process_Detach: 
    begin 
      {if we are getting unmapped from a process then, remove} 
      {the pointer to our process wide memory mapped variable} 
      UnMapFileMemory
    end; 
  end { case dwReason }
end; { DllEntryPoint }     Exports 
  KeyBoardProc name 'KEYBOARDPROC', 
  GetHookRecPointer name 'GETHOOKRECPOINTER', 
  StartKeyBoardHook name 'STARTKEYBOARDHOOK', 
  StopKeyBoardHook name 'STOPKEYBOARDHOOK';     begin 
  {Set our Dll's main entry point} 
  DLLProc := @DllEntryPoint; 
  {Call our Dll's main entry point} 
  DllEntryPoint (Dll_Process_Attach)
end.    
--- Everything I say is a lie. @>---
skya0
一般會員


發表:5
回覆:4
積分:1
註冊:2003-01-05

發送簡訊給我
#3 引用回覆 回覆 發表時間:2003-12-23 01:45:56 IP:218.165.xxx.xxx 未訂閱
小弟不材 可否舉個例子 這個dll該如何使用?
hagar
版主


發表:143
回覆:4056
積分:4445
註冊:2002-04-14

發送簡訊給我
#4 引用回覆 回覆 發表時間:2003-12-23 08:43:30 IP:202.39.xxx.xxx 未訂閱
那參考這篇吧, 有詳細的步驟: http://oldlook.experts-exchange.com/Programming/Programming_Languages/Delphi/Q_20694870.html --- Everything I say is a lie. @>---
系統時間:2024-04-27 19:45:03
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!