Emulating a keyboard key press

Reply

Join Date: Jul 2005
Posts: 18
Reputation: sbedford is an unknown quantity at this point 
Solved Threads: 0
sbedford sbedford is offline Offline
Newbie Poster

Emulating a keyboard key press

 
0
  #1
Aug 24th, 2005
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
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 41
Reputation: mrmike is an unknown quantity at this point 
Solved Threads: 0
mrmike mrmike is offline Offline
Light Poster

Re: Emulating a keyboard key press

 
0
  #2
Sep 7th, 2005
i would assume each button will be set up individually

if so then name it button, and use onclick to display value
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 18
Reputation: sbedford is an unknown quantity at this point 
Solved Threads: 0
sbedford sbedford is offline Offline
Newbie Poster

Re: Emulating a keyboard key press

 
0
  #3
Sep 13th, 2005
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
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 41
Reputation: mrmike is an unknown quantity at this point 
Solved Threads: 0
mrmike mrmike is offline Offline
Light Poster

Re: Emulating a keyboard key press

 
0
  #4
Sep 14th, 2005
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
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 18
Reputation: sbedford is an unknown quantity at this point 
Solved Threads: 0
sbedford sbedford is offline Offline
Newbie Poster

Re: Emulating a keyboard key press

 
0
  #5
Sep 14th, 2005
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 6
Reputation: neverwinter is an unknown quantity at this point 
Solved Threads: 0
neverwinter neverwinter is offline Offline
Newbie Poster

Re: Emulating a keyboard key press

 
0
  #6
Sep 15th, 2005
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];
...
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 41
Reputation: mrmike is an unknown quantity at this point 
Solved Threads: 0
mrmike mrmike is offline Offline
Light Poster

Re: Emulating a keyboard key press

 
0
  #7
Sep 15th, 2005
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
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 18
Reputation: sbedford is an unknown quantity at this point 
Solved Threads: 0
sbedford sbedford is offline Offline
Newbie Poster

Re: Emulating a keyboard key press

 
0
  #8
Sep 15th, 2005
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?
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 41
Reputation: mrmike is an unknown quantity at this point 
Solved Threads: 0
mrmike mrmike is offline Offline
Light Poster

Re: Emulating a keyboard key press

 
0
  #9
Sep 15th, 2005
set the OnEnter event for each editbox to point to the EditClick

regards
mrmike
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 18
Reputation: sbedford is an unknown quantity at this point 
Solved Threads: 0
sbedford sbedford is offline Offline
Newbie Poster

Re: Emulating a keyboard key press

 
0
  #10
Sep 15th, 2005
thanks - the keypad is working great

S Bedford
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the Pascal and Delphi Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC