[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Respond to arrow keys in form

Posted on 1998-05-23
12
Medium Priority
?
312 Views
Last Modified: 2010-04-06
Hello,

How do I make my form respond to the arrow keys (assigning my own procedures behind them) and ignore the default behavior (movement between components on the form) when pressing the arrow keys?

Regards,
Frank.
0
Comment
Question by:franksymons
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 2
  • +2
12 Comments
 
LVL 1

Expert Comment

by:ssite
ID: 1347462
Apparently, the code that does this is deep down in controls.pas and you can't effectively override it just in one place.

If you really want to do this, you need to inherit a new component from each one where you want the behaviour to change and then you need to watch for the CN_KeyDown message and then check if message.charcode is one of the VK_DOWN VK_LEFT VK_RIGHT or VK_UP. If so, dont call inherited and do your thing. otherwise, call inherited.

I'm pretty sure this would work.

Another solution for the more professional would be to hook your own process and then look out for WM_Keydown messages.

If you want the second one, write your e-mail and I'll tell you more.
0
 

Author Comment

by:franksymons
ID: 1347463
Could you be more specific?
0
 
LVL 2

Expert Comment

by:mvz121697
ID: 1347464
Set the form's KeyPreview property to True (in object inspector).

Then add code to the 'OnKeyDown' event.

Greetings,
MvZ
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Expert Comment

by:mvz121697
ID: 1347465
In the OnkeyDown event code something like:

 if Key=VK_DOWN then begin
   MessageBeep(0);
   Key:=0;    
 end;

Set Key to 0 to prevent the default behaviour.
other key-values are VK_LEFT, VK_RIGHT.

Greetings,
MvZ
0
 

Author Comment

by:franksymons
ID: 1347466
Thanks for your answer, providing there is nothing on the form this works, but try adding a button and an edit box on the form. Use tab to make sure the button has the focus, now when pushing the down arrow key focus whill shift from the button to the edit box without executing the procedure. Pushing the down key with the editbox in focus will perform the procedure.
That's not what I'm looking for, I want the procedure to run every time, regardless of what may be on the form.
0
 
LVL 3

Expert Comment

by:rickpet
ID: 1347467
Okay...the fix all for capturing keystrokes before they go to components...

use TPopUpMenu.  The menu items have a visible property which you can set so that items are not shown to your users.  If you need to know what component had focus look at Screen.ActiveControl.  The Shortcut function will take virtual keystrokes and convert them to TShortCuts.

procedure TForm1.FormCreate(Sender: TObject);
begin
//Note the forms PopUpMenu Property is set to the PopMenu
  Left1.ShortCut := ShortCut(vk_down,[]);
  Right1.ShortCut := ShortCut(vk_right,[]);
end;

procedure TForm1.left1Click(Sender: TObject);
begin
  Memo1.SetFocus;
end;


Rick
0
 

Author Comment

by:franksymons
ID: 1347468
Thanks for the answer, but I don't wan't to solve it that way. There has to be a way to catch the windows message itself and then send it to whereever I want it to.
0
 
LVL 1

Expert Comment

by:titz
ID: 1347469
hallo,
i think you should kill the focus of the button or the edit field.
i did this with
button.enable:=false;
button.enable:=true;
then the button has no focus and you can handle the arrowkeys with the OnKeydownEvent (or OnKeypressedEvent)
i think its not elegant but it works.
regards
titz

0
 
LVL 3

Expert Comment

by:rickpet
ID: 1347470
Okay...tried this a couple different ways...tried to create a new button that overrode WndProc...but the Wm_ButtonDown never comes through this handler...it's dispatched by the Component ahead of it in Tab Order(watched it in WinSight to verify this).

So we went to the ultimate message handler...Application.OnMessage...that works...

procedure TForm1.FormCreate(Sender: TObject);
var
  myButton: TMybutton;
begin
  Application.OnMessage := MyMessage;
end;

procedure TForm1.MyMessage(var Msg: TMsg; var Handled: Boolean);
begin
if (Screen.ActiveControl is TButton) and (Msg.message = WM_KeyDown) then
  begin
    if (Msg.WParam = vk_Left) or (Msg.WParam = vk_Right) then
      Handled := true;
  end;
end;


Rick
0
 
LVL 3

Expert Comment

by:rickpet
ID: 1347471
Note certain controls handle the Left Arrow and Right Arrow different than Buttons...i.e. TEdit.

Rick
0
 

Author Comment

by:franksymons
ID: 1347472
Thanks titz, but I'll go with the comments that rickpet gave me. It's exactly what I need. Rickpet if you want the points form it in a question and they're yours.
0
 
LVL 3

Accepted Solution

by:
rickpet earned 800 total points
ID: 1347473
Thanks...

Rick
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question