size : 1958
uploaded_on : Tue Oct 20 00:00:00 1998
modified_on : Wed Dec 8 14:03:21 1999
title : Simulating button down
org_filename : keysimul.txt
author : James D. Rofkar
authoremail : jim_rofkar%lotusnotes1@instinet.com
description : Simulating down position of button while key pressed
keywords :
tested : not tested yet
submitted_by : Unofficial Delphi Developers FAQ
submitted_by_email : uddf@gnomehome.demon.nl
uploaded_by : nobody
modified_by : nobody
owner : nobody
lang : plain
file-type : text/plain
category : delphi-commoncontrols
__END_OF_HEADER__
Simulating ButtonDown
From: "James D. Rofkar"
Paulo Oliveira wrote: > > I have a set of buttons,(caption ='0'..'9') and I'd like to simulate the down position of the
button, when the user presses the corresponding key. I.e. if user presses key '1' the button goes down on screen. How can I
do this, without a new Tbutton component?
No problem Paulo:
You'll probably want to be using 10 TSpeedButton controls, or an array of them, since this button provides a "Down" property. Anyhow, set the "KeyPreview"
property of your main form to "True". Then, in your "OnKeyDown" event handler, write something like this...
case Key of
VK_NUMPAD0: btn0.Down := True;
VK_NUMPAD1: btn1.Down := True;
VK_NUMPAD2: btn2.Down := True;
VK_NUMPAD3: btn3.Down := True;
VK_NUMPAD4: btn4.Down := True;
VK_NUMPAD5: btn5.Down := True;
VK_NUMPAD6: btn6.Down := True;
VK_NUMPAD7: btn7.Down := True;
VK_NUMPAD8: btn8.Down := True;
VK_NUMPAD9: btn9.Down := True;
end;
And, in your "OnKeyUp" event handler, write something like...
case Key of
VK_NUMPAD0: btn0.Down := False;
VK_NUMPAD1: btn1.Down := False;
VK_NUMPAD2: btn2.Down := False;
VK_NUMPAD3: btn3.Down := False;
VK_NUMPAD4: btn4.Down := False;
VK_NUMPAD5: btn5.Down := False;
VK_NUMPAD6: btn6.Down := False;
VK_NUMPAD7: btn7.Down := False;
VK_NUMPAD8: btn8.Down := False;
VK_NUMPAD9: btn9.Down := False;
end;
You'll want to experiment with the "AllowAllUp" property and the "GroupIndex" property to get the button response/effect you like.
Again, an array of TSpeedButtons would be the most elegant solution to this problem, since you could use the VK_ constant as the index, and make both event
handlers a one line call to Button[VK_x].Down := True {or False}.