Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/eiannone/keyboard
Simple library to listen for keystrokes from the keyboard
https://github.com/eiannone/keyboard
Last synced: 30 days ago
JSON representation
Simple library to listen for keystrokes from the keyboard
- Host: GitHub
- URL: https://github.com/eiannone/keyboard
- Owner: eiannone
- License: mit
- Created: 2015-10-25T11:53:59.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2024-07-03T00:27:25.000Z (5 months ago)
- Last Synced: 2024-08-04T01:19:37.755Z (4 months ago)
- Language: Go
- Homepage:
- Size: 39.1 KB
- Stars: 564
- Watchers: 12
- Forks: 57
- Open Issues: 21
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Authors: AUTHORS
Awesome Lists containing this project
README
# Keyboard
Simple library to listen for keystrokes from the keyboardThe code is inspired by [termbox-go](https://github.com/nsf/termbox-go) library.
### Installation
Install and update this go package with `go get -u github.com/eiannone/keyboard`### Usage
Example of getting a single keystroke:```go
char, _, err := keyboard.GetSingleKey()
if (err != nil) {
panic(err)
}
fmt.Printf("You pressed: %q\r\n", char)
```Example of getting a series of keystrokes with a blocking `GetKey()` function:
```go
package mainimport (
"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")
for {
char, key, err := keyboard.GetKey()
if err != nil {
panic(err)
}
fmt.Printf("You pressed: rune %q, key %X\r\n", char, key)
if key == keyboard.KeyEsc {
break
}
}
}
```Example of getting a series of keystrokes using a channel:
```go
package mainimport (
"fmt"
"github.com/eiannone/keyboard"
)func main() {
keysEvents, err := keyboard.GetKeys(10)
if err != nil {
panic(err)
}
defer func() {
_ = keyboard.Close()
}()fmt.Println("Press ESC to quit")
for {
event := <-keysEvents
if event.Err != nil {
panic(event.Err)
}
fmt.Printf("You pressed: rune %q, key %X\r\n", event.Rune, event.Key)
if event.Key == keyboard.KeyEsc {
break
}
}
}
```