Re: Arrow Key Pressed

Posted by Frankie on March 11, 2009

In Reply to Re: Arrow Key Pressed posted by Stowaway on March 09, 2009

: Another thing im trying to do is to detect if the left AND up key are pressed at the same time.

You never can press 2 keys at exactly the same time. You have to remember the first key that is pressed, and on the next key-press check if that first key is still down. For the 2 keys that you want, you need 2 variables that remember if LEFT or UP are down. The variables are reset with the OnKeyUp event. Here is the code that I tried:

var
  LEFTpressed, UPpressed: Boolean;
  
procedure TForm1.FormCreate(Sender: TObject);
begin
  Label1.Caption := '...';
  LEFTpressed := False;
  UPpressed := False;
end;
  
procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  case Key of
    VK_LEFT:  LEFTpressed := True;
    VK_UP :   UPpressed   := True;
  end;
  ShowKeysPressed;
end;
  
procedure TForm1.FormKeyUp(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  case Key of
    VK_LEFT:  LEFTpressed := False;
    VK_UP :   UPpressed   := False;
  end;
  ShowKeysPressed;
end;
  
procedure TForm1.ShowKeysPressed;
begin
  if LEFTpressed and UPpressed then Label1.Caption := 'LEFT + UP'
  else if LEFTpressed then Label1.Caption := 'LEFT'
  else if UPpressed then Label1.Caption := 'UP'
  else Label1.Caption := '...';
end;

Good luck!
Frankie

Related articles

       

Follow Ups