Lazarus

Free Pascal => General => Topic started by: Trenatos on December 01, 2018, 01:52:26 am

Title: Systemwide keyboard hotkey
Post by: Trenatos 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?
Title: Re: Systemwide keyboard hotkey
Post by: Kays on December 01, 2018, 03:00:40 pm
[…] Do we have a cross platform way?
No.
Title: Re: Systemwide keyboard hotkey
Post by: sash 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.
Title: Re: Systemwide keyboard hotkey
Post by: lucamar 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.
Title: Re: Systemwide keyboard hotkey
Post by: Trenatos 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.
Title: Re: Systemwide keyboard hotkey
Post by: taazz 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.
Title: Re: Systemwide keyboard hotkey
Post by: Trenatos 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.
Title: Re: Systemwide keyboard hotkey
Post by: Remy Lebeau 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() (https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-setclipboardviewer) (XP and earlier) or AddClipboardFormatListener() (https://docs.microsoft.com/en-us/windows/desktop/api/Winuser/nf-winuser-addclipboardformatlistener) (Vista and later) for that.  See Monitoring Clipboard Contents (https://docs.microsoft.com/en-us/windows/desktop/dataxchg/using-the-clipboard#monitoring-clipboard-contents) on MSDN for more details.

Other platforms may have similar monitoring capabilities, you will have to check on that yourself.
Title: Re: Systemwide keyboard hotkey
Post by: Trenatos 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 :)
Title: Re: Systemwide keyboard hotkey
Post by: Remy Lebeau on December 04, 2018, 09:14:15 pm
I still need a hotkey though

On Windows, look at things like:

Title: Re: Systemwide keyboard hotkey
Post by: lucamar 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.
Title: Re: Systemwide keyboard hotkey
Post by: Trenatos on December 04, 2018, 10:36:33 pm
Awesome info guys, I appreciate it  :)
Title: Re: Systemwide keyboard hotkey
Post by: Kays 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.
TinyPortal © 2005-2018