Solved

Keyboard Problem

Posted on 1998-12-23
18
256 Views
Last Modified: 2010-04-04
Hi There,
Is there some of you who know, how to snap all keyevents
in a TWinControl.
I am making a editor component of a TWinControl,
and I want to be able to check the keyword.
But when I press the arrow keys, then my control lose
focus. How can I disable this...???
0
Comment
Question by:Smilly
  • 8
  • 6
  • 3
  • +1
18 Comments
 
LVL 3

Expert Comment

by:Matvey
ID: 1352663
Try handling the WM_KILLFOCUS message and returning a non-zero value when the component receives it...
0
 
LVL 1

Author Comment

by:Smilly
ID: 1352664
Hello Matvey,
It doesn't work, I have tried this.
Lets say you got a TButton, how will you avoid when
you press the arrow keys, the focus jump to another
control......
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1352665
case key of
  VK_LEFT, VK_RIGHT, VK_UP, VK_DOWN : ButtonName.SetFocus;
end;

Merry Chrismas!

-Viktor
--Ivanov
0
 
LVL 1

Author Comment

by:Smilly
ID: 1352666
Nope viktornet, It doesn't help me, it's pretty mush the same as the first answer from Matvey.
But okey, thanks anyway..... I'll make my component based on
EDIT control instead........

0
 
LVL 10

Expert Comment

by:viktornet
ID: 1352667
Did u try it at all?? I think that handling the WM_KILLFOCUS message as Matvey mentioned will do exactly what you want without any problems...
0
 
LVL 1

Author Comment

by:Smilly
ID: 1352668
Hello Viktornet,
Yes, I have....

procedure TKeyControl.WMKeyDown(var Message: TWMKeyDown);
begin
  case Message.CharCode of
       VK_LEFT, VK_RIGHT, VK_UP, VK_DOWN : caption:='Arrow';
  else
       caption:='None Arrow';
  end;
  inherited;
end;

procedure TKeyControl.WMKillFocus(var Msg: TWMKillFocus);
begin
     inherited;
     setfocus;
     msg.Result := 1;
end;

But I can't test for the arrow keys.
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1352669
Ok, you tested the one with WM_KILLFOCUS, but the one that I told you before is not the same as the one that you have tested... Yo should put this code in the OnKeyDown() of the button...

//I don't remember if the parameters were as I put them here, but you got the idea
procedure TForm1.Button1KeyDown(var Key : Word; Sender : TObject);
begin
  case Key of
       VK_LEFT, VK_RIGHT, VK_UP, VK_DOWN : Button1.SetFocus;
  end;
end;

Now this one will work for sure....

-Viktor
--Ivanov
0
 
LVL 1

Author Comment

by:Smilly
ID: 1352670
Hello viktornet, nope, have you tried it, it doesent work....
The event keydown, doesen't check for arrow keys.....

0
 
LVL 44

Expert Comment

by:bruintje
ID: 1352671
Happy new year everyone.

Strange didn't see it work either, and in the end I did had to override the keydown of TButton.

Found something in the help file

type
  TWMKey = record
    Msg: Cardinal;
    CharCode: Word;
    KeyData: Longint;
    Result: Longint;
  end;

function DoKeyDown(var Message: TWMKey): Boolean;

This is an protected method of TWinControl and as stated there it does the preprocessing of the keydown event.

Hope this helps.
:O) bruintje.

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 3

Expert Comment

by:Matvey
ID: 1352672
OK, this works:

___________________________________________________________________________
unit NoWayOutButton;

interface
uses Classes, StdCtrls;

type
  TNoWayOutButton = class(TButton)
  private
    FNoWayOut: Boolean;
  protected
    procedure doExit; override;
  public
    constructor Create(AOwner: TComponent); override;
  published
    property NoWayOut: Boolean read FNoWayOut write FNoWayOut default true;
  end;

procedure Register;

implementation

constructor TNoWayOutButton.Create(AOwner: TComponent);
begin Inherited;
  FNoWayOut := True;
  Width := 100;
end;

procedure TNoWayOutButton.Doexit;
begin
  if FNoWayOut then SetFocus
  else Inherited;
end;

procedure Register;
begin
  RegisterComponents('Samples', [TNoWayOutButton]);
end;

end.
___________________________________________________________________________

Good luck, and a happy new year!
--Matvey
0
 
LVL 1

Author Comment

by:Smilly
ID: 1352673
Hello Matvey,
Okey, now you can't get out from the button, but thats
not my Question, my Question is how can I test the arrow
keys inside a button without to lose focus......

Example:

procedure TForm1.Button1KeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
     if (key=VK_LEFT)  then button1.caption:='Left'
     if (key=VK_UP)    then button1.caption:='Up'
     if (key=VK_RIGHT) then button1.caption:='Right'
     if (key=VK_DOWN)  then button1.caption:='Down'
end;
0
 
LVL 3

Expert Comment

by:Matvey
ID: 1352674
-The component allows it!!! You get the keyboard events, the only diffeerence is that you can't turn the focus away from it.
0
 
LVL 1

Author Comment

by:Smilly
ID: 1352675
Hello Matvey,
Nope, you can't test for arrow keys on a Tbutton, as
default...
There must be a way to do this.....

0
 
LVL 3

Expert Comment

by:Matvey
ID: 1352676
I meant the component I posted - TNoWayOutButton
0
 
LVL 1

Author Comment

by:Smilly
ID: 1352677
Yes I knew that, but you TNoWayOutButton is made out of a
TButton, and you cant check the arrow keys with it.....
Try it yourself, you cant check the arrow keys, with your
component.......
0
 
LVL 3

Expert Comment

by:Matvey
ID: 1352678
Sorry, I really didn't check it. But it's easy to fix:

One way is to use the KeyUp event instead, and another way is to override one method in the TButton by this:

________________________________________________________________
  protected
    procedure WndProc(var Message: TMessage); override;

......................................................

procedure TNoWayOutButton.WndProc(var Message: TMessage);
begin inherited;
 DefaultHandler(Message.Msg);
end;
________________________________________________________________

--Matvey
0
 
LVL 1

Author Comment

by:Smilly
ID: 1352679
Thanks Matvey,
That was just what I was seeking for......
But you have to make your comment to a answer,
so you can get your points.......

0
 
LVL 3

Accepted Solution

by:
Matvey earned 20 total points
ID: 1352680
Glad we got it at last. All the best wishes for the new year!

Matvey
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
calling Tcolordialog in dll make the form disappear 8 103
Delphi 2 51
LAN or WAN ? 11 83
How do I add a client SSL certificate to a SOAP client in Rad Studio Delphi? 2 47
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

863 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now