Recent

Author Topic: [SOLVED] Capturing Key Combinations Globally  (Read 1718 times)

JoeJoeTV

  • Jr. Member
  • **
  • Posts: 65
  • Hobbyist Programmer
    • Useless Website
[SOLVED] Capturing Key Combinations Globally
« on: April 05, 2021, 07:59:24 pm »
Hello there,

I want to bauild an application, where I need to capture global Hotkeys to activate something in the app.

I've searched around and found https://github.com/sysrpl/Cross.Codebot/, but AFAIK it only works on Linux and only supports gtk2(I tried it, since I am on linux), but I want to make my Application as much as possible cross platform.
I've also found https://github.com/salvadorbs/AsuiteComps, but I read here(https://forum.lazarus.freepascal.org/index.php/topic,52496), that it requires a c support library and I'm not sure how to use it correctly and how to compile it since I would probably have to do it myself.
I also found multiple results that showed how to do this in windows only, but since I want it to mostly work cross-platform or if not, work on linux atleast, that is not enough.
 
Can anybody help me with the library by El Salvador or provide antoher way I can get cross platform global hotkeys?

I appreciate every help I can get and if my english seems off sometimes, it's not my mother tongue.

EDIT: I treid the library by El Salvador and for gtk2 it works, but when trying it with Qt5, it says the previously mentioned c library is missing, which I don't know how to compile.
« Last Edit: April 21, 2021, 10:44:01 pm by JoeJoeTV »
Lazarus 2.2.4 / FPC 3.2.2 / 64bit / Linux Mint 21.1 Cinnamon
https://github.com/JoeJoeTV

Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 1120
  • Professional amateur ;-P
Re: Capturing Key Combinations Globally
« Reply #1 on: April 05, 2021, 08:12:32 pm »
Hey JoeJoeTV,

Can I ask for a clarification?

When you say global, do you mean that your app can be out of focus/minimized/on system tray and still capture that key combination?

Or is it just when your app is focused?

Cheers,
Gus
Lazarus 3.99(main) FPC 3.3.1(main) Ubuntu 23.10 64b Dark Theme
Lazarus 3.0.0(stable) FPC 3.2.2(stable) Ubuntu 23.10 64b Dark Theme
http://github.com/gcarreno

JoeJoeTV

  • Jr. Member
  • **
  • Posts: 65
  • Hobbyist Programmer
    • Useless Website
Re: Capturing Key Combinations Globally
« Reply #2 on: April 05, 2021, 08:39:27 pm »
Yeah, I mean when the app isn't focused, also maybe when it is minimized to the tray, yso you can link hotkeys to actions in the application.
Lazarus 2.2.4 / FPC 3.2.2 / 64bit / Linux Mint 21.1 Cinnamon
https://github.com/JoeJoeTV

Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 1120
  • Professional amateur ;-P
Re: Capturing Key Combinations Globally
« Reply #3 on: April 05, 2021, 08:47:17 pm »
Hey JoeJoeTV,

Yeah, I mean when the app isn't focused, also maybe when it is minimized to the tray, yso you can link hotkeys to actions in the application.

Many thanks for the clarification!!

Cheers,
Gus
Lazarus 3.99(main) FPC 3.3.1(main) Ubuntu 23.10 64b Dark Theme
Lazarus 3.0.0(stable) FPC 3.2.2(stable) Ubuntu 23.10 64b Dark Theme
http://github.com/gcarreno

Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 1120
  • Professional amateur ;-P
Re: Capturing Key Combinations Globally
« Reply #4 on: April 05, 2021, 08:56:37 pm »
Hey JoeJoeTV,

Here's another link to add to the pile of stuff to research: Global keyboard hook

I'm still digging for more.

Cheers,
Gus
Lazarus 3.99(main) FPC 3.3.1(main) Ubuntu 23.10 64b Dark Theme
Lazarus 3.0.0(stable) FPC 3.2.2(stable) Ubuntu 23.10 64b Dark Theme
http://github.com/gcarreno

Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 1120
  • Professional amateur ;-P
Re: Capturing Key Combinations Globally
« Reply #5 on: April 05, 2021, 09:01:55 pm »
Hey JoeJoeTV,

Here's another one for windows via Delphi help: https://forum.lazarus.freepascal.org/index.php/topic,17911.msg99932.html#msg99932

Cheers,
Gus
Lazarus 3.99(main) FPC 3.3.1(main) Ubuntu 23.10 64b Dark Theme
Lazarus 3.0.0(stable) FPC 3.2.2(stable) Ubuntu 23.10 64b Dark Theme
http://github.com/gcarreno

Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 1120
  • Professional amateur ;-P
Re: Capturing Key Combinations Globally
« Reply #6 on: April 05, 2021, 09:20:26 pm »
Hey JoeJoeTV,

And I've reached the same conclusion as you: Loads for Windows but only Codebot for GTK.

Sorry!

Cheers,
Gus
Lazarus 3.99(main) FPC 3.3.1(main) Ubuntu 23.10 64b Dark Theme
Lazarus 3.0.0(stable) FPC 3.2.2(stable) Ubuntu 23.10 64b Dark Theme
http://github.com/gcarreno

El Salvador

  • Full Member
  • ***
  • Posts: 134
Re: Capturing Key Combinations Globally
« Reply #7 on: April 05, 2021, 09:42:53 pm »
Hello there,

I want to bauild an application, where I need to capture global Hotkeys to activate something in the app.

I've searched around and found https://github.com/sysrpl/Cross.Codebot/, but AFAIK it only works on Linux and only supports gtk2(I tried it, since I am on linux), but I want to make my Application as much as possible cross platform.
I've also found https://github.com/salvadorbs/AsuiteComps, but I read here(https://forum.lazarus.freepascal.org/index.php/topic,52496), that it requires a c support library and I'm not sure how to use it correctly and how to compile it since I would probably have to do it myself.
I also found multiple results that showed how to do this in windows only, but since I want it to mostly work cross-platform or if not, work on linux atleast, that is not enough.
 
Can anybody help me with the library by El Salvador or provide antoher way I can get cross platform global hotkeys?

I appreciate every help I can get and if my english seems off sometimes, it's not my mother tongue.

EDIT: I treid the library by El Salvador and for gtk2 it works, but when trying it with Qt5, it says the previously mentioned c library is missing, which I don't know how to compile.
In qt5, you must build (qmake, make & make install) QGHotkeyHookPas (https://www.github.com/salvadorbs/AsuiteComps/tree/main/library%2Fplatform%2Funix%2FQGHotkeyHookPas) and distribuite it with your application. It is a very simple c library to install the event filter for qtsystem (I haven't found a way to do it in pascal). In simple words,

In a nutshell, the library allows you to install an event filter for QCoreApplications (see https://doc.qt.io/qt-5/qcoreapplication.html#installNativeEventFilter). Also I had a hard time recovering keyCode and keyState data in FreePascal, so I had to do it in C and then pass it into FreePascal. (see https://github.com/salvadorbs/AsuiteComps/blob/85aa9ed667f5016f8ff285b89a738dfa55fce677/library/platform/unix/QGHotkeyHookPas/src/qghotkey_hook.h#L76)

In Windows (see https://github.com/salvadorbs/AsuiteComps/blob/main/library/platform/win/Hotkeys.Manager.Platform.pas) and gtk2 (see https://github.com/salvadorbs/AsuiteComps/blob/85aa9ed667f5016f8ff285b89a738dfa55fce677/library/platform/unix/Hotkeys.Manager.Platform.pas#L466), this operation is very simple and you can do it in pascal. In GTK3, I don't know. Maybe you can use a GTK3 hook library and binding it in pascal.

Edit: More information for QT and links.
« Last Edit: April 06, 2021, 09:52:36 am by El Salvador »

JoeJoeTV

  • Jr. Member
  • **
  • Posts: 65
  • Hobbyist Programmer
    • Useless Website
Re: Capturing Key Combinations Globally
« Reply #8 on: April 21, 2021, 10:43:46 pm »
Hi @El Salvador,

Thank you for helping me with this, I got it to build and tried it with a fes test programs and it worked.
I will mark this thread as solved now.

Again, thank you, I can now continue working on my small project. :)
Lazarus 2.2.4 / FPC 3.2.2 / 64bit / Linux Mint 21.1 Cinnamon
https://github.com/JoeJoeTV

El Salvador

  • Full Member
  • ***
  • Posts: 134
Re: [SOLVED] Capturing Key Combinations Globally
« Reply #9 on: April 28, 2021, 07:35:27 pm »
I'm glad it all works. By the way, these days I managed to add support for the GTK3 widgetset (strangely it doesn't work on distro KDE based, but on Ubuntu I had no problem).

 

TinyPortal © 2005-2018