• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2296
  • Last Modified:

VK_UP & VK_DOWN

Hello,

I want to do something in my app when the user presses the Up or Down arrow, so I wrote my code on the FormKeyDown event (with KeyPreview = true).
 
The problem is the following : the event doesn't happen when the up or down arrow is pressed but happen if any other key is pressed.

What can I do?
0
logosapience
Asked:
logosapience
  • 3
  • 2
  • 2
  • +4
1 Solution
 
LischkeCommented:
Well, you can't do anything if a control on your form uses VK_UP and VK_DOWN for its own handling and has the focus. It can tell the form not to pass these keys to its KeyDown/KeyPress event. It sounds strange, I know, as the property of the form is KeyPreview, but the window with the focus still gets its messages first. If it, by default, doesn't handle the key it passes the key to its parent form which checks for the preview property and (if KeyPreview is True) calls its KeyDown/KeyPress event and finally (if the key code hasn't been set to #0) the OnKeyDown/OnKeyPress events of the primary control are called. If the control doesn't call its inherited message handler for key messages then this chain is never triggered.

I know this because I have just written an ESC handling for a control which is my current project and I had to prevent the form to receive the ESC key in special cases...

What you could do is to pick other keys or avoid using controls which deal with up/down keys (which is hard to avoid as these keys are so often used for scrolling etc.).

Ciao, Mike
0
 
christhaCommented:

This code moves a label up or down
when the up/down arrow-key is pressed.

procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin

if key = 38 then begin   // up
  label1.top:=label1.top - 5;
end
else if key = 40 then begin   // down
    label1.top:=label1.top + 5;
end;
end;

0
 
logosapienceAuthor Commented:
The problem is that the formKeyDown does not happen...
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
AttarSoftwareCommented:
I agree with Mike :O)

Sounds like some other control is eating your messages...

Not much help, I know...soz...

Tim.
0
 
LischkeCommented:
christha, PLEASE, read the question first and post comments instead of answers unless you are totally sure the questioneer wants your contribution as answer! Thank you.

logosapience, you replied to christha but not to me. Don't you believe me or did it make you struck all of a heap ;-)?

 
Ciao, Mike
0
 
intheCommented:
would this help:

type
  TForm1 = class(TForm)
  private
    { Private declarations }
    Procedure CMDialogKey( Var msg: TCMDialogKey );
      message CM_DIALOGKEY;
 

  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

Procedure TForm1.CMDialogkey;
Begin
    Case msg.charcode of
               vk_Down:
                       showmessage('key down pressed');
                 vk_Up:
                       showmessage('key up pressed');
     else
     inherited;
    End;
  End;

Regards Barry
0
 
logosapienceAuthor Commented:
Thanks, it works...
0
 
LischkeCommented:
Oh is it really so easy? I'm ashamed :-/ Looks as if there's another "call back" path for keyboard input I haven't seen yet.

Thank you and

Ciao, Mike
0
 
MoondancerCommented:
Finalized today by Moondancer - EE Moderator
0
 
snehanshuCommented:
Hey!
  inthe's code works!
  Wonder why his answer wasn't accepted.
  ne way, thanks inthe. Can you explain what TCMDialogKey is?
...Shu
0
 
snehanshuCommented:
NM,
  A similar question has been asked recently, and while searching for an answer, I found this Q and found it interesting that a good answer was not accepted.
  If inthe is active, he can comment at:
http://www.experts-exchange.com/Q_20895615.html
  But if he isn't , I was hoping that it was more likely that an email due to my comment would bring him here.
...Shu
0

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

  • 3
  • 2
  • 2
  • +4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now