Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

my Capturing "keys" pressed after "ALT" key using FormKeyDown event... (my vagabund code)

emailx45

Programmer
Jan 10, 2025
3
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;
Project1_RGegbM0htx.gif
 
Last edited:

Part and Inventory Search

Sponsor

Back
Top