moses-palmer / pynput

Sends virtual input commands
GNU Lesser General Public License v3.0
1.78k stars 248 forks source link

idea for new back end: using the user's shell key binding functionality to detect keys without root or X? #548

Closed sudomain closed 1 year ago

sudomain commented 1 year ago

Hello,

I came across this project when I began working on my hot key project. I was going to use this until I read about the limitations for Linux (either run as root or run X). On the system I'm developing for (Termux), I don't have root and I'd like to not run X all the time (though it is possible using a VNC server).

I thought I'd share the idea I settled on for my project, and propose it as another back end for pynput (not requiring root or X): The user's shell can be utilized for receiving a key signal and then sending information to pynput. For example (in Bash):

setting a key bind for global hotkeys

bind -x '"\C-p\C-p": "termux-notification -c Ctrl-pCtrl-ptestnotification"' The termux-notification... is a command for termux that can be replaced with something like python ... to send a signal to pynput from the shell. Pynput could register hotkeys for the user or advise them how to do it without cluttering their bashrc file:

avoiding clutter in bashrc

When bash is started interactively (with -i which I think is required for control codes to work), a separate rc file can be specified: bash --rcfile custom-bash-rc -i This file itself can source the default ~/.bashrc so users can have two types of shell:

  1. one containing only their functions, binds, etc.
  2. And one containing their customizations as well as potential pynput signalling keybinds mentioned above

Output/Simulating keypresses to a shell

This I just learned about so it may be incomplete. We can pipe Ctrl characters to an interactive shell. Assuming the user has some bindings setup (e.g. the C-p C-p from above), we can trigger that binding or just send arbitrary keys using: printf $'\cp\cp' | bash -i

Of course most of these examples are Bash-centric, but from what I can tell other major shells also have key binding functionality. Sorry for such a rambling post. The tl;dr is: What do you think of using the user's shell for detecting keypresses to avoid the requirements of root or running X?

moses-palmer commented 1 year ago

Thank you for your suggestion!

I think, however, that it would not really be possible to integrate this solution with the interface presented by this library. Using bash, or really readline, to signal a Python process would be a very roundabout way of just reading from the terminal.

But if you write an application to do this, please send me a ping!