Smilly
asked on
Keyboard Problem
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...???
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...???
Try handling the WM_KILLFOCUS message and returning a non-zero value when the component receives it...
ASKER
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......
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......
case key of
VK_LEFT, VK_RIGHT, VK_UP, VK_DOWN : ButtonName.SetFocus;
end;
Merry Chrismas!
-Viktor
--Ivanov
VK_LEFT, VK_RIGHT, VK_UP, VK_DOWN : ButtonName.SetFocus;
end;
Merry Chrismas!
-Viktor
--Ivanov
ASKER
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........
But okey, thanks anyway..... I'll make my component based on
EDIT control instead........
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...
ASKER
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(va r Msg: TWMKillFocus);
begin
inherited;
setfocus;
msg.Result := 1;
end;
But I can't test for the arrow keys.
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(va
begin
inherited;
setfocus;
msg.Result := 1;
end;
But I can't test for the arrow keys.
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
//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
ASKER
Hello viktornet, nope, have you tried it, it doesent work....
The event keydown, doesen't check for arrow keys.....
The event keydown, doesen't check for arrow keys.....
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.
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.
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(AOw ner: TComponent);
begin Inherited;
FNoWayOut := True;
Width := 100;
end;
procedure TNoWayOutButton.Doexit;
begin
if FNoWayOut then SetFocus
else Inherited;
end;
procedure Register;
begin
RegisterComponents('Sample s', [TNoWayOutButton]);
end;
end.
__________________________ __________ __________ __________ __________ _________
Good luck, and a happy new year!
--Matvey
__________________________
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(AOw
begin Inherited;
FNoWayOut := True;
Width := 100;
end;
procedure TNoWayOutButton.Doexit;
begin
if FNoWayOut then SetFocus
else Inherited;
end;
procedure Register;
begin
RegisterComponents('Sample
end;
end.
__________________________
Good luck, and a happy new year!
--Matvey
ASKER
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(Send er: 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;
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(Send
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;
-The component allows it!!! You get the keyboard events, the only diffeerence is that you can't turn the focus away from it.
ASKER
Hello Matvey,
Nope, you can't test for arrow keys on a Tbutton, as
default...
There must be a way to do this.....
Nope, you can't test for arrow keys on a Tbutton, as
default...
There must be a way to do this.....
I meant the component I posted - TNoWayOutButton
ASKER
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.......
TButton, and you cant check the arrow keys with it.....
Try it yourself, you cant check the arrow keys, with your
component.......
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(va r Message: TMessage);
begin inherited;
DefaultHandler(Message.Msg );
end;
__________________________ __________ __________ __________ ________
--Matvey
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(va
begin inherited;
DefaultHandler(Message.Msg
end;
__________________________
--Matvey
ASKER
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.......
That was just what I was seeking for......
But you have to make your comment to a answer,
so you can get your points.......
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.