The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages - More
> Delphi Programming
|
Enable arrow keys
Discuss Enable arrow keys in the Delphi Programming forum on Dev Shed. Enable arrow keys Delphi Programming forum discussing Delphi related topics including Kylix, C++ Builder, and more. Delphi is a high-performance language, originally based on the PASCAL language.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

March 20th, 2012, 10:46 AM
|
|
Registered User
|
|
Join Date: Mar 2012
Posts: 4
Time spent in forums: 2 h 39 sec
Reputation Power: 0
|
|
|
Enable arrow keys
I am working on a program that is basically finished, all but 2 tabstops are set to False so I can only tab between 2 TButtons. The issue that I have is when I press the space bar to operate 1 button the tabstops are set to 4 TRadioButtons that I want to tab between then pressing the space-bar again leaves to the next tabstop.
Someone sent me the following code to only allow the Tab and Space keys.
procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
// Only allow tab & space keys to be forwarded to BetweenButton
if self.ActiveControl = BetweenButton then
if not (Key in [VK_TAB, VK_SPACE]) then
Key := 0;
end;
The following code is supposed to allow the arrow keys to be enabled but I cant seem to get them working.
procedure TForm1.DialogKey(var Msg: TWMKey);
begin
// Make sure the OnKeyDown handler is called for arrow keys
case Msg.CharCode of
VK_DOWN,
VK_UP,
VK_RIGHT,
VK_LEFT : if Assigned(onKeyDown) then onKeyDown(Self, Msg.CharCode, KeyDataToShiftState(Msg.KeyData));
else
inherited
end;
end;
Can someone please help me solve my problems.
Many thanks
Steve 
|

March 21st, 2012, 06:59 AM
|
|
Contributing User
|
|
Join Date: Jan 2010
Posts: 68
Time spent in forums: 23 h 53 m 47 sec
Reputation Power: 4
|
|
Hi,
First I couldn't exaclly understand the explanation of your program. Can you try explain it better especially the part when you press the space bar. Otherwise about the code you have been posted I tried this.
Code:
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Button3: TButton;
Button4: TButton;
procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
private
{ Private declarations }
procedure DialogKey(var Msg: TWMKey); message CM_DIALOGKEY;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.DialogKey(var Msg: TWMKey);
begin
case Msg.CharCode of
VK_DOWN, VK_UP, VK_RIGHT, VK_LEFT:
if Assigned(onKeyDown) then
onKeyDown(Self, Msg.CharCode, KeyDataToShiftState(Msg.KeyData));
else
inherited
end;
end;
procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
case Key of
VK_DOWN: Top := Top + 5;
VK_UP: Top := Top - 5;
VK_LEFT: Left := Left - 5;
VK_RIGHT: Left := Left + 5;
end;
end;
end.
I found it here It works fine for me. So the DialogKey prccedure works fine. About your FormKeyDown procedure. With this
Code:
if not (Key in [VK_TAB, VK_SPACE]) then
Key := 0;
you accept only the tab or the spacebar key. So when you call this procedure from DialogKey with the pressing of the arrows you just skip them in the FormKeyDown procedure. Try to put them in the condition something like this
Code:
if not (Key in [VK_TAB, VK_SPACE, VK_DOWN, VK_UP, VK_RIGHT, VK_LEFT] ) then
Key := 0;
I think this sould be the way but again I didn't understand the hole idea of your program.
Dimiter.
Last edited by Dimixx : March 21st, 2012 at 07:02 AM.
|

March 22nd, 2012, 05:52 AM
|
|
Registered User
|
|
Join Date: Mar 2012
Posts: 4
Time spent in forums: 2 h 39 sec
Reputation Power: 0
|
|
|
Enable arrow keys
Thanks for your reply, I will try to explain better this time.
The form is as follows, the top of the form there are two TButtons, pressing the tab key allows one to move between the 2 and selected by pressing the spacebar. On pressing the spacebar the cursor drops below to 4 TRadioButtons that I need to select by using the tab key and executing by pressing the spacebar. From there the cursor drops again to a TDateTimePicker on the left allowing one to select a date from, tabbing to the TDateTimePicker on the right allowing one to select a date to. From there tabbing to a final TButton to calculate the date difference.
Everything is working as expected, but for the 4 TRadioButtons, the 2 top TButtons, the 2 TDateTimePickers and the Calculate TButton can all be moved to by using the tab key and selected by pressing the spacebar, but I cannot move between the 4 TRadioButtons. I hope this explains the form layout and what I am trying to accomplice.
As I am a new user I cannot put a link here, but if you are kind enough to email me then I would send you the link to show you an avi explaining in full.
kindest regards
Steve
|

March 22nd, 2012, 05:56 AM
|
|
Registered User
|
|
Join Date: Mar 2012
Posts: 4
Time spent in forums: 2 h 39 sec
Reputation Power: 0
|
|
|
Enable arrow keys
Sorry, just realised a mistake, it should be as follows;
The form is as follows, the top of the form there are two TButtons, pressing the tab key allows one to move between the 2 and selecting by pressing the spacebar. On pressing the spacebar the cursor drops below to 4 TRadioButtons that I need to select by using the arrow keys and once selected pressing the spacebar makes it jump to the next section. From there the cursor drops again to a TDateTimePicker on the left allowing one to select a date from, tabbing to the TDateTimePicker on the right allowing one to select a date to. From there tabbing to a final TButton to calculate the date difference.
Everything is working as expected, but for the 4 TRadioButtons, the 2 top TButtons, the 2 TDateTimePickers and the Calculate TButton can all be moved to by using the tab key and selected by pressing the spacebar, but I cannot move between the 4 TRadioButtons. I hope this explains the form layout and what I am trying to accomplice.
As I am a new user to this forum I cannot put a link here, but if you are kind enough to email me then I would send you the link to show you an avi explaining in full.
kindest regards
Steve
P.S. The reason for the way I want it to work is that I am designing it for a blind user.
|

March 22nd, 2012, 12:32 PM
|
|
Contributing User
|
|
Join Date: Jan 2010
Posts: 68
Time spent in forums: 23 h 53 m 47 sec
Reputation Power: 4
|
|
Hi,
First may be because you're new in the forum you don't know there is a Edit post button there is no need to repost
Now about the project now your idea is a lot clearer. So are you using 4 TRadioButtons? If so try with TRadioGroup. In the Items property you can add as many radiobuttons that you want. There you can navigate through the radiobuttons with the arrows without any code.
Dimiter
Last edited by Dimixx : March 22nd, 2012 at 01:37 PM.
|

March 22nd, 2012, 02:54 PM
|
|
Registered User
|
|
Join Date: Mar 2012
Posts: 4
Time spent in forums: 2 h 39 sec
Reputation Power: 0
|
|
|
Hi Dimiter, Thanks you for your reply, yes I now know I can edit posts, but I only realised after I resubmitted it, sorry.
I tried a TRadioGroup and still cannot move using the arrow keys, I suppose because I have the following code so only the Tab key and Spacebar is used, ie;
procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
// Only allow tab & space keys to be forwarded to BetweenButton
if self.ActiveControl = BetweenButton then
if not (Key in [VK_TAB, VK_SPACE] ) then
Key := 0;
end;
I would have to somehow make the arrow keys enabled again while only using the Radio Buttons
Regards
Steve
|

March 22nd, 2012, 04:09 PM
|
|
Contributing User
|
|
Join Date: Jan 2010
Posts: 68
Time spent in forums: 23 h 53 m 47 sec
Reputation Power: 4
|
|
Hi,
I think you're right. You will have to think some kind of logic about when you are in the radiogroup, so you will have to accept only the arrows and all other cases when you will accept only enter and space bar. Although I have a problem setting the focus on the radiogroup. If a switch it wirh Tab it's ok arrows work, but if I try to make it with code like this radiogroup.SetFocus you can't work with the arrows. So I came up with another idea don't switch the focus on the radiobuttons after pressing the first button with space bar just set some boolean varaible to true and check it in the DialogKey method then on the FormKeyDown make it something like that
Code:
case Key of
VK_DOWN: RadioGroup1.ItemIndex := RadioGroup1.ItemIndex - 1;
VK_UP: RadioGroup1.ItemIndex := RadioGroup1.ItemIndex + 1;
end;
Try both ways and see which you find better
Dimiter
|

March 22nd, 2012, 04:59 PM
|
|
Contributing User
|
|
Join Date: Jan 2006
Location: Carlsbad, CA
|
|
|
I seem to be missing something.
What is BetweenButton?
The code already restricts the special action to when this object has focus,
so I am not sure why it affects the TRadioGroup or any other components.
What happens if you remove the DialogKey code?
|

May 30th, 2012, 10:12 AM
|
|
Registered User
|
|
Join Date: May 2012
Posts: 3
Time spent in forums: 30 m 53 sec
Reputation Power: 0
|
|
|
Delphi Programming forum discussing Delphi related topics including Kylix
Delphi Programming forum discussing Delphi related topics including Kylix very nice frnd thanks 
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|