Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Linux] Application does not get triggered when hot-key is pressed #93

Open
CsBigDataHub opened this issue Jan 3, 2025 · 16 comments
Open
Assignees

Comments

@CsBigDataHub
Copy link

CsBigDataHub commented Jan 3, 2025

Hello,

I was able to build the application but I am not able to trigger it even though I can see the application running on task bar.

I tried multiple combinations without success, here are the combinations I tried to set in application settings -

ctrl+space
ctrl+`
ctrl+F1 [I tried all function keys]
Ctrl+space
Ctrl+`
Ctrl+F1 [I tried all function keys]
Just keys too.

Information:

  • Firmware Version: 1.93
  • OS Name: openSUSE Tumbleweed
  • OS Type: 64-bit
  • GNOME Version: 47
  • Windowing System: Wayland
  • Kernel Version: Linux 6.12.6-1-default
@theJayTea
Copy link
Owner

Hi! Thanks for reaching out.

@momokrono , would it be possible for you to take a look here?

@momokrono
Copy link
Collaborator

Hi!
This is probably a problem related to wayland, since by default it does not allow apps to listen to keypresses unless they are in focus.

So, to debug your problem, I would start by logging into an Xorg session, check if the app works there and then try to make it work in wayland.

To login into an Xorg session you should select something like Gnome legacy or Gnome on X or anything along this line in your login manager.

@CsBigDataHub
Copy link
Author

@momokrono,

You are spot on. Issue is with Wayland. Switched to Xorg and it has been working fine.

Maybe a note in README might help other users.

@momokrono
Copy link
Collaborator

Well, glad to have "solved" your issue ^^"

As a note, in theory if you run a wayland session, all apps running via xwayland (stuff like IDEs, discord, slack and so on) will work, since the apps run inside an Xorg server and so global hotkeys will trigger correctly, but only inside those apps. KDE Plasma 6 has released an initial support for global hotkeys in wayland not long ago, if you need wayland (for stuff like screen tearing, HDR etc) you can try it.

And yes, the README is outdated and needs some updated info, I'll add these things as soon as I can. Thank you for pointing this out.

@CsBigDataHub
Copy link
Author

@momokrono, I've managed to get the app working on Wayland, albeit with a generous interpretation of that term, using KDE Plasma 6.2.5 and the global hotkeys.

In contrast, when running on X11 (using xorg), I can see AI options available. However, on Wayland, all I see is a text box even when a paragraph is selected, the options in the highlighted red box do not appear.

Screenshot_20250106_175124

@CsBigDataHub
Copy link
Author

Here is the screenshot on wayland with some content selected. I do not see any options except for the text box.

Screenshot_20250106_190116

@theJayTea
Copy link
Owner

Hi @CsBigDataHub .
That’s happening because Writing Tools is not able to detect any selected text (and is opening in chat mode, which shows when you invoke it without selecting text).

Internally, it emulates a ctrl+c and then tries to read the clipboard to get a hold of the selected text (and also does this without messing with your existing clipboard content by saving & putting that back in milliseconds).

However, it looks like Wayland isn’t allowing that to work. Welp :/

@momokrono
Copy link
Collaborator

Hello @CsBigDataHub

if you run the app from the terminal, when you spawn the window after selecting some text you should see some logs with the backup of the clipboard and the text copied.

Do you see any errors? Is the clipboard accessed correctly? Does the program complain about missing packages?

For example, on a fresh arch installation, I had to install xclip as a necessary dependency. This package is used by pyperclip (the library used to handle the clipboard) to correctly grab the content you selected.

Reopening for visibility

@momokrono momokrono reopened this Jan 7, 2025
@CsBigDataHub
Copy link
Author

@theJayTea, @momokrono,

I debugged this application this morning, looks like Ctrl+c cannot be simulated on wayland with pynput.

This is the DEBUG log -

2025-01-07 09:37:29,052 - DEBUG - Loading config from /home/mypc/git-repos/WritingTools/Windows_and_Linux/config.json
2025-01-07 09:37:29,053 - DEBUG - Config loaded successfully
2025-01-07 09:37:29,053 - DEBUG - Config found, setting up hotkey and tray icon
2025-01-07 09:37:29,115 - DEBUG - Creating system tray icon
2025-01-07 09:37:29,122 - DEBUG - Tray icon dark
2025-01-07 09:37:29,122 - DEBUG - Tray icon displayed
2025-01-07 09:37:29,122 - DEBUG - Registering hotkey
2025-01-07 09:37:29,122 - DEBUG - Registering global hotkey for shortcut: <ctrl>+`
2025-01-07 09:37:29,124 - DEBUG - Hotkey registered
2025-01-07 09:37:34,126 - DEBUG - triggered hotkey
2025-01-07 09:37:34,126 - DEBUG - Hotkey pressed
2025-01-07 09:37:34,126 - DEBUG - Cancelling current provider's request
2025-01-07 09:37:34,134 - DEBUG - Showing popup window
2025-01-07 09:37:34,136 - DEBUG - Clipboard backup: "" (sleep: 0.2s)
2025-01-07 09:37:34,137 - DEBUG - Simulating Ctrl+C
2025-01-07 09:37:48,312 - DEBUG - Waited 0.2s for clipboard
2025-01-07 09:37:57,692 - DEBUG - Selected text: "        press_ctrl_c()
"
2025-01-07 09:38:06,612 - DEBUG - Selected text: "        press_ctrl_c()

As you can see, no text is being selected.

Looks like pynput does not support wayland (yet).

Here are some references -

LOUDO56/PyMacroRecord#61
streamdeck-linux-gui/streamdeck-linux-gui#103

I tried to use kdotool by adding it to the path, but looks like it needs elevated permissions to dumpkeys.
https://eco.kde.org/blog/2024-02-20-sok24-wayland_support_kdeecotest/
https://github.com/jinliu/kdotool

@CsBigDataHub
Copy link
Author

CsBigDataHub commented Jan 8, 2025

Hello @momokrono and @theJayTea,

I have identified a potential workaround for the issue on wayland. My web browsers are installed as flatpaks. By utilizing Flatseal to manage flatpak permissions, I disabled the wayland socket, which resolved the problem.

Screenshot_20250107_200232

I am now investigating whether a similar approach can be applied to native applications. I attempted to launch kmail with the command XDG_SESSION_TYPE=x11 kmail from the terminal. Although kmail launched and functioned correctly, the Writing-tools application was still unable to simulate Ctrl+c.

@momokrono
Copy link
Collaborator

@CsBigDataHub

I honestly have no clue why it's not working properly, and right now I'm installing Tumbleweed inside a VM to play with it and see if I can help you solve this.

I know flatpacks run sandboxed but I would have never guessed even emulating ctrl+c would be troublesome.

I've read online some users suggest adding yourself to the input group, but I personally didn't need to do so and the app works correctly anyway.

Also, from the logs you provided the app did in fact copy the clipboard:

2025-01-07 09:37:34,136 - DEBUG - Clipboard backup: "" (sleep: 0.2s)    <--- your clipboard was empty, so the backup is the empy string
2025-01-07 09:37:34,137 - DEBUG - Simulating Ctrl+C
2025-01-07 09:37:48,312 - DEBUG - Waited 0.2s for clipboard
2025-01-07 09:37:57,692 - DEBUG - Selected text: "        press_ctrl_c()    <--- this is what you copied from the clipboard
"

so, the app is copying something and this should spawn the popup with the different options. If it doesn't, then there's another bug I should investigate.

@momokrono momokrono self-assigned this Jan 8, 2025
@momokrono
Copy link
Collaborator

Update:
inside the VM, I was able to replicate what I said some messages ago: I installed discord, which I knew runs via xwayland (you can use the command xlsclients to list all apps running via xwayland, get it with sudo zypper install xlsclients) and in this case WritingTools correctly grabs what you select. Outside of xwayland apps, the app spawns the chat popup and works in chat mode, but is unable to correctly grab the selected text.

This is very likely a problem with pynput I need to investigate/fix. I will probably need to switch to python-uinput to have Wayland working correctly, but this will require some refactoring.

For now, Xorg seems to be the only viable session to correctly use the app.

@CsBigDataHub
Copy link
Author

Clipboard backup: "" (sleep: 0.2s) <--- your clipboard was empty, so the backup is the empty string

might not be the case. I have a clipboard history enabled and I am using clipboard history plugin to navigate my clipboard.

2025-01-07 09:37:57,692 - DEBUG - Selected text: " press_ctrl_c() <--- this is what you copied from the clipboard

Yes when Ctrl+c is simulated, the Writing-tools does not recognize the selected text on any application running on Wayland. If the application runs on XWayland, Writing-tools seems to working with out any issue.

@theJayTea
Copy link
Owner

As a summary:

Currently, Writing Tools only works on Wayland if you:

This is due to Wayland's security architecture — non-active windows can't intercept hotkeys.

In the future, we could look into implementing KDE's global shortcuts API so things work smoothly on Wayland with KDE.

@theJayTea
Copy link
Owner

I'm curious — @momokrono @CsBigDataHub , if we're able to compile Writing Tools into a Flatpak, and then turn off Wayland for it with Flatseal, would things magically work haha?

@CsBigDataHub
Copy link
Author

Only one way to find out :).
Let me try and compile it Flatpak.

But I want to point out that, compiling the app and using it have been finicky. So I am running the app with python main.py in a venv, which has been more stable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants