Recent

Author Topic: Systemwide keyboard hotkey  (Read 1400 times)

Trenatos

  • Sr. Member
  • ****
  • Posts: 454
  • Software developer - Open source contributor
    • MarcusFernstrom.com
Systemwide keyboard hotkey
« on: December 01, 2018, 01:52:26 am »
I made a minimalist clipboard manager (https://github.com/MFernstrom/cloppy) and one of my friends requested I bind it to a systemwide hotkey, IE. listen for raw keyboard events while not in focus.

I looked around, obviously, but only found a Windows-specific example.

Do we have a cross platform way?

Kays

  • Full Member
  • ***
  • Posts: 116
  • Whasup!?
    • KaiBurghardt.de
Re: Systemwide keyboard hotkey
« Reply #1 on: December 01, 2018, 03:00:40 pm »
[…] Do we have a cross platform way?
No.
Yours Sincerely
Kai Burghardt

sash

  • Full Member
  • ***
  • Posts: 158
Re: Systemwide keyboard hotkey
« Reply #2 on: December 01, 2018, 03:36:51 pm »
Some notes:

1. "Systemwide" hotkey could be handled in multiple ways: particularly
  - as global input event hook (on linux-like)
  - as Dispaly manager hook (on linux-like X11 and similarly on Windows)
  - as configurable desktop environment shortcut/launcher (it is rather matter of user's machine configuration).

2. A systemwide hotkey behavior  should be carefully thought out, because of possible interference with running applications.
Lazarus 1.8.4 Unversioned directory FPC 3.0.4 x86_64-linux-gtk2 -- Ubuntu 18.04 XFCE

lucamar

  • Sr. Member
  • ****
  • Posts: 494
Re: Systemwide keyboard hotkey
« Reply #3 on: December 01, 2018, 05:18:29 pm »
I made a minimalist clipboard manager [...]

IMHO, you (or your friend) don't need a global keyboard hook. What you need is to register your application as a clipboard handler, which you can learn how to do in your favourite window manager programming documentation.

Once registered, your program will be called to process clipboard events, whether the (user-configurable) system-wide keys have been pressed or an application requets a clipboard operation.

But you should already know all this, if you have programmed a clipbrd manager.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 1.8.4/FPC 3.0.4 on:
(K)Ubuntu 11..16, Windows XP SP3 (Home/Prof.) and various DOS incarnations.

Trenatos

  • Sr. Member
  • ****
  • Posts: 454
  • Software developer - Open source contributor
    • MarcusFernstrom.com
Re: Systemwide keyboard hotkey
« Reply #4 on: December 02, 2018, 11:40:56 pm »
What Cloppy does (The program I made) is simple, it polls the clipboard every 500ms and waits for the value to change, when it changes the new value is added to the listbox.

Double click an item and the value is placed into the clipboard buffer.

What I'm looking to do is create a shortcut to open the program, I figured I'd create a background process that listens for raw keyboard input and opens + focus the program.

taazz

  • Hero Member
  • *****
  • Posts: 5362
Re: Systemwide keyboard hotkey
« Reply #5 on: December 03, 2018, 02:15:35 am »
I made a minimalist clipboard manager (https://github.com/MFernstrom/cloppy) and one of my friends requested I bind it to a systemwide hotkey, IE. listen for raw keyboard events while not in focus.

I looked around, obviously, but only found a Windows-specific example.

Do we have a cross platform way?
as far as I know there is no ready made library/unit that will help you to auto register a system wide hot key. you already found the windows example. here is a link for a macos example in cocoa and Objective-C https://snippets.aktagon.com/snippets/361-Registering-global-hot-keys-with-Cocoa-and-Objective-C
I have no idea how is done on linux/bsd you have to search for the systems you are interested.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

Trenatos

  • Sr. Member
  • ****
  • Posts: 454
  • Software developer - Open source contributor
    • MarcusFernstrom.com
Re: Systemwide keyboard hotkey
« Reply #6 on: December 03, 2018, 03:30:00 am »
Thanks taazz.

If I find a reliable way across the common platforms I'll create a package, or an fpc patch.

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 515
    • Lebeau Software
Re: Systemwide keyboard hotkey
« Reply #7 on: December 03, 2018, 09:09:53 pm »
What Cloppy does (The program I made) is simple, it polls the clipboard every 500ms and waits for the value to change, when it changes the new value is added to the listbox.

Don't poll at all if the clipboard itself can actively notify you whenever a change occurs.  That is what @lucamar was trying to tell you.

For instance, on Windows, use SetClipboardViewer() (XP and earlier) or AddClipboardFormatListener() (Vista and later) for that.  See Monitoring Clipboard Contents on MSDN for more details.

Other platforms may have similar monitoring capabilities, you will have to check on that yourself.
« Last Edit: December 04, 2018, 09:07:14 pm by Remy Lebeau »
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

Trenatos

  • Sr. Member
  • ****
  • Posts: 454
  • Software developer - Open source contributor
    • MarcusFernstrom.com
Re: Systemwide keyboard hotkey
« Reply #8 on: December 04, 2018, 02:36:59 am »
I had no idea you could do that, definitely going to look into it.

I still need a hotkey though, so that's two items on the Todo list :)

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 515
    • Lebeau Software
Re: Systemwide keyboard hotkey
« Reply #9 on: December 04, 2018, 09:14:15 pm »
I still need a hotkey though

On Windows, look at things like:

Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

lucamar

  • Sr. Member
  • ****
  • Posts: 494
Re: Systemwide keyboard hotkey
« Reply #10 on: December 04, 2018, 09:54:15 pm »
On Linux your friend should be able to do it himself. The accompanying image shows a (Gnome) menu to access the application.

In Spanish, o.c. but it roughly tranlates to
  System>Preferences>Key combinations (i.e. "shortcuts")

Note that the same can be done by editing the "Applications" menus, editing the .desktop file, and/or with a pletora of other utiity programs.
« Last Edit: December 04, 2018, 09:56:08 pm by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 1.8.4/FPC 3.0.4 on:
(K)Ubuntu 11..16, Windows XP SP3 (Home/Prof.) and various DOS incarnations.

Trenatos

  • Sr. Member
  • ****
  • Posts: 454
  • Software developer - Open source contributor
    • MarcusFernstrom.com
Re: Systemwide keyboard hotkey
« Reply #11 on: December 04, 2018, 10:36:33 pm »
Awesome info guys, I appreciate it  :)

Kays

  • Full Member
  • ***
  • Posts: 116
  • Whasup!?
    • KaiBurghardt.de
Re: Systemwide keyboard hotkey
« Reply #12 on: December 09, 2018, 12:56:07 am »
Awesome info guys, I appreciate it  :)
Yet still not cross-platform, as you originally asked for. I would've written “replace the keyboard interrupt handler,” but that eliminates the issue of platforms altogether.
Yours Sincerely
Kai Burghardt