RSS Forums RSS
Please support our Pascal and Delphi advertiser: Programming Forums
Views: 11780 | Replies: 9 | Thread Tools  Display Modes
Reply
Join Date: Jul 2005
Posts: 18
Reputation: sbedford is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
sbedford sbedford is offline Offline
Newbie Poster

Emulating a keyboard key press

  #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
AddThis Social Bookmark Button
Reply With Quote  
Join Date: May 2005
Posts: 41
Reputation: mrmike is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
mrmike mrmike is offline Offline
Light Poster

Re: Emulating a keyboard key press

  #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  
Join Date: Jul 2005
Posts: 18
Reputation: sbedford is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
sbedford sbedford is offline Offline
Newbie Poster

Re: Emulating a keyboard key press

  #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  
Join Date: May 2005
Posts: 41
Reputation: mrmike is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
mrmike mrmike is offline Offline
Light Poster

Re: Emulating a keyboard key press

  #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

procedure TForm1.ButtonPressed(Sender: TObject);
var
index   :Integer;
begin
for index:=0 to ControlCount -1 do
begin
 if Controls[index] is TWinControl then
  if (Controls[index] as TWinControl).Focused then
   Edit1.Text:=Edit1.Text+Char(((Controls[index] as TWinControl).Tag)
end;
end;
and then set the OnClick event for each button to go to ButtonPressed

hope this helps
mrmike
Reply With Quote  
Join Date: Jul 2005
Posts: 18
Reputation: sbedford is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
sbedford sbedford is offline Offline
Newbie Poster

Re: Emulating a keyboard key press

  #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  
Join Date: Aug 2005
Posts: 6
Reputation: neverwinter is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
neverwinter neverwinter is offline Offline
Newbie Poster

Re: Emulating a keyboard key press

  #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  
Join Date: May 2005
Posts: 41
Reputation: mrmike is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
mrmike mrmike is offline Offline
Light Poster

Re: Emulating a keyboard key press

  #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.

procedure ButtonPressed(Sender: TObject);
var
index   :Integer;
result  :Integer;
begin
result:=0;
for index:=0 to ControlCount -1 do
 begin
 if (Controls[index] is TButton) and TButton(Controls[index]).Focused then
 result:=index;
end;
    Label2.Caption:='Button Active '+Char(TButton(Controls[result]).Tag);
    TEdit(Controls[cureditbox]).Text:=TEdit(Controls[cureditbox]).Text+Char(TButton(Controls[result]).Tag);
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.

procedure EditClick(Sender: TObject);
var
 loop :integer;
begin
 cureditbox:=1;
for loop:=0 to ControlCount -1 do
begin
 if (Controls[loop] is TEdit) and TEdit(Controls[loop]).Focused then
  cureditbox:=loop;
end;
Label1.Caption:='Edit Box Active '+Char(TEdit(Controls[cureditbox]).Tag+48);
end;

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

Re: Emulating a keyboard key press

  #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

   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  
Join Date: May 2005
Posts: 41
Reputation: mrmike is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
mrmike mrmike is offline Offline
Light Poster

Re: Emulating a keyboard key press

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

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

Re: Emulating a keyboard key press

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

S Bedford
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.



Other Threads in the Pascal and Delphi Forum
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 4:41 am.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC