From 9904d3574537db492a6169e38ec2ff5dfddcf908 Mon Sep 17 00:00:00 2001 From: SakthiMahendran <84713203+SakthiMahendran@users.noreply.github.com> Date: Sun, 6 Nov 2022 12:13:29 +0530 Subject: [PATCH 1/2] Added "GetAllKeys()" to continuously listen keys --- keyboard_common.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/keyboard_common.go b/keyboard_common.go index 20bd570..978b02f 100644 --- a/keyboard_common.go +++ b/keyboard_common.go @@ -229,3 +229,26 @@ func GetSingleKey() (ch rune, key Key, err error) { } return } + +func GetAllKeys() <-chan KeyEvent { + keyEventChan := make(chan KeyEvent) + + go func(chan<- KeyEvent) { + for { + ke := KeyEvent{} + + ke.Rune, ke.Key, ke.Err = GetKey() + + if ke.Err != nil { + keyEventChan <- ke + close(keyEventChan) + return + } + + keyEventChan <- ke + + } + }(keyEventChan) + + return keyEventChan +} From 5408ed65165ad57c2efab09e776c68af9ca9ce16 Mon Sep 17 00:00:00 2001 From: SakthiMahendran <84713203+SakthiMahendran@users.noreply.github.com> Date: Sun, 6 Nov 2022 12:32:11 +0530 Subject: [PATCH 2/2] Added example for "GetAllKeys()" function. --- README.md | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/README.md b/README.md index 653b0e4..25fd1fe 100644 --- a/README.md +++ b/README.md @@ -78,4 +78,42 @@ func main() { } } } + +``` +Example of getting continuous keystrokes with `GetAllKeys()` function: +```go +package main + +import ( + "fmt" + "github.com/eiannone/keyboard" +) + +func main() { + if err := keyboard.Open(); err != nil { + panic(err) + } + + defer func() { + _ = keyboard.Close() + }() + + fmt.Println("Press ESC to quit") + + keyEvents := keyboard.GetAllKeys() // return's a channel of "KeyEvent" + + for ev := range keyEvents { + if ev.Err != nil { + panic(err) + } + + fmt.Printf("You pressed: rune %q, key %X\r\n", ev.Rune, ev.Key) + + if key == keyboard.KeyEsc { + break + } + + } + +} ``` \ No newline at end of file