|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "time" |
| 5 | + |
| 6 | + "github.com/MarinX/keylogger" |
| 7 | + "github.com/sirupsen/logrus" |
| 8 | +) |
| 9 | + |
| 10 | +func main() { |
| 11 | + |
| 12 | + // find keyboard device, does not require a root permission |
| 13 | + keyboard := keylogger.FindKeyboardDevice() |
| 14 | + |
| 15 | + // check if we found a path to keyboard |
| 16 | + if len(keyboard) <= 0 { |
| 17 | + logrus.Error("No keyboard found...you will need to provide manual input path") |
| 18 | + return |
| 19 | + } |
| 20 | + |
| 21 | + logrus.Println("Found a keyboard at", keyboard) |
| 22 | + // init keylogger with keyboard |
| 23 | + k, err := keylogger.New(keyboard) |
| 24 | + if err != nil { |
| 25 | + logrus.Error(err) |
| 26 | + return |
| 27 | + } |
| 28 | + defer k.Close() |
| 29 | + |
| 30 | + // write to keyboard example: |
| 31 | + go func() { |
| 32 | + time.Sleep(5 * time.Second) |
| 33 | + // open text editor and focus on it, it should say "marin" and new line will be printed |
| 34 | + keys := []string{"m", "a", "r", "i", "n", "ENTER"} |
| 35 | + for _, key := range keys { |
| 36 | + // write once will simulate keyboard press/release, for long press or release, lookup at Write |
| 37 | + k.WriteOnce(key) |
| 38 | + } |
| 39 | + }() |
| 40 | + |
| 41 | + events := k.Read() |
| 42 | + |
| 43 | + // range of events |
| 44 | + for e := range events { |
| 45 | + switch e.Type { |
| 46 | + // EvKey is used to describe state changes of keyboards, buttons, or other key-like devices. |
| 47 | + // check the input_event.go for more events |
| 48 | + case keylogger.EvKey: |
| 49 | + |
| 50 | + // if the state of key is pressed |
| 51 | + if e.KeyPress() { |
| 52 | + logrus.Println("[event] press key ", e.KeyString()) |
| 53 | + } |
| 54 | + |
| 55 | + // if the state of key is released |
| 56 | + if e.KeyRelease() { |
| 57 | + logrus.Println("[event] release key ", e.KeyString()) |
| 58 | + } |
| 59 | + |
| 60 | + break |
| 61 | + } |
| 62 | + } |
| 63 | +} |
0 commit comments