网上有很多代码是用来实现键盘和鼠标事件的记录、回放的!但是找不到可以把记录下的鼠标和键盘操作事件保存下来的代码就是想实现按键精灵的功能,可以录制一段鼠标键盘操作,并把录制的操作保留下来谁有代码可以提供!谢谢了

解决方案 »

  1.   

    这个实现起来比较麻烦
    你得用Hook了
      

  2.   

    就可以用按键精灵来实现呀,如果一定要用Delphi可以,只是比较复杂.
      

  3.   

    嘿嘿,我刚好有实例,记得给分哦~unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;type
      TForm1 = class(TForm)
        Button1: TButton;
        Button2: TButton;
        Button3: TButton;
        procedure FormCreate(Sender: TObject);
        procedure Button1Click(Sender: TObject);
        procedure Button2Click(Sender: TObject);
        procedure Button3Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;
      EventArr:array[0..1000]of EVENTMSG;
      EventLog:Integer;
      PlayLog:Integer;
      hHook,hPlay:Integer;
      recOK:Integer;
      canPlay:Integer;
      bDelay:Bool;
    implementation{$R *.dfm}
    Function PlayProc(iCode:Integer;wParam:wParam;lParam:lParam):LRESULT;stdcall;
    begin
      canPlay:=1;
      Result:=0;
      if iCode < 0 then //必须将消息传递到消息链的下一个接受单元
      Result := CallNextHookEx(hPlay,iCode,wParam,lParam)
      else if iCode = HC_SYSMODALON then
      canPlay:=0
      else if iCode = HC_SYSMODALOFF then
      canPlay:=1
      else if ((canPlay =1 )and(iCode=HC_GETNEXT)) then begin
      if bDelay then begin
      bDelay:=False;
      Result:=50;
      end;
      pEventMSG(lParam)^:=EventArr[PlayLog];
      end
      else if ((canPlay = 1)and(iCode = HC_SKIP))then begin
      bDelay := True;
      PlayLog:=PlayLog+1;
      end;
      if PlayLog>=EventLog then begin
      UNHookWindowsHookEx(hPlay);
      end;
    end;function HookProc(iCode:Integer;wParam:wParam;lParam:lParam):LRESULT;stdcall;
    begin
      recOK:=1;
      Result:=0;
      if iCode < 0 then
      Result := CallNextHookEx(hHook,iCode,wParam,lParam)
      else if iCode = HC_SYSMODALON then
      recOK:=0
      else if iCode = HC_SYSMODALOFF then
      recOK:=1
      else if ((recOK>0) and (iCode = HC_ACTION)) then begin
      EventArr[EventLog]:=pEventMSG(lParam)^;
      EventLog:=EventLog+1;
      if EventLog>=1000 then begin
      UnHookWindowsHookEx(hHook);
      end;
      end;
    end;procedure TForm1.FormCreate(Sender: TObject);
    begin
     Button1.Caption:='纪录';
      Button2.Caption:='停止';
      Button3.Caption:='回放';
      Button2.Enabled:=False;
      Button3.Enabled:=False;end;procedure TForm1.Button1Click(Sender: TObject);
    begin
    EventLog:=0;
      //建立键盘鼠标操作消息纪录链
      hHook:=SetwindowsHookEx(WH_JOURNALRECORD,HookProc,HInstance,0);
      Button2.Enabled:=True;
      Button1.Enabled:=False;end;procedure TForm1.Button2Click(Sender: TObject);
    begin
    UnHookWindowsHookEx(hHook);
      hHook:=0;
      Button1.Enabled:=True;
      Button2.Enabled:=False;
      Button3.Enabled:=True;end;procedure TForm1.Button3Click(Sender: TObject);
    begin
    PlayLog:=0;
      //建立键盘鼠标操作消息纪录回放链
      hPlay:=SetwindowsHookEx(WH_JOURNALPLAYBACK,PlayProc,
      HInstance,0);
      Button3.Enabled:=False;end;end.
    整个打包工程下载:
    http://download.csdn.net/source/734043
      

  4.   


    非常感谢!你这段代码我已经有了。不是舍不得那几分。可能是我没有表达清楚吧,上面的代码已经实现了可以记录和回放了,但是EXE退出的时候,记录的鼠标键盘事件也随之丢失了。我是想把记录下的鼠标键盘事件保存下来。谁可以教教我该怎么做?
      

  5.   

    N年前我学习的时候抄书上的,现在加个保存和打开的。去我的资源里下载吧,我每次上传了都看不到,好像要过好久才能看到!不知道CSDN在搞什么!http://download.csdn.net/etomahawk
      

  6.   

    用全局HOOK消息。把消息写到文件里去吗