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?
LVL 1
logosapienceAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.