Link to home
Create AccountLog in
Avatar of AndrewJohnGraham
AndrewJohnGrahamFlag for Australia

asked on

Delphi keyboard arrow keys

Hi experts,
 I have a large Delphi application and on the main form I am trying to react to the pressing of the keyboard arrow keys (up, down, left, right). If I have the main form 'keypreview' set to false I can never get the 'on key' events to trigger. If I set 'keypreview' to true then the 'onkeyup' event always triggers but the 'onkeydown' event (the one I actually want) only triggers after the 4th keydown (arrow keys - character keys always trigger) and then continues to trigger for each subsequent event. I've set as many component 'tabstop' to false as I can find.
 I assume some component is capturing the event but how do I find out which one or is there some other explanation?
 Thanks
Avatar of jimyX
jimyX

If "KeyPreview" is set to true that enables your application to trigger Keypressing even if the focus is on another control (catch of keys will be at the Form level instead of passing them to the components).

Try to clear the code on that procedure and replace it with this one to ensure that it is not from your code:

procedure TForm1.FormKeyDown(Sender: TObject;
  var Key: Word; Shift: TShiftState);
begin
  case Key of
    VK_Left : Showmessage('Left');
    VK_Right : Showmessage('Right');
    VK_Up : Showmessage('Up');
    VK_Down : Showmessage('Down');
  end;
Key := 0;
end; 

Open in new window


Do not forget to set "KeyPreview" to True.
Let's see if it has the same behavior.

Also you can try the OnKeypress:

procedure TForm1.FormKeyPress(Sender: TObject; var Key: Char);
begin
  case ord(Key) of
    VK_Left : Showmessage('Left');
    VK_Right : Showmessage('Right');
    VK_Up : Showmessage('Up');
    VK_Down : Showmessage('Down');
  end;
Key := #0;
end;

Open in new window

VK keys (arrows) do not trigger onKeyPress, only onKeyUp/Down

test with an empty application/form to first be sure you don't have any strange component interference
ASKER CERTIFIED SOLUTION
Avatar of Thommy
Thommy
Flag of Germany image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Hi epasquier,
Long time man, where have you been? Belated Merry Christmas and happy New Year.
What I know is that Delete key and some more keys do not work at OnKeyPress but never tried arrows. Theoretically it seems like it might work. Thanks for catching that.

Andrew,
Also you can read the state of keys by using GetKeyState(Key) to ensure the status of the arrow keys:

procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if (GetKeyState(VK_Left) and 128) = 128 then
    Showmessage('Left');
  if (GetKeyState(VK_Right) and 128) = 128 then
    Showmessage('Right');
  if (GetKeyState(VK_VK_Up) and 128) = 128 then
    Showmessage('Up');
  if (GetKeyState(VK_Down) and 128) = 128 then
    Showmessage('Down');
end;

Open in new window

Hi Thommy,
Also you disappeared, where do you people go so I can go as well ;-)
Merry Christmas and Happy New Year.
FormKeyDown event should capture your key stroke with no problems then you can use the virtual key codes to determine the key pressed (VK_LEFT, VK_RIGHT, VK_UP, VK_DOWN as already shown)

Check if you have any other code in the form thats interfering or intercepting the key stroke
Avatar of AndrewJohnGraham

ASKER

Brilliant. I've never heard of this message service but it really works! It's also allowed me to tidy up the keydown code on several other screens too.
Thanks also to the other experts you've all been a real help.
Thanks for the points and a successful new year...
:o)))