Autotyping passwords
The need for this came about when was using a program on Linux that I couldn’t paste into. I use pass for my password manager which will put your unlocked password directly into the clipboard using the --c
option. Thus the goal was to write an easy to use script that could autotype whatever is in the clipboard.
Setup
We need to install a few Python packages in order to get this functionality. We will use pyuserinput
for the typing and clipboard
to access the clipboard.
$ pip3 install pyuserinput clipboard
The script
#!/usr/bin/env python3
from pykeyboard import PyKeyboard
from pymouse import PyMouseEvent
import clipboard
import time
class AutoTyper(PyMouseEvent):
def __init__(self):
PyMouseEvent.__init__(self)
self.keyboard = PyKeyboard()
self.string_to_paste = clipboard.paste()
def click(self, x, y, button, press):
if press:
self.keyboard.type_string(self.string_to_paste)
exit()
at = AutoTyper()
at.run()
Note the class inherits from PyMouseEvent
. This way we just need to write the click
function, which as you might guess gets called when the user clicks their mouse. While the docs for pyuserinput
say to call self.stop()
when we’re done listening for mouse input, I found this would just hang so I used an exit()
instead.
Usage
I then simply put this script in my path and made it executable. When I need to autotype a password, I run pass -c whateverpassword
and then run the script (in my case using the handy dmenu
that comes with i3wm
). Finally, I just click wherever I want the password to be typed, and the script types away. Very handy.