这个简单的捕获键可以作为独立的 VCL 项目正常工作
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, DBCtrls, DBCGrids, Db, DBTables, ExtCtrls;
type
TForm1 = class(TForm)
Memo1: TMemo;
Timer1: TTimer;
Label1: TLabel;
procedure Timer1Timer(Sender: TObject);
private
{ Private declarations }
FLastMsg: TMsg;
public
{ Public declarations }
end;
var
Form1: TForm1;
capturedWord:string;
implementation
{$R *.DFM}
function GetCharFromVirtualKey(Key: Word): string;
var
keyboardState: TKeyboardState;
asciiResult: Integer;
begin
GetKeyboardState(keyboardState) ;
SetLength(Result, 2) ;
asciiResult := ToAscii(key, MapVirtualKey(key, 0), keyboardState, @Result[1], 0) ;
case asciiResult of
0: Result := '';
1: SetLength(Result, 1) ;
2:;
else
Result := '';
end;
end;
procedure TForm1.Timer1Timer(Sender: TObject);
var i,j:integer;
begin
if ((GetAsyncKeyState(13) and 1) = 1) or ((GetAsyncKeyState(32) and 1) = 1) then begin;
capturedWord:='';
exit;
end;
For i:=32 to 128 do begin;
if ((GetAsyncKeyState(i) and 1) = 1) then begin;
capturedWord:=capturedWord + GetCharFromVirtualKey(i);
capturedWord:=LowerCase(capturedWord);
label1.Caption:= (capturedWord);
end;
end;
end;
end.
但是当我将它集成到我的项目中时,它会捕获中文或无意义的键!
知道为什么吗?