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

i would assume each button will be set up individually

if so then name it button, and use onclick to display value

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

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

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

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];
...

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

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?

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

regards
mrmike

thanks - the keypad is working great

S Bedford

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.