Recent

Author Topic: Keyboard hook - platform independent  (Read 839 times)

backprop

  • Full Member
  • ***
  • Posts: 192
Keyboard hook - platform independent
« on: February 09, 2026, 10:21:20 am »
I need to detect key pressing inside Lazarus made application, which is independent for focused component. It is fairly easy to do that with Windows API calls, but I'm not certain for Linux.

For now I using timers and check for particularly pressed keys with GetKeyState(), but that is not suitable enough, as it depend on timer interval, however, application need to react instantly.

jamie

  • Hero Member
  • *****
  • Posts: 7586
Re: Keyboard hook - platform independent
« Reply #1 on: February 09, 2026, 11:39:19 am »
Application.onuserinput ?
The only true wisdom is knowing you know nothing

cdbc

  • Hero Member
  • *****
  • Posts: 2662
    • http://www.cdbc.dk
Re: Keyboard hook - platform independent
« Reply #2 on: February 09, 2026, 11:41:17 am »
Hi
Have you tried setting 'KeyPreview' to true for the form itself and then create an 'OnKeyDown' event-handler for the form, where you can catch the key-presses before they drip further down the hierarchy ?!?
I use that on Linux + Qt6  :D
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE6/QT6 -> FPC Release -> Lazarus Release &  FPC Main -> Lazarus Main

n7800

  • Hero Member
  • *****
  • Posts: 639
  • Lazarus IDE contributor
    • GitLab profile
Re: Keyboard hook - platform independent
« Reply #3 on: February 09, 2026, 01:43:46 pm »
Yes, KeyPreview and OnKeyDown in a form are the primary way to create "hotkeys" for a form.

There are also more general events if you need them (unlikely):
* Application.AddOnKeyDownHandler
* Application.AddOnKeyDownBeforeHandler

There's also a wiki article with a detailed description of keyboard event handling, but I'd say it's not for beginners ))

n7800

  • Hero Member
  • *****
  • Posts: 639
  • Lazarus IDE contributor
    • GitLab profile
Re: Keyboard hook - platform independent
« Reply #4 on: February 09, 2026, 01:49:51 pm »
And there is a TActionList component in the Component Palette, which is also a common use case. It also has a number of other advantages. It's a shame the documentation and wiki don't cover them in detail right now, I hope someone will fix that.

backprop

  • Full Member
  • ***
  • Posts: 192
Re: Keyboard hook - platform independent
« Reply #5 on: February 26, 2026, 07:46:41 pm »
Have you tried setting 'KeyPreview' to true for the form itself and then create an 'OnKeyDown' event-handler for the form, where you can catch the key-presses before they drip further down the hierarchy ?!?

I did exactly that, it works, but for the purpose, that is a bit sluggish. When I used timer approach with quite short interval, that was much more suitable. It is simply not clear in which interval OnKeyDown event is firing.
« Last Edit: February 26, 2026, 08:21:28 pm by backprop »


n7800

  • Hero Member
  • *****
  • Posts: 639
  • Lazarus IDE contributor
    • GitLab profile
Re: Keyboard hook - platform independent
« Reply #7 on: February 26, 2026, 11:27:19 pm »
https://forum.lazarus.freepascal.org/index.php?topic=70470.0
https://forum.lazarus.freepascal.org/index.php?topic=52496.0

These topics discuss "global" OS-level keyboard shortcuts that an app can intercept even when minimized (and even before the keypress is received by another app that's currently open).

As far as I understand, the topic author is interested in receiving keypresses only within their app's window (but regardless of the focused component)?

n7800

  • Hero Member
  • *****
  • Posts: 639
  • Lazarus IDE contributor
    • GitLab profile
Re: Keyboard hook - platform independent
« Reply #8 on: February 26, 2026, 11:36:40 pm »
Have you tried setting 'KeyPreview' to true for the form itself and then create an 'OnKeyDown' event-handler for the form, where you can catch the key-presses before they drip further down the hierarchy ?!?

I did exactly that, it works, but for the purpose, that is a bit sluggish. When I used timer approach with quite short interval, that was much more suitable. It is simply not clear in which interval OnKeyDown event is firing.

It's strange, the OnKeyDown + KeyPreview approach is considered standard and is used, among other things, in the Lazarus IDE itself...

But in fact, it's possible, since GetKeyState calls the WinAPI directly, while the LCL performs complex, multi-layered processing. However, I personally never noticed a speed difference on my system.

 

TinyPortal © 2005-2018