因为程序需要监视按下了MOUSE什么键?
谢谢

解决方案 »

  1.   

    需要HOOK,需要DLL
    library hookprj;{ Important note about DLL memory management: ShareMem must be the
      first unit in your library's USES clause AND your project's (select
      Project-View Source) USES clause if your DLL exports any procedures or
      functions that pass strings as parameters or function results. This
      applies to all strings passed to and from your DLL--even those that
      are nested in records and classes. ShareMem is the interface unit to
      the BORLNDMM.DLL shared memory manager, which must be deployed along
      with your DLL. To avoid using BORLNDMM.DLL, pass string information
      using PChar or ShortString parameters. }uses
      SysUtils,
      Classes,
      hkprocunit in 'hkprocunit.pas';{$R *.RES}
    exports
      EnableMouseHook, //只要把这两个函数输出就可以了
      DisableMouseHook;
    beginend.unit hkprocunit;interfaceuses
      Windows,Messages;var
      hHk: HHOOK;//钩子的句柄值。
      function MouseHookProc(nCode: Integer;WParam: WPARAM;LParam: LPARAM): LRESULT;stdcall;
      //鼠标钩子的回调函数,即是用它来处理得到消息后要干什么。这里我只是发送一个//WM_PASTE消息。
      //nCode参数是Hook的标志,一般只关心小于0时。看下面的详细说明
      //WParam参数表示鼠标消息的类型
      //LParam参数是一个指向 TMOUSEHOOKSTRUCT 结构的指针。结构包含了鼠标消息的状态,我只用了hwnd一个
      //即鼠标消息要传递给的窗口句柄。
      function EnableMouseHook:Boolean; stdcall; export;
      function DisableMouseHook:Boolean; stdcall; export;//两个函数都是Boolean类型,成功都是返回Trueimplementationfunction MouseHookProc(nCode: Integer;WParam: WPARAM;LParam: LPARAM): LRESULT;stdcall;
    var
      MouseHookStruct: ^TMOUSEHOOKSTRUCT;//这个结构Delphi在Windows单元有定义,直接用就可以了。
      nState: SHORT;//得到键盘状态的GetKeyState函数的返回值。这是一个16位的数。
    begin
      Result := 0; //最好首先给他一个返回值,不然会有警告的!记住这可不是C语言。
      //当nCode小于0时表示还有其它的Hook,必须把参数传给他。
      //此时就要用Api函数CallNextHookEx让他调用下一个Hook!!!  
      if nCode < 0 then 
        Result := CallNextHookEx(hHk,nCode,WParam,LParam)//参数是现成的,直接用就可以了,
                                                        //详细的说明可以参考Win32 SDK
      else if wParam = WM_LBUTTONDBLCLK then //判断是不是鼠标左键双击事件
      begin
        nState := GetKeyState(VK_CONTROL);//这个函数只有一个参数,就是要得到的键的
                                          //键值,这里用windows的虚拟键值表示ctrl键。
        if (nState and $80000000) = $80000000 then//如果按下了,那么返回值的最高位为1
        begin                                     //即是16进制的80000000,如果没有按下就返回0
          MouseHookStruct := Pointer(LParam);//转换指针并付值给MouseHookStruct变量。
          SendMessage(MouseHookStruct.hwnd,WM_PASTE,0,0);//如果条件都满足了就发送消息。
        end;
      end;end;function EnableMouseHook:Boolean; stdcall; export;
    begin
      if hHk = 0 then //为了安全,必须判断一下再设置钩子。
      Begin  
      // 第三个参数的Hinstance 在Delphi中有定义,用就可以了。第四个参数必须为0
        hHk := SetWindowsHookEx(WH_MOUSE,@MouseHookProc,Hinstance,0);
        Result := True;
      end
      else
        Result := False;
    end;function DisableMouseHook:Boolean; stdcall; export;
    begin
      if hHk <> 0 then //如果有钩子就卸掉他。
        begin
          UnHookWindowsHookEx(hHk);
          hHk := 0;
          Result := True;
        end
      else
        Result := False;
    end;end.
      

  2.   

    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
      StdCtrls;type
      TForm1 = class(TForm)
        Button1: TButton;
        Button2: TButton;
        Edit1: TEdit;
        Label1: TLabel;
        procedure Button1Click(Sender: TObject);
        procedure Button2Click(Sender: TObject);
        procedure FormClose(Sender: TObject; var Action: TCloseAction);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;
      //下面是引用hookprj.dll中的函数。
        function EnableMouseHook:Boolean; stdcall; external 'Hookprj.dll' name 'EnableMouseHook';
        function DisableMouseHook:Boolean; stdcall; external 'Hookprj.dll' name 'DisableMouseHook';
    implementation{$R *.DFM}procedure TForm1.Button1Click(Sender: TObject);
    begin
     if EnableMouseHook then
       ShowMessage('启动钩子成功');
    end;procedure TForm1.Button2Click(Sender: TObject);
    begin
      if DisableMouseHook then
        ShowMessage('停止钩子成功');
    end;procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
    begin
      DisableMouseHook;
      //这里调用是必须的,否则有可能没有卸载钩子就退出了,那就不好了。
    end;end.
      

  3.   

    先谢谢上面的兄弟
    可我从哪取得,是按了MOUSE什么键呢?
      

  4.   

    if wParam = WM_LBUTTONDBLCLK 左双击
               = WM_LBUTTONDOWN 左按下
                 WM_RBUTTONDOWN 右按下