Board index » delphi » Simulating Button Clicks from keyboard

Simulating Button Clicks from keyboard

Hi,

Part of my application looks like a numeric keypad, where TButton objects
represent numbers 0 to 9.  I want the user to be able to enter numbers by
pressing the appropriate number keys on their keyboard.  Its easy to detect
which key they've pressed using the OnKeyDown events but I want  each
corresponding button to appear to 'click' as the user enters a number.
I've tried calling the 'Click' method for each button but this doens't
work.

Any ideas?

--
Andrew Jackson
ajack...@cccgroup.co.uk

 

Re:Simulating Button Clicks from keyboard


Andrew,
  Try using the forms OnKeyPress event and set the forms KeyPreview to
True.  Then use some code like this in the OnKeyPressEvent:

procedure TForm1.FormKeyPress(Sender: TObject; var Key: Char);
begin
  case Key of
  '1' :begin
         SendMessage(Button1.Handle, wm_LButtonDown,0,0);
         Sleep(500);
         SendMessage(Button1.Handle, wm_LButtonUp, 0,0);
       end;
  '2' :begin
         SendMessage(Button2.Handle, wm_LButtonDown,0,0);
         Sleep(500);
         SendMessage(Button2.Handle, wm_LButtonUp, 0,0);
       end;
  end;
end;

Note that this exact code won't work in D1 due to the lack of the Sleep()
function but you could make a work around with a TTimer or using the
GetTickCount API.

Hope this helps!
--

Rodney E Geraghty
GERA-Tech
Ottawa, Canada
gera...@ibm.net

Andrew Jackson <ajack...@cccgroup.co.uk> wrote in article
<01bc8e0d$2cd46f90$f60170c1@jalfrezi>...

Quote
> Hi,

> Part of my application looks like a numeric keypad, where TButton objects
> represent numbers 0 to 9.  I want the user to be able to enter numbers by
> pressing the appropriate number keys on their keyboard.  Its easy to
detect
> which key they've pressed using the OnKeyDown events but I want  each
> corresponding button to appear to 'click' as the user enters a number.
> I've tried calling the 'Click' method for each button but this doens't
> work.

> Any ideas?

> --
> Andrew Jackson
> ajack...@cccgroup.co.uk

Re:Simulating Button Clicks from keyboard


Quote
Andrew Jackson wrote:
> Part of my application looks like a numeric keypad, where TButton objects
> represent numbers 0 to 9.  I want the user to be able to enter numbers by
> pressing the appropriate number keys on their keyboard.  Its easy to detect
> which key they've pressed using the OnKeyDown events but I want  each
> corresponding button to appear to 'click' as the user enters a number.
> I've tried calling the 'Click' method for each button but this doens't
> work.

You can send a WM_LBUTTONDOWN message followed by a timed WM_LBUTTONUP
message to the button control. However, this may be somewhat risky since
the button will capture the mouse for the time it is "down". Also, the
button control may assume that it receives a WM_MOUSEMOVE message prior
to a WM_xBUTTONDOWN message. Therefore, use this approach with care.

An easier way of achieving your goal is using the TSpeedButton
component. That one has a Down property that can be set
programmatically. Unfortunately, you cannot set focus to a TSpeedButton.

--
Andre M. Naef

Remove the "delete.this" part from the address when replying by email.

Other Threads