做了一个键盘HOOK,拦截键'W',使之更改为‘上'键,但在使用过程中发现,’上'键是出来了,但其后紧跟着一个'W'键,说明虚拟按键是好使了,但系统对'W'键的处理还没取消。请问如何实现。‘W' 键对应’上'键的操作。谢谢。

解决方案 »

  1.   

    改result:=1下边的不知道行不行:
      Result:=0;
      if (lParam shr 31)=0 then
      begin
        GetKeyNameText(lParam,arr,SizeOf(arr));
        if Uppercase(arr[0])='W' then
        begin
          h:=GetFocus;
          SendMessage(h,WM_KEyDown,ord('a'),0);
          SendMessage(h,wm_char,ord('a'),0);
          SendMessage(h,WM_KEYUp,ord('a'),0);
          Result:=1;
        end;
      end;
      

  2.   

    Mars (战神)同志:
           我也对你的那个问题很感兴趣!能否把你的代码给我看一下!万分感谢!
    我的E-MAIL :[email protected]
      

  3.   

    还有那个键盘的各个键在程序里怎么表示我也不大清楚,能否讲一下呢?
     WM_SYSCOMMAND有什么用?表示什么?最好介绍一篇有关这些问题的书给我看一下!
     THANK YOU VERY MUCH!
      

  4.   

    不知道下面这个有没有用,我不懂Hook
    ========================================
    利用Hook技术实现键盘监控 
    乌鲁木齐军医学院 
    卢立建 ---- 在许多系统中,出于安全或其它原因,常常要求随时对键盘进行监控,一个专业的监控程序必须具备两点,一是实时;二是作为指示图标运行。实际应用中把利用hook(即钩子)技术编写的应用程序添加到windows的任务栏的指示区中就能够很好的达到这个目的。我在参考了api帮助文档基础上,根据在delphi开发环境中的具体实现分别对这两部分进行详细论述。 
    一、hook(钩子)的实现: ---- hook是应用程序在microsoft windows 消息处理过程中设置的用来监控消息流并且处理系统中尚未到达目的窗口的某一类型消息过程的机制。如果hook过程在应用程序中实现,若应用程序不是当前窗口时,该hook就不起作用;如果hook在dll中实现,程序在运行中动态调用它,它能实时对系统进行监控。根据需要,我们采用的是在dll中实现hook的方式。 ---- 1.新建一个导出两个函数的dll文件,在hookproc.pas中定义了钩子具体实现过程。代码如下: library keyspy;
    uses
    windows, messages, hookproc in 'hookproc.pas';
    exports
    setkeyhook,
    endkeyhook;
    begin
    nexthookproc:=0;
    procsaveexit:=exitproc;
    exitproc:=@keyhookexit;
    end.2.在hookproc.pas中实现了钩子具体过程:
    unit hookproc;
    interface
    uses
    windows, messages, sysutils, controls, stdctrls;
    var
    nexthookproc:hhook;
    procsaveexit:pointer;
    function keyboardhook(icode:integer;wparam:wparam;
        lparam:lparam):lresult;stdcall;export;
    function setkeyhook:bool;export;//加载钩子
    function endkeyhook:bool;export;//卸载钩子
    procedure keyhookexit;far;
    const
    afilename='c:\debug.txt';//将键盘输入动作写入文件中
    var
    debugfile:textfile;
    implementation
    function keyboardhookhandler(icode:integer;wparam:wparam;
         lparam:lparam):lresult;stdcall;export;
    begin
    if icode<0 then
    begin
    result:=callnexthookex(hnexthookproc,icode,wparam,lparam);
    exit;
    end;
    assignfile(debugfile,afilename);
    append(debugfile);
    if getkeystate(vk_return)<0 then
    begin
    writeln(debugfile,'');
    write(debugfile,char(wparam));
    end
    else
    write(debugfile,char(wparam));
    closefile(debugfile);
    result:=0;
    end;
    function endkeyhook:bool;export;
    begin
    if nexthookproc<>0 then begin
    unhookwindowshookex(nexthookproc);
    nexthookproc:=0;
    messagebeep(0); end;
    result:=hnexthookproc=0;
    end;
    procedure keyhookexit;far;
    begin
    if nexthookproc<>0 then endkeyhook;
    exitproc:=procsaveexit; end;
    end.
    二、win95/98使用任务栏右方指示区来显示应用程序或工具图标对指示区图标的操作涉及了一个api函数shell_notifyicon,它有两个参数,一个是指向tnotifyicondata结构的指针,另一个是要添加、删除、改动图标的标志。通过该函函数将应用程序的图标添加到指示区中,使其作为图标运行,增加专业特色。当程序起动后,用鼠标右键点击图标,则弹出一个菜单,可选择sethook或endhook。 unit kb;
    interface
    uses
    windows, messages, sysutils, classes,
    graphics, controls, forms,
    dialogs,
    stdctrls, menus,shellapi;
    const
    icon_id=1;
    mi_iconevent=wm_user+1;//定义一个用户消息
    type
    tform1 = class(tform)
    popupmenu1: tpopupmenu;
    sethook1: tmenuitem;
    endhook1: tmenuitem;
    n1: tmenuitem;
    about1: tmenuitem;
    close1: tmenuitem;
    gettext1: tmenuitem;
    procedure formcreate(sender: tobject);
    procedure sethook1click(sender: tobject);
    procedure endhook1click(sender: tobject);
    procedure formdestroy(sender: tobject);
    procedure close1click(sender: tobject);
    private
    { private declarations }
    nid:tnotifyicondata;
    normalicon:ticon;
    public
    { public declarations }
    procedure icontray(var msg:tmessage); 
    message mi_iconevent;
    end;
    var
    form1: tform1;
    implementation
    {$r *.dfm}
    function setkeyhook:bool;external 'keyspy.dll';
    function endkeyhook:bool;external 'keyspy.dll';procedure tform1.icontray(var msg:tmessage);
    var
    pt:tpoint;
    begin
    if msg.lparam=wm_lbuttondown then
    sethook1click(self);
    if msg.lparam=wm_rbuttondown then
    begin
    getcursorpos(pt);
    setforegroundwindow(handle);
    popupmenu1.popup(pt.x,pt.y);
    end;
    end;procedure tform1.formcreate(sender: tobject);
    begin
    normalicon:=ticon.create;
    application.title:=caption;
    nid.cbsize:=sizeof(nid);
    nid.wnd:=handle;
    nid.uid:=icon_id;
    nid.uflags:=nif_icon or nif_message or nif_tip;
    nid.ucallbackmessage:=mi_iconevent;
    nid.hicon :=normalicon.handle;
    strcopy(nid.sztip,pchar(caption));
    nid.uflags:=nif_message or nif_icon or nif_tip;
    shell_notifyicon(nim_add,@nid);
    setwindowlong(application.handle,
    gwl_exstyle,ws_ex_toolwindow);
    end;procedure tform1.sethook1click(sender: tobject);
    begin
    setkeyhook;
    end;procedure tform1.endhook1click(sender: tobject);
    begin
    endkeyhook;
    end;procedure tform1.formdestroy(sender: tobject);
    begin
    nid.uflags :=0;
    shell_notifyicon(nim_delete,@nid);
    end;procedure tform1.close1click(sender: tobject);
    begin
    application.terminate;
    end;
    ---- 该程序虽然只用了几个shellai函数,但是它涉及到了在delphi中对dll的引用、钩子实现、对指示区的操作、用户定义消息的处理、文件的读写等比较重要的内容,我相信这篇文章能对许多delphi的初学者有所帮助。 
    ---- 该程序在win98、delphi4.0中正常运行。