Recent

Author Topic: (Solved)TUpDown KeyDown event  (Read 520 times)

morknot

  • Jr. Member
  • **
  • Posts: 57
  • still learning
(Solved)TUpDown KeyDown event
« on: April 14, 2026, 05:08:27 pm »
I would like to vary the Increment value, say from 1 to 5, of an UpDown component at runtime by pressing the Shift key. Unfortunately the TUpDown component does not have an OnKeyDown event.

 In the past (to my surprise) I managed to add an event to a component created at runtime which didn't have an OnMouseWheel event by including:

type
  progbar=Class(TJvSpecialProgress)
 published
 property OnMouseWheel;
 end;

but the equivalent doesn't work for TUpDown, also created at runtime.

Is there a similar and simple method for TUpDown or do I have to try and create a new component.

Thanks.






 
« Last Edit: April 15, 2026, 12:10:04 pm by morknot »

LeP

  • Sr. Member
  • ****
  • Posts: 294
Re: TUpDown KeyDown event
« Reply #1 on: April 14, 2026, 06:01:57 pm »
You can capture OnKeyDown event on control associate to UpDown component (Edit1 in example) and:

[EDIT]: changed var inline ...  %)

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Edit1KeyDown(Sender: TObject; var Key: Word;
  2.   Shift: TShiftState);
  3. var p: TComponent;
  4. begin
  5.   if (ssCTRL in Shift) and (key = vk_F1) then   //<--- This will activated if "CTRL + F1" is pressed, change this whatever you want
  6.     for p in self.GetControls([ceftAll]) do
  7.       begin
  8.         if p is TUpDown then
  9.           if (P as TUpDown).Associate = Sender then
  10.             begin
  11.               (P as TUpDown).Increment := 5;
  12.               ShowMessage('Done');
  13.             end;
  14.       end;
  15. end;
  16.  

I take it from my old program code (in Delphi) ... should be tested in Lazarus.
« Last Edit: April 14, 2026, 06:11:24 pm by LeP »
Un Sistema per domarli, un IDE per trovarli, un codice per ghermirli e nel framework incatenarli.
An operating system to tame them, an IDE to find them, a code to catch them and in the framework chain them.

morknot

  • Jr. Member
  • **
  • Posts: 57
  • still learning
Re: (Solved)TUpDown KeyDown event
« Reply #2 on: April 15, 2026, 12:18:52 pm »
Thank you for your reply.

I have experimented a bit more and found that with the mouse cursor over the UpDown component and with it highlighted, it does not automatically seem to get Focus. By using SetFocus in the MouseEnter event..

type
  myupdown=Class(TUpDown)
    published
    property OnKeyDown;
    property OnKeyUp;
   end;     

then OnkeyDown and OnKeyUp events now fire.

LeP

  • Sr. Member
  • ****
  • Posts: 294
Re: (Solved)TUpDown KeyDown event
« Reply #3 on: April 15, 2026, 11:21:09 pm »
Locking focus on a component during mouse movement (which can then move to other components during the movement) isn't a positive experience, I think, and it can lead to annoying side effects: the user is actually acting on a control they didn't expect because it changed without their knowledge.
Then, obviously, your changes will only work if the component is instantiated at runtime; otherwise, you should build a new component and install it in the Lazarus IDE.
Un Sistema per domarli, un IDE per trovarli, un codice per ghermirli e nel framework incatenarli.
An operating system to tame them, an IDE to find them, a code to catch them and in the framework chain them.

 

TinyPortal © 2005-2018