Basic keylogger in Go (no C deps)
Capture global keyboard events on Linux
go get github.com/MarinX/keylogger
There is a helper on finding the keyboard.
go keyboard := keylogger.FindKeyboardDevice()Which goes through each file device name to find keyword "keyboard"
sh /sys/class/input/event[0-255]/device/nameand returns the file event path if found
sh /dev/input/event2If the function returns empty string, you will need to cat each device name and get the event number. If you know already, you can easily pass it to constructor
Once the keylogger returns channel event, you can switch by event code as described in input_event.go For start, you can listen on keyboard state change
go keylogger.EvKeyOnce you get desire event, there is a helper to parse code into human readable key.
Best way is to open an text editor and see how keyboard will react There are 2 methods:
go func (k *KeyLogger) WriteOnce(key string) errorand
go func (k *KeyLogger) Write(direction KeyEvent, key string) error
WriteOncemethod simulates single key press, eg: press and release letter M
Writewrites to keyboard and sync the event. This will keep the key pressed or released until you call another write with other direction eg, if the key is "A" and direction is press, on UI, you will see "AAAAA..." until you stop with release
Probably you want to use
If you listen on keyboard state change, it will return double results. This is because pressing and releasing the key are 2 different state change. There is a helper function which you can call to see which type of state change happend ```go // returns true if key on keyboard is pressed event.KeyPress()
// returns true if key on keyboard is released event.KeyRelease() ```
You can find a example script in
No magic, just run
sh go test -v
This library is under the MIT License