Skip to content

Commit

Permalink
feat(examples): rewrite snoop example to use a virtual keyboard
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuar committed May 3, 2023
1 parent 5b0a50d commit 7b53e19
Showing 1 changed file with 26 additions and 11 deletions.
37 changes: 26 additions & 11 deletions examples/snoop/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,31 @@ func main() {
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
zerolog.SetGlobalLevel(zerolog.DebugLevel)

keys := gokbd.SnoopAllKeyboards(gokbd.OpenKeyboardDevices())
for k := range keys {
if k.Value == 1 && k.TypeName == "EV_KEY" {
log.Debug().Msgf("Key pressed: %s %s %d %c", k.TypeName, k.EventName, k.Value, k.AsRune)
}
if k.Value == 0 && k.TypeName == "EV_KEY" {
log.Debug().Msgf("Key released: %s %s %d", k.TypeName, k.EventName, k.Value)
}
if k.Value == 2 && k.TypeName == "EV_KEY" {
log.Debug().Msgf("Key held: %s %s %d %c", k.TypeName, k.EventName, k.Value, k.AsRune)
}
vDev, err := gokbd.NewVirtualKeyboard("gokbd test")
if err != nil {
log.Panic().Msg("Could not create a virtual keyboard!")
}
kbd, err := gokbd.OpenKeyboardDevice(vDev.Device())
if err != nil {
log.Panic().Msg("Could not access virtual keyboard!")
}

keys := gokbd.SnoopKeyboard(kbd)

// keys := gokbd.SnoopAllKeyboards(gokbd.OpenKeyboardDevices())
go func() {
for k := range keys {
if k.Value == 1 && k.TypeName == "EV_KEY" {
log.Debug().Msgf("Key pressed: %s %s %d %c", k.TypeName, k.EventName, k.Value, k.AsRune)
}
if k.Value == 0 && k.TypeName == "EV_KEY" {
log.Debug().Msgf("Key released: %s %s %d", k.TypeName, k.EventName, k.Value)
}
if k.Value == 2 && k.TypeName == "EV_KEY" {
log.Debug().Msgf("Key held: %s %s %d %c", k.TypeName, k.EventName, k.Value, k.AsRune)
}
}
}()
vDev.TypeString("Hello there!")
vDev.Close()
}

0 comments on commit 7b53e19

Please sign in to comment.