TForm & focus

Hello,

I have 2 TForm created, one above the other.
What I want to do is to use the VK_LEFT or VK_RIGHT key on the top-most window but the problem is when I press one of these 2 keys, the windows beside the other gets the focus.

How 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.

rwilson032697Commented:
The form which has focus will be sent these keypresses, it seems strange the the topmost form (which would normally have focus) isn't getting them.

I assume you are using an OnKeyDown handler in the form? You might like to make sure that KeyPreview property for the forms is set to true...

Cheers,

Raymond.
0
logosapienceAuthor Commented:
new thing :
I put the following procedure on each form :

Procedure CMDialogKey( Var msg: TCMDialogKey );
     message CM_DIALOGKEY;

and when I press VK_Left or Right, it's the procedure in the hidden window which is executed...

So, it means that the hidden window always have the focus...

I put a Form2.setfocus on the OnShow event of the topmost window but it's the same.
0
rwilson032697Commented:
Have you tried it with a simple OnKeyDown event in the form?

Cheers,

Raymond.
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

logosapienceAuthor Commented:
Yes I tried but all this happens before the onkeydown event...
0
intheCommented:
hello,
if trapping the CM_DIALOGKEY you'll need to do the VK_LEFT / VK_RIGHT  events inside the message handler:
Regards Barry


unit Unit2;

interface

uses
  Windows, Messages, SysUtils, Classes,Controls,Forms, Dialogs,
   ExtCtrls, StdCtrls;

type
  TForm2 = class(TForm)
    Button1: TButton;
    Button2: TButton;
  private
    { Private declarations }
  procedure CMDialogKey(var Message: TCMDialogKey); message CM_DIALOGKEY;
 public
    { Public declarations }
  end;

var
  Form2: TForm2;

implementation

{$R *.DFM}

procedure TForm2.CMDialogKey(var Message: TCMDialogKey);
begin
  with Message do
    if ((message.CharCode = VK_LEFT)  or (CharCode = VK_RIGHT))
      then
    begin
    Result := 1;
    //do your stuff like showmessage whatever
    showmessage('lets change focus');
    //send focus to next control
    SendMessage(handle,WM_NEXTDLGCTL,0,0);
    end
    else
      Result := 0;
end;

end.


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
logosapienceAuthor Commented:
Ok, it works, thanks very much!
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.