Prevent window from getting focus

I developed a form which I use as an onscreen keyboard. I never want this form to get the focus since the field to receive the characters must retain the focus.

How can I prevent a form from ever getting the focus. I am able to acomplish some of it with

procedure TFormKeyboard.WMMouseActivate(var Message: TMessage);
begin
  Message.Result := MA_NOACTIVATE;
end;

The keyboard form still gets the focus on FormKeyboard.Show and if the user moves it.
 
moonriseAsked:
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.

moonriseAuthor Commented:
Edited text of question.
0
intheCommented:
Hi,
you need a WmNCHitTest handler as well so your form looks like:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;

type
  TForm1 = class(TForm)
  private
    { Private declarations }
   procedure WMMouseActivate(var Msg: TWMMouseActivate); message WM_MOUSEACTIVATE;
 procedure WmNCHitTest(var Msg :TWMNCHitTest); message WM_NCHITTEST;

  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.WMMouseActivate(var Msg: TWMMouseActivate);
 begin
  Msg.Result := MA_NOACTIVATE;
end;

procedure TForm1.WmNCHitTest(var Msg: TWMNCHitTest);
begin
  DefaultHandler(Msg);
  if Msg.Result = HTCAPTION then
    Msg.Result := HTNOWHERE;
end;

end.


Regards Barry
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
intheCommented:
i was bit worried if keyboard still get focus so i tested with keyboard component:(maybe same one as you are using)

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ExtCtrls, Keyboard;

type
  TForm1 = class(TForm)
    Keyboard1: TKeyboard;
    procedure Keyboard1Key(Sender: TObject; Char: Char);
  private
    { Private declarations }
   procedure WMMouseActivate(var Msg: TWMMouseActivate); message WM_MOUSEACTIVATE;
 procedure WmNCHitTest(var Msg :TWMNCHitTest); message WM_NCHITTEST;

  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.WMMouseActivate(var Msg: TWMMouseActivate);
 begin
  Msg.Result := MA_NOACTIVATE;
end;

procedure TForm1.WmNCHitTest(var Msg: TWMNCHitTest);
begin
  DefaultHandler(Msg);
  if Msg.Result = HTCAPTION then
    Msg.Result := HTNOWHERE;
end;

procedure TForm1.Keyboard1Key(Sender: TObject; Char: Char);
begin
showmessage(char);
end;

end.

it seems to work well as the keyboard still get focus correctly but form doesnt.
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

moonriseAuthor Commented:
I tried your suggestion. The form still gets the focus when I "Show" it. Also, from what i understand this prevents the form from being moved - the user needs to be able to move the keyborad around so this will not work for me.
Thank you.
0
moonriseAuthor Commented:
I tried your suggestion. The form still gets the focus when I "Show" it. Also, from what i understand this prevents the form from being moved - the user needs to be able to move the keyborad around so this will not work for me.
Thank you.
0
intheCommented:
ok now the form only move if mouse is on component:


unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ExtCtrls, Keyboard;

type
  TForm1 = class(TForm)
    Keyboard1: TKeyboard;
    procedure Keyboard1Key(Sender: TObject; Char: Char);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    { Private declarations }
   procedure WMMouseActivate(var Msg: TWMMouseActivate); message WM_MOUSEACTIVATE;
 procedure WmNCHitTest(var Msg :TWMNCHitTest); message WM_NCHITTEST;

  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

function MouseOverComponent(Msg: TWMNCHitTest; Sender : TControl) : boolean; begin
  Msg.XPos := Msg.XPos - Sender.Parent.Left;
  Msg.YPos := Msg.YPos - Sender.Parent.Top;
  result := true;
  result := result AND (Msg.XPos >= Sender.Left);
  result := result AND (Msg.XPos <=  Sender.Left + Sender.Width);   result := result AND (Msg.YPos >= Sender.Top);
  result := result AND (Msg.YPos <=  Sender.Top + Sender.Height); end;



procedure TForm1.WMMouseActivate(var Msg: TWMMouseActivate);
 begin
  Msg.Result := MA_NOACTIVATE;
end;

{procedure TForm1.WmNCHitTest(var Msg: TWMNCHitTest);
begin

  if Msg.Result = HTCLIENT then
    Msg.Result := HTCAPTION
   else if
    }
    procedure TForm1.WMNCHitTest(var Msg: TWMNCHitTest);
begin
  inherited;
  DefaultHandler(Msg);
  IF Msg.Result = HTCAPTION then
    Msg.Result := HTCLIENT
    ELSE
  if (htClient = Msg.Result) and (MouseOverComponent(Msg,kEYBOARD1))then
    Msg.Result := htCaption;
end;


procedure TForm1.Keyboard1Key(Sender: TObject; Char: Char);
begin
showmessage(char);
end;

procedure TForm1.FormCreate(Sender: TObject);
var
  L : LongInt;    //stop alt tab keys
begin
  SystemParametersInfo (97, Word (True), @L, 0);
  end;

procedure TForm1.FormDestroy(Sender: TObject);
var
  L : LongInt;    //re-enable alt tab keys
begin
  SystemParametersInfo (97, Word (False), @L, 0);
  end;

end.
0
intheCommented:
yes form still gets focus when shown though :-( working on that one

just wondering are you calling the form from another form
ie:
form2.showmodal; ?
0
moonriseAuthor Commented:
Here is a better explanation of what I am trying to do:

I have two forms:

Form1 has a button with Form2.Show when clicked.

Form2 has the keyboard component.

The button Form1 and the buttons used in the keyboard do not get the focus (they are like speedbuttons).

When I click on the button on Form1 I want Form2 to be displayed but I want Form1 to retain the focus so that characters typed on the keyboard on Form2 will be displayed on Form1.
Thank you.
0
intheCommented:
HI,
 you know you can do this on the keyboard form to send text to form1 editbox for instance:

procedure TForm2.Keyboard1Key(Sender: TObject; Char: Char);
begin
Form1.Edit1.Text := Form1.Edit1.Text + char;
end;

well this was best i got:


unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ExtCtrls, Keyboard;

type
  TForm1 = class(TForm)
    Keyboard1: TKeyboard;
    procedure Keyboard1Key(Sender: TObject; Char: Char);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure FormActivate(Sender: TObject);
  private
    { Private declarations }
  procedure WMMouseActivate(var Msg: TWMMouseActivate); message WM_MOUSEACTIVATE;
  procedure WmNCHitTest(var Msg :TWMNCHitTest); message WM_NCHITTEST;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

uses Unit2;

{$R *.DFM}

function MouseOverComponent(Msg: TWMNCHitTest; Sender : TControl) : boolean;
 begin
  Msg.XPos := Msg.XPos - Sender.Parent.Left;
  Msg.YPos := Msg.YPos - Sender.Parent.Top;
  result := true;
  result := result AND (Msg.XPos >= Sender.Left);
  result := result AND (Msg.XPos <=  Sender.Left + Sender.Width);
  result := result AND (Msg.YPos >= Sender.Top);
  result := result AND (Msg.YPos <=  Sender.Top + Sender.Height);
 end;

procedure TForm1.WMMouseActivate(var Msg: TWMMouseActivate);
 begin
  Msg.Result := MA_NOACTIVATE;
end;

procedure TForm1.WMNCHitTest(var Msg: TWMNCHitTest);
 var
  h : hwnd;
begin
  inherited;
  DefaultHandler(Msg);
  IF Msg.Result = HTCAPTION then
    Msg.Result := HTCLIENT
    ELSE
  if (htClient = Msg.Result) and (MouseOverComponent(Msg,kEYBOARD1))then
    Msg.Result := htCaption;
h := getnextwindow(form1.handle,GW_HWNDFIRST);
setforegroundwindow(h);

end;


procedure TForm1.Keyboard1Key(Sender: TObject; Char: Char);
begin
Form2.Edit1.Text := Form2.Edit1.Text + char;
end;

procedure TForm1.FormCreate(Sender: TObject);
var
  L : LongInt;    //stop alt tab keys
begin
  SystemParametersInfo (97, Word (True), @L, 0);
  end;

procedure TForm1.FormDestroy(Sender: TObject);
var
  L : LongInt;    //re-enable alt tab keys
begin
  SystemParametersInfo (97, Word (False), @L, 0);
  end;

procedure TForm1.FormActivate(Sender: TObject);
var
  h : hwnd;
begin
h := getnextwindow(form1.handle,GW_HWNDFIRST);
setforegroundwindow(h);

end;

end.

i know its not perfect :-(
Regards Barry
0
moonriseAuthor Commented:
Thank you. I was able to work something up from what you gave me.
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.