[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 466
  • Last Modified:

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?
0
logosapience
Asked:
logosapience
  • 3
  • 2
1 Solution
 
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
2018 Annual Membership Survey

Here at Experts Exchange, we strive to give members the best experience. Help us improve the site by taking this survey today! (Bonus: Be entered to win a great tech prize for participating!)

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

Featured Post

[Webinar] Improve your customer journey

A positive customer journey is important in attracting and retaining business. To improve this experience, you can use Google Maps APIs to increase checkout conversions, boost user engagement, and optimize order fulfillment. Learn how in this webinar presented by Dito.

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