Lazarus
Free Pascal => General => Topic started 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?
-
[…] Do we have a cross platform way?
No.
-
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.
-
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.
-
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.
-
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.
-
Thanks taazz.
If I find a reliable way across the common platforms I'll create a package, or an fpc patch.
-
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.
-
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 :)
-
I still need a hotkey though
On Windows, look at things like:
- RegisterHotKey() (https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-registerhotkey)
- SetWindowsHookEx() (https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-setwindowshookexw) and its WH_KEYBOARD (https://msdn.microsoft.com/en-us/library/windows/desktop/ms644984.aspx) and WH_KEYBOARD_LL (https://msdn.microsoft.com/en-us/library/windows/desktop/ms644985.aspx) hooks
- RegisterRawInputDevices() (https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-registerrawinputdevices) and the WM_INPUT (https://docs.microsoft.com/en-us/windows/desktop/inputdev/wm-input) notification message
-
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.
-
Awesome info guys, I appreciate it :)
-
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.