fyne icon indicating copy to clipboard operation
fyne copied to clipboard

add feature to hook global key bindings into (desktop?) OS

Open yangguangwuwu opened this issue 3 years ago • 15 comments

add Handle Key Presses in App

hi! I want to listen for shortcuts in the background and then do some tasks

I looked at the documentation and didn't find the API

yangguangwuwu avatar Jun 29 '21 06:06 yangguangwuwu

Please use the templates (the feature request template in this case) when opening issues in the future.

There is already support for this. You should be able to pass a function to the .SetOnTypedKey() method on the window canvas (https://pkg.go.dev/fyne.io/fyne/v2#Canvas). That should be your window .Canvas().SetOnTypedKey().

Jacalz avatar Jun 29 '21 15:06 Jacalz

I'm sorry I wrote a new one .Canvas().SetOnTypedKey() This function is inconsistent

Is your feature request related to a problem? Please describe:

yes I need to achieve a quick screenshot through the keyboard Minimize background running .Canvas() .Canvas().SetOnTypedKey() .Canvas().SetOnTypedKey() .Canvas() .AddShortcut(shortcut Shortcut, handler func(shortcut Shortcut)) Only when the program is running in the foreground can it take effect Only when the current work window takes effect

Is it possible to construct a solution with the existing API?

no Canvas() .Canvas().SetOnTypedKey() .Canvas().SetOnTypedKey() .Canvas() .AddShortcut()
Only when the current work window takes effect

Describe the solution you'd like to see:

I need an API API similar to .Canvas() .AddShortcut()
background running Non current work window takes effect

yangguangwuwu avatar Jun 30 '21 06:06 yangguangwuwu

The function is not inconsistent, it is simply not supposed to take inputs other than when the canvas is focused. Most operating systems, as far as I know, don't exactly send key presses to applications when they are not focused. That usually requires some kind of deeper hook into the key handling in the desktop environment and that is going to be very platform specific. I think that it most likely is outside the scope of Fyne, but that is just my understanding of the matter.

Jacalz avatar Jun 30 '21 14:06 Jacalz

If I understand your use case correctly, I think the most sensible solution in this case would be to overwrite a shortcut in your operating system and make that to run your application instead.

Jacalz avatar Jun 30 '21 14:06 Jacalz

I think there is something wrong with my description, which makes you understand it wrong

For example

I'm playing a game

I use the keyboard shortcut of OBS Studio software to record video in the game

fyne cannot be performed in inactive window

My active window is the game interface

yangguangwuwu avatar Jul 01 '21 01:07 yangguangwuwu

Fyne lacks API to implement function

These hotkeys can be run in inactive windows

微信截图_20210701101738

yangguangwuwu avatar Jul 01 '21 02:07 yangguangwuwu

Almost many applications have the function of receiving shortcut keys in inactive windows

OBS discord ....

yangguangwuwu avatar Jul 01 '21 02:07 yangguangwuwu

The implementation is not inconsistent, it simply lacks the feature you desire - OS global Keys must be registered separately and have complications such as needing to be globally unique. I have updated the title accordingly.

andydotxyz avatar Jul 01 '21 08:07 andydotxyz

thank you

yangguangwuwu avatar Jul 01 '21 08:07 yangguangwuwu

Reopening to track the feature addition.

andydotxyz avatar Jul 01 '21 09:07 andydotxyz

@Jacalz thank your quick response! Is there any plan to deliver this feature?

kcmvp avatar Mar 21 '22 14:03 kcmvp

The plan is currently to target this feature for the next big release after the one we are working on now (codename "Cragganmore"). Things might change, especially since someone actually needs to picks this up and work on it, but that's the rough plan.

Jacalz avatar Mar 21 '22 15:03 Jacalz

Many thanks!

kcmvp avatar Mar 21 '22 15:03 kcmvp

I would very much like to see the ability to register global hotkeys as well. I would like to develop a 'hotkeys' type application to run in the background with other windows in focus. I'll keep an eye on this thread.

onyx-and-iris avatar Jul 14 '22 14:07 onyx-and-iris

Seems like this feature is coming to GLFW https://github.com/glfw/glfw/issues/1204

matwachich avatar Jul 26 '22 17:07 matwachich

Is that now possible?

dpengchen avatar Aug 25 '23 07:08 dpengchen

No. The GLFW issue has not been resolved yet and this issue is also open still.

Jacalz avatar Aug 25 '23 07:08 Jacalz

I managed to work around it using https://github.com/golang-design/hotkey

ivancho-ifa avatar Nov 05 '23 23:11 ivancho-ifa

This might be doable on Linux (including Wayland) for supported desktops using the XDG Desktop Portal interface for GlobalShortcuts: https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.portal.GlobalShortcuts.html

Jacalz avatar Nov 28 '23 16:11 Jacalz