943,648 Members | Top Members by Rank

Ad:
Aug 24th, 2005
0

Emulating a keyboard key press

Expand Post »
Just wondering if it is possible to do this? I am writing a program for a touchscreen monitor and would like an onscreen keypad. Is there any way to pass a key by pressing a button? For example, I press button '1' on the virtual keypad and I want the number 1 to appear in whatever editbox etc is selected at the time.

Thanks

Sean Bedford
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sbedford is offline Offline
18 posts
since Jul 2005
Sep 7th, 2005
0

Re: Emulating a keyboard key press

i would assume each button will be set up individually

if so then name it button, and use onclick to display value
Reputation Points: 11
Solved Threads: 0
Light Poster
mrmike is offline Offline
41 posts
since May 2005
Sep 13th, 2005
0

Re: Emulating a keyboard key press

Yes, each button is setup individually - but the problem lies in how do I get it to output the value? I've tried writeln, which won't work
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sbedford is offline Offline
18 posts
since Jul 2005
Sep 14th, 2005
0

Re: Emulating a keyboard key press

if using an edit box then don't need to use writeln

for each button give it a tag number corresponding to the ascii value you want it to display (ie 0 = 48, 1 = 49, 2 = 50, etc)

create the following procedure

Pascal and Delphi Syntax (Toggle Plain Text)
  1. procedure TForm1.ButtonPressed(Sender: TObject);
  2. var
  3. index :Integer;
  4. begin
  5. for index:=0 to ControlCount -1 do
  6. begin
  7. if Controls[index] is TWinControl then
  8. if (Controls[index] as TWinControl).Focused then
  9. Edit1.Text:=Edit1.Text+Char(((Controls[index] as TWinControl).Tag)
  10. end;
  11. end;
and then set the OnClick event for each button to go to ButtonPressed

hope this helps
mrmike
Reputation Points: 11
Solved Threads: 0
Light Poster
mrmike is offline Offline
41 posts
since May 2005
Sep 14th, 2005
0

Re: Emulating a keyboard key press

Yeah, I was thinking something along the lines of concatonating a char, but is there any way for me to do this to whatever edit box is selected - just a way to tell which edit box is in focus will do

Thanks

S Bedford
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sbedford is offline Offline
18 posts
since Jul 2005
Sep 15th, 2005
0

Re: Emulating a keyboard key press

Try checking the Application object to see if it has an available property/method to return the control that has focus...

You can also revise the sample code (above) to check for TCustomEdit (instead of TWinControl), i.e.

...
if (Controls[index] is TCustomEdit) and TCustomEdit(Controls[index]).Focused then
result := Controls[index];
...
Reputation Points: 10
Solved Threads: 0
Newbie Poster
neverwinter is offline Offline
6 posts
since Aug 2005
Sep 15th, 2005
0

Re: Emulating a keyboard key press

The labels are not required, but were used for testing, as you change from the editbox to the button, the editbox will lose focus, and the button will become focused, thus giving you an issue if using more than one edit box.

The procedure ButtonPressed uses the TAG option to assign the ascii codes for each button pressed to be displayed in the correct edit box, which is obtained from the EditClick procedure. Remember to set the OnClick event for each of the corresponding buttons.

Pascal and Delphi Syntax (Toggle Plain Text)
  1. procedure ButtonPressed(Sender: TObject);
  2. var
  3. index :Integer;
  4. result :Integer;
  5. begin
  6. result:=0;
  7. for index:=0 to ControlCount -1 do
  8. begin
  9. if (Controls[index] is TButton) and TButton(Controls[index]).Focused then
  10. result:=index;
  11. end;
  12. Label2.Caption:='Button Active '+Char(TButton(Controls[result]).Tag);
  13. TEdit(Controls[cureditbox]).Text:=TEdit(Controls[cureditbox]).Text+Char(TButton(Controls[result]).Tag);
  14. end;

As the focus will change from the editbox selected to the button being pressed, the following procedure will handle which box will be updated when pressing button. You will need to set the OnClick event for each edit box to point to this procedure.

Pascal and Delphi Syntax (Toggle Plain Text)
  1. procedure EditClick(Sender: TObject);
  2. var
  3. loop :integer;
  4. begin
  5. cureditbox:=1;
  6. for loop:=0 to ControlCount -1 do
  7. begin
  8. if (Controls[loop] is TEdit) and TEdit(Controls[loop]).Focused then
  9. cureditbox:=loop;
  10. end;
  11. Label1.Caption:='Edit Box Active '+Char(TEdit(Controls[cureditbox]).Tag+48);
  12. end;

regards
mrmike
Reputation Points: 11
Solved Threads: 0
Light Poster
mrmike is offline Offline
41 posts
since May 2005
Sep 15th, 2005
0

Re: Emulating a keyboard key press

Right, what ive done is create a procedure which sets a var of type TEdit to the correct box, then upon clicking say, key number 1, it does

Pascal and Delphi Syntax (Toggle Plain Text)
  1. focusededitbox.Text:=focusededitbox.Text+inttostr(1)

This works fine as long as the box is clicked before you try to use the keypad.

Now, the only problem I have is if users do tab to move between boxes - is there any way to make a focus on an edit box force it to do the onclick event?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sbedford is offline Offline
18 posts
since Jul 2005
Sep 15th, 2005
0

Re: Emulating a keyboard key press

set the OnEnter event for each editbox to point to the EditClick

regards
mrmike
Reputation Points: 11
Solved Threads: 0
Light Poster
mrmike is offline Offline
41 posts
since May 2005
Sep 15th, 2005
0

Re: Emulating a keyboard key press

thanks - the keypad is working great

S Bedford
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sbedford is offline Offline
18 posts
since Jul 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Pascal and Delphi Forum Timeline: Creating a program to run under various OS
Next Thread in Pascal and Delphi Forum Timeline: report in quickreport





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC