Recent

Author Topic: ScrollBar and arrow keys  (Read 8582 times)

vgoudreault

  • New Member
  • *
  • Posts: 11
ScrollBar and arrow keys
« on: February 01, 2011, 04:21:22 am »
I have a mild issue with the way the ScrollBar widget works.
I have an application that uses the ScrollBar in the horizontal layout, with the minimum value at the left and the maximum at the right. Evidently, clicking on the little arrows at either ends of the bar will cause the bar to scroll, just as it would if someone was to drag the bar using the mouse.
The issue is when a user insists on using the keyboard for everything, and relies on the keyboard directional control arrow to change the ScrollBar value. Using the left and right arrow works as it should, but using the "up" arrow performs the same function as using the "left" (and 'down' emulated 'right') which is counter-intuitive as far as going up is associated with reaching a higher value (although the definition of the window in Lazarus is that the Y value goes up going down, but bear with me; this program is an engineering tool, and engineers have the Y going up positively).

Does anyone have any idea how one could potentially trap the up/down arrow key press while the ScrollBar has focus, which would allow a procedure to invert their behavior?

In more general terms, is there a widget that would identify which key is being pressed, and return some code value?


vgoudreault

  • New Member
  • *
  • Posts: 11
Re: ScrollBar and arrow keys
« Reply #1 on: February 01, 2011, 07:11:04 am »
It may appear strange to reply to one's own post, but since I figured out a way out of my predicament, and that perhaps everyone is asleep or off-line, and to avoid unnecessary effort to post a solution, I can post a go around I just figured out. Perhaps that will help someone in the future.

Basically, the issue is that the up arrow is associated with dropping the value as it is interpreted as a left arrow key press. So the solution is to intercept the up and down arrow with a 'KeyDown' event, and apply DOUBLE the normal scroll rate. The effect is that the pressing of the up key cause the value to go down as it normally does, but the trapping of the key, using the VK_UP value, adds its own correction in the other direction, enough to cancel the unwanted down value and add the desired change.

Crude, but effective.

Code: [Select]
procedure TForm1.ScrollBar1KeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
 Case Key of
    VK_UP : ScrollBar1.Position:=ScrollBar1.Position+2;
    VK_DOWN : ScrollBar1.Position:=ScrollBar1.Position-2;
  end;
end;



Now, if anyone has an *elegant* solution that does not involve the unneeded sacrifice of CPU cycle time to have something have to come up and mop up the mess (by having it do what is desired directly) that would be an improvement.

Many thanks


CBVG

vgoudreault

  • New Member
  • *
  • Posts: 11
Re: ScrollBar and arrow keys
« Reply #2 on: February 02, 2011, 03:17:48 am »
A final word of caution, for whoever would be using this technique: at the limits, the use of the UP and DOWN key will cause the ScrollBar to stall at one "SmallChange" short of the actual limit, as the imposition of the double scroll value causes the position of the bar to be bounded by the Min/Max, just before the actual interpretation of the Up or Down key will cause it to back out away from it.
The work-around in this case is to buffer the target value of the bar (where it should be, including the limits) in a global variable, and to impose said bar position with a "OnKeyUp" event procedure, using the global variable value. This, however, will cause the position of the bar to briefly flash as being one SmallChange value from the limit before it is restored to the actual limit.

Since this is rather clunky -- despite being robust -- approach, I would appreciate anything that would be more elegant.


CBVG

Blaazen

  • Hero Member
  • *****
  • Posts: 3241
  • POKE 54296,15
    • Eye-Candy Controls
Re: ScrollBar and arrow keys
« Reply #3 on: February 02, 2011, 03:46:33 am »
I cannot test it with Qt4.7.1 widgetset. With Horizontal Scrollbar - left arrow=down arrow and up arrow is the same like right arrow. But here scrollbar does not generate any OnKeyDown event. Maybe bug?
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

 

TinyPortal © 2005-2018