Code:
var
cnt1 : integer = 0;
cnt2 : integer = 0;
LKeys: TArray<Word> = []; // array of word
function ThisKeysWasPressed: string;
begin
for var K in LKeys do // a..z
result := result + ', ' + VirtualKeyCodeToKeyName(K);
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
Memo1.Enabled := false;
Memo1.Lines.Clear;
Self.KeyPreview := true;
end;
procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
inc(cnt1);
//
LKeys := LKeys + [Key];
//
if (Key = 18) then // start again...
LKeys := [Key];
// testing...
Label1.Caption := 'Counting keys pressed: key=' + Key.ToString + ', Name=' + VirtualKeyCodeToKeyName(Key) + ', cnt1=' + cnt1.ToString;
end;
procedure TForm1.FormKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
inc(cnt2);
//
Memo1.Text := ThisKeysWasPressed.Remove(0, 2);
//
Label2.Caption := 'Counting keys pressed: key=' + Key.ToString + ', Name=' + VirtualKeyCodeToKeyName(Key) + ', cnt2=' + cnt2.ToString;
end;
Code:
function VirtualKeyCodeToKeyName(vk: TVirtualKeyCode): string; // from internet code... not mine!!!
var
nSize: Integer;
begin
// Win32 API can't translate mouse button virtual keys to string
case vk of
VK_LBUTTON:
Result := cLBUTTON;
VK_MBUTTON:
Result := cMBUTTON;
VK_RBUTTON:
Result := cRBUTTON;
else
nSize := 32; // should be enough
SetLength(Result, nSize);
vk := MapVirtualKey(vk, 0);
nSize := GetKeyNameText((vk and $FF) shl 16, PChar(Result), nSize);
SetLength(Result, nSize);
end;
end;
Last edited: