请问如何拦截到输入的中文文字?如果用hook拦截键盘的话,则只能得到输入的英文字母,不能得到输入的中文。如何才能正确拦截到输入的中文?(最好有例子)

解决方案 »

  1.   

    SetWindowsHookEx(WH_GETMESSAGE, @GetMsgProc, HInstance,0);function GetMsgProc(Code: Integer; WParam: WParam; LParam: LParam): LRESULT;
    var
      theMsg: TMessage;
    begin
      Result := 0;
      if Code < 0 then begin
        Result := CallNextHookEx(HOOK, Code, WParam, LParam);
        Exit;
      end;
      if Code = HC_ACTION then begin
        if (PMsg(LParam).message = WM_IME_COMPOSITION) then begin
          theMsg.Msg := PMsg(LParam).message;
          theMsg.WParam := PMsg(LParam).wParam;
          theMsg.LParam := PMsg(LParam).lParam;
          Proc(theMsg);
        end;
        Result := CallNextHookEx(HOOK, Code, WParam, LParam);
      end
    end;procedure Proc(var Message: TMessage);
    var
      hIMC: Integer;
      dwSize: Integer;
      hstr: Integer;
      lpstr: Pointer;
    begin
      if Message.Msg = WM_IME_COMPOSITION then begin
        if (Message.lParam and GCS_RESULTSTR) <> 0 then begin
          hIMC := ImmGetContext(GetFocus);
          dwSize := ImmGetCompositionString(hIMC, GCS_RESULTSTR, nil, 0);
          dwSize := dwSize + sizeof(WCHAR);
          hstr := GlobalAlloc(GHND,dwSize);
          lpstr := GlobalLock(hstr);
          ImmGetCompositionString(hIMC, GCS_RESULTSTR, lpstr, dwSize);
          ImmReleaseContext(GetFocus, hIMC);
          SaveStrToFile(lpstr);//保存函数你自己写吧
        end;
      end;
    end;
      

  2.   

    多谢你的回复!
    但按照你的代码运行,不能拦截到任何数据。我的程序如下,请看看哪里不对。unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls,Imm;type
      TForm1 = class(TForm)
        Edit1: TEdit;
        procedure FormCreate(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;
    var
      Form1: TForm1;
      HOOK:integer;
    implementation
    {$R *.dfm}
    procedure Proc(var Message: TMessage);
    var
      hIMC: Integer;
      dwSize: Integer;
      hstr: Integer;
      lpstr: Pointer;
    begin
      if Message.Msg = WM_IME_COMPOSITION then begin
        if (Message.lParam and GCS_RESULTSTR) <> 0 then begin
          hIMC := ImmGetContext(GetFocus);
          dwSize := ImmGetCompositionString(hIMC, GCS_RESULTSTR, nil, 0);
          dwSize := dwSize + sizeof(WCHAR);
          hstr := GlobalAlloc(GHND,dwSize);
          lpstr := GlobalLock(hstr);
          ImmGetCompositionString(hIMC, GCS_RESULTSTR, lpstr, dwSize);
          ImmReleaseContext(GetFocus, hIMC);
          //SaveStrToFile(lpstr);//保存函数你自己写吧
          showmessage('拦截成功');
        end;
      end;
    end;function GetMsgProc(Code: Integer; WParam: WParam; LParam: LParam): LRESULT;
    var
      theMsg: TMessage;
    begin
      Result := 0;
      if Code < 0 then begin
        Result := CallNextHookEx(HOOK, Code, WParam, LParam);
        Exit;
      end;
      if Code = HC_ACTION then begin
        if (PMsg(LParam).message = WM_IME_COMPOSITION) then begin
          theMsg.Msg := PMsg(LParam).message;
          theMsg.WParam := PMsg(LParam).wParam;
          theMsg.LParam := PMsg(LParam).lParam;
          Proc(theMsg);
        end;
        Result := CallNextHookEx(HOOK, Code, WParam, LParam);
      end
    end;
    procedure TForm1.FormCreate(Sender: TObject);
    begin
      Hook:=SetWindowsHookEx(WH_GETMESSAGE, @GetMsgProc, HInstance,0);
    end;end.
      

  3.   

    多谢!现在已经可以在word、wps等编辑软件中拦截到任何的中、英文输入了。