Link to home
Start Free TrialLog in
Avatar of moonrise
moonrise

asked on

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.
 
Avatar of moonrise
moonrise

ASKER

Edited text of question.
ASKER CERTIFIED SOLUTION
Avatar of inthe
inthe

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
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.
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.
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.
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; ?
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.
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
Thank you. I was able to work something up from what you gave me.