The task is to change the values of a an element (a variable or a visual control, like for example SpinEdit) while a button is pressed. One of the buttons shall increase the value and the other one should decrease it.
So for an example I created a TSpinEdit, two buttons (ButtonUp and ButtonDown) and a Timer1, disabled by default, and I declared a SpinInc: Integer;
First obstacle is that I could not find how to detect if the button is pressed.
I did :
procedure TForm1.ButtonUpClick(Sender: TObject);
begin
SpinEdit1.Value:=SpinEdit1.Value+1;
end;
procedure TForm1.ButtonUpMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
SpinInc:=1;
Timer1.Enabled:=True;
end;
procedure TForm1.ButtonUpMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
Timer1.Enabled:=False;
end;
procedure TForm1.ButtonDownClick(Sender: TObject);
begin
SpinEdit1.Value:=SpinEdit1.Value-1;
end;
procedure TForm1.ButtonDownMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
SpinInc:=-1;
Timer1.Enabled:=True;
end;
procedure TForm1.ButtonDownMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
Timer1.Enabled:=False;
end;
procedure TForm1.Timer1Timer(Sender: TObject);
begin
SpinEdit1.Value:=SpinEdit1.Value + SpinInc;
end;
It sort of works, with the exception when the button is clicked by the keyboard (TAB to it and press spacebar).
In order to do it, I have to use also OnKeyDown and OnKeyUp, checking if the key is Spacebar.
Maybe there is a simpler solution?
I tried BitButton and SpeedButton, but they have no pressed property neither.