Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/gagliardetto/keyable
Callback-oriented wrapper around github.com/eiannone/keyboard
https://github.com/gagliardetto/keyable
Last synced: about 2 months ago
JSON representation
Callback-oriented wrapper around github.com/eiannone/keyboard
- Host: GitHub
- URL: https://github.com/gagliardetto/keyable
- Owner: gagliardetto
- Created: 2020-05-14T09:07:12.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2020-05-14T09:37:35.000Z (over 4 years ago)
- Last Synced: 2024-10-12T01:31:09.240Z (3 months ago)
- Language: Go
- Size: 1.95 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
## Install
```bash
go get -u github.com/gagliardetto/keyable
```## Why I wrote it
I needed to make things happen in a program **while** it was running.
Plus, I also didn't want to write a lot of `if` statements.
So I wrote `keyable` to have a clean interface for having callbacks
associated with keys or key combinations.## Usage
```golang
// Create a new Keyable object and add callbacks
// for the various characters and key combinations you send from
// the keyboard:
kb := keyable.New()// onStat will be the callback for information for your program
// (progress, etc.)
onStat := func() {
fmt.Println("some stats here")
// TODO: add stats
}
// onQuit is a callback for when you quit the program:
onQuit := func() {
fmt.Println("Exiting...")
// TODO: clean up, send exit signals, etc.
os.Exit(0)
}// Add the callbacks:
kb.
OnChar(
onStat,
'i',
).
OnKey(
onQuit,
keyboard.KeyEsc, keyboard.KeyCtrlC,
).
OnKey(
func() {
fmt.Println("FORCE-QUITTING...")
os.Exit(1)
},
keyboard.KeyCtrlF,
)
// Start the keyboard listener:
err := kb.Start()
if err != nil {
panic(err)
}
// Don't forget to stop it!
defer kb.Stop()```