* * *

Author Topic: Disabling keyboard focus for a control (TButton especially)?  (Read 2426 times)

dahaiou

  • New member
  • *
  • Posts: 40
Is it possible to disable focus for a control (TButton especially)?

I have already set Tabstop := False for all these buttons,
and it works in the sense that they are passed over when
the Tab-key is pressed.

But when I use the arrow-keys, it seems these buttons still
receive focus, and the button faces are annoyingly focus-
highlighted one after the other as I keep hitting an arrow key.

I am using the arrows for specific things in the program and
have them intercepted by the main form. This is working as
I want, but it's unrelated to the buttons on the form, and
I don't want to see them flickering as I press the arrows.

There is a TabOrder property that holds an Integer value.
I was trying to take the buttons out of the TabOrder
sequence entirely by setting this to an empty string
but the IDE won't allow it. I might be missing something
vary basic here (?).

Note: There is no problem with the functionality of the
buttons as such. I am intercepting the enter-key as well
and a button press is never generated from the keyboard.
It's just the annnoying highlighting I need to get rid of.

How can I prevent it?

PS. In case it matters, I am using Lazarus 0.9.30.4/32bit under Win7-64.
« Last Edit: July 27, 2012, 02:08:25 pm by dahaiou »

dahaiou

  • New member
  • *
  • Posts: 40
Re: Disabling keyboard focus for a control (TButton especially)?
« Reply #1 on: July 27, 2012, 02:38:56 pm »
An afterthought: If focus cannot be disabled, just disabling the hightlighting would do too.

Is this a tough one perhaps, since no-one has come up with anything?
Any ideas?

Zoran

  • Hero Member
  • *****
  • Posts: 1144
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: Disabling keyboard focus for a control (TButton especially)?
« Reply #2 on: July 27, 2012, 03:00:08 pm »
I don't know if it can be done.
Anyway, the workaround for you is using TSpeedButton instead of TButton controls. TSpeedButton does not descend from TWinControl, so it never receives focus.

dahaiou

  • New member
  • *
  • Posts: 40
Re: Disabling keyboard focus for a control (TButton especially)?
« Reply #3 on: July 27, 2012, 04:31:14 pm »
Thanks, I changed them all to TSpeedButton and it seems to do the trick.
No more highlighting.

Only thing that happens now, when I use the Up/Down arrow keys, is that once in a while all these buttons flicker briefly, like they are redrawn for no reason.

I guess this might be an effect of the parent TGroupBox receiving focus. This is a smaller nuisance than the highlighting though. And it's only happening with the Up/Down arrow keys, not Left/Right. Strange.

CM630

  • Hero Member
  • *****
  • Posts: 688
  • Не съм сигурен, че те разбирам.
    • http://sourceforge.net/u/cm630/profile/
Re: Disabling keyboard focus for a control (TButton especially)?
« Reply #4 on: March 16, 2017, 11:03:27 am »

A few years later- maybe there is already asolution for that?
I want to fully disable changing focus with the arrows on the form components.
Maybe I could put something in the OnKeyDown event of the form (I have keypreview enabled).
Lazarus 1,6,4;XP 32bit or W7 64bit ;FPC3,0,2;rev 54278

Zoran

  • Hero Member
  • *****
  • Posts: 1144
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: Disabling keyboard focus for a control (TButton especially)?
« Reply #5 on: March 16, 2017, 12:03:32 pm »

A few years later- maybe there is already asolution for that?
I want to fully disable changing focus with the arrows on the form components.
Maybe I could put something in the OnKeyDown event of the form (I have keypreview enabled).

To disable changing focus only with arrows set Form1.KeyPreview to True and Form1.OnKeyDown to this:
Code: Pascal  [Select]
  1. procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState
  2.   );
  3. begin
  4.   if Key in [VK_LEFT, VK_RIGHT, VK_UP, VK_DOWN] then
  5.     Key := 0;
  6. end;
  7.  

The focus will still change with Tab. If you want to disable tab as well, add VK_TAB in the set.
« Last Edit: March 16, 2017, 12:08:45 pm by Zoran »

CM630

  • Hero Member
  • *****
  • Posts: 688
  • Не съм сигурен, че те разбирам.
    • http://sourceforge.net/u/cm630/profile/
Re: Disabling keyboard focus for a control (TButton especially)?
« Reply #6 on: March 16, 2017, 02:11:55 pm »
Thanks, it works!
I only changed it to if (Key >36) and (key <41) then  Key := 0; because VK_ requred uses  ... windows... which causes some problems.
Lazarus 1,6,4;XP 32bit or W7 64bit ;FPC3,0,2;rev 54278

Zoran

  • Hero Member
  • *****
  • Posts: 1144
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: Disabling keyboard focus for a control (TButton especially)?
« Reply #7 on: March 16, 2017, 09:21:01 pm »
Thanks, it works!
I only changed it to if (Key >36) and (key <41) then  Key := 0; because VK_ requred uses  ... windows... which causes some problems.
No, VK constants can be found in LCLType unit.
There is even a FAQ about it :) http://wiki.lazarus.freepascal.org/Lazarus_Faq#Virtual_key_constants

Thaddy

  • Hero Member
  • *****
  • Posts: 3636
Re: Disabling keyboard focus for a control (TButton especially)?
« Reply #8 on: March 16, 2017, 09:30:44 pm »
Correct. I often wonder why people never read documentation. >:D

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus