Recent

Author Topic: User define global shortcut from background application (Linux)  (Read 695 times)

backprop

  • Jr. Member
  • **
  • Posts: 90
SimpleScreeenRecorder have ability to define shortcut and start recording while it is in the background. It do not warn user to enter admin pass, but still is able in user mode to define and start recording on pressing that shortcut.

Is it that possible with Lazarus? IIRC, here is someone wrote that on linux is not possible to hook any keyboard strokes from backgrond app, but apparently it is.

dbannon

  • Hero Member
  • *****
  • Posts: 3026
    • tomboy-ng, a rewrite of the classic Tomboy
Re: User define global shortcut from background application (Linux)
« Reply #1 on: August 12, 2024, 04:47:43 am »
Hmm, not sure I am answering the question you have asked.

You want to use a keyboard short cut (defined at a OS/Desktop level) and have that passed to a background task ?

Normally, a keystroke (of any sort) is not passed to a background task, so, may, initially seem difficult.

My approach would be to define the keystroke to start the application. Then, use something like IPC to check if there is an existing instance is running and if it is, send a message to it and exit. In your case the message is "user pressed the magic keys".

IPC, https://wiki.freepascal.org/SimpleIPC allows one process to talk to another. I use it in my app to primarily to ensure only one instance is running in pretty much the behaviour you seek IMHO.

Davo

edit: typo, exit v exist
« Last Edit: August 12, 2024, 08:59:13 am by dbannon »
Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

MarkMLl

  • Hero Member
  • *****
  • Posts: 7527
Re: User define global shortcut from background application (Linux)
« Reply #2 on: August 12, 2024, 08:53:09 am »
On unix (of which Linux is a variant), you will generally need to do this in the context of the desktop environment (Window Manager) i.e. Gnome, KDE etc.

You might be able to do something using Lazarus for the specific case where a program you've written already has the forground focus and you want to transfer the focus elsewhere (i.e. including the case of starting another program from scratch). However in the general case there is a real possibility that you could end up getting bogged down with various Window Manager APIs or control programs: from experience I'd say that this is something you should research only if you /need/ it.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Thaddy

  • Hero Member
  • *****
  • Posts: 15555
  • Censorship about opinions does not belong here.
Re: User define global shortcut from background application (Linux)
« Reply #3 on: August 12, 2024, 11:05:46 am »
Yes, it depends on the desktop version, except if your unix/linux has /dev/uinput, which is rare.
You can then use tools like xdotool/ydotool to install a global key. xdotool comes with libxdotool so you can do it in freepascal.
ydotool handles over raw input and therefor needs the uinput device exposed.
xdotool uses, well, X. :D
Both have issues with Wayland, though and I am not aware of a Wayland compatible solution. Must be X based.
https://www.kernel.org/doc/html/v4.12/input/uinput.html
If not, you may install it by hand:
Code: Text  [Select][+][-]
  1. sudo mknod /dev/uinput c 10 223
  2. sudo chmod 666 /dev/uinput
  3. sudo modprobe uinput
This kernel module does NOT work on wsl/wsl2 installed nixes. It is simply not available.
« Last Edit: August 12, 2024, 11:39:52 am by Thaddy »
If I smell bad code it usually is bad code and that includes my own code.

MarkMLl

  • Hero Member
  • *****
  • Posts: 7527
Re: User define global shortcut from background application (Linux)
« Reply #4 on: August 12, 2024, 12:12:09 pm »
Yes, it depends on the desktop version, except if your unix/linux has /dev/uinput, which is rare.

I suppose that it might be possible to do it at the kernel's event handling level... I'm using that for handling a gamepad but trying to interwork with a controlled selection of application programs and trying to control it from Pascal will not be easy.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

dbannon

  • Hero Member
  • *****
  • Posts: 3026
    • tomboy-ng, a rewrite of the classic Tomboy
Re: User define global shortcut from background application (Linux)
« Reply #5 on: August 12, 2024, 12:41:24 pm »
The model I outlined in the second post works fine with every Desktop I have tested. And I test against a lot of desktops !

Davo
Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

 

TinyPortal © 2005-2018