gokbd is a package that uses libevdev to talk to a keyboard on Linux. It allows snooping the keys pressed as well as typing out keys.
import gokbd "github.com/joshuar/gokbd"Examples for reading what keys are being typed (snooping) and writing to a
virtual keyboard are available under the examples/ directory. To run them:
cd examples/snoop
go build
sudo setcap cap_setgid,cap_setuid=p ./snoop
./snoop
cd examples/type
go build
sudo setcap cap_setgid,cap_setuid=p ./type
./typeYou may need to grant additional permissions to the user running any program
using gokbd.
- To read (snoop) from keyboards, the user will need to be part of the
inputgroup. Typically, the user can be added with the following command:
sudo gpasswd -a $USER input- To create a virtual keyboard and write to it, the user will need access to the kernel uinput device. Typically, this can be granted with a udev rule like the following:
echo KERNEL==\"uinput\", GROUP=\"$USER\", MODE:=\"0660\" | sudo tee /etc/udev/rules.d/99-$USER.rules
sudo udevadm triggerYou will also need to grant your compiled binary the CAP_SETUID and
CAP_SETGID Linux capabilities. You can do this with:
sudo setcap cap_setgid,cap_setuid=p /path/to/binary