Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Listen and SimulateKeyPress #1

Merged
merged 4 commits into from
Jun 2, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
docs: autoupdate
  • Loading branch information
MarvinJWendt committed Jun 2, 2022
commit 6d78b6618aec574362bdd5a88dba15468835a20d
98 changes: 77 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,48 +74,104 @@

Package keyboard can be used to read key presses from the keyboard, while in a
terminal application. It's crossplatform and keypresses can be combined to check
for ctrl+c, alt+4, ctrl-shift, alt+ctrl+right, etc.
for ctrl+c, alt+4, ctrl-shift, alt+ctrl+right, etc. It can also be used to
simulate (mock) keypresses for CI testing.

Works nicely with https://atomicgo.dev/cursor

```go

keyboard.StartListener()
defer keyboard.StopListener()
## Simple Usage

for {
keyInfo, _ := keyboard.GetKey()
key := keyInfo.Code
keyboard.Listen(func(key keys.Key) (stop bool, err error) {
if key.Code == keys.CtrlC {
return true, nil // Stop listener by returning true on Ctrl+C
}

if key == keys.CtrlC {
break
fmt.Println("\r" + key.String()) // Print every key press
return false, nil // Return false to continue listening
})

## Advanced Usage

// Stop keyboard listener on Escape key press or CTRL+C.
// Exit application on "q" key press.
// Print every rune key press.
// Print every other key press.
keyboard.Listen(func(key keys.Key) (stop bool, err error) {
switch key.Code {
case keys.CtrlC, keys.Escape:
return true, nil // Return true to stop listener
case keys.RuneKey: // Check if key is a rune key (a, b, c, 1, 2, 3, ...)
if key.String() == "q" { // Check if key is "q"
fmt.Println("\rQuitting application")
os.Exit(0) // Exit application
}
fmt.Printf("\rYou pressed the rune key: %s\n", key)
default:
fmt.Printf("\rYou pressed: %s\n", key)
}

fmt.Println("\r", keyInfo.String())
}
return false, nil // Return false to continue listening
})

```
## Simulate Key Presses (for mocking in tests)

go func() {
keyboard.SimulateKeyPress("Hello") // Simulate key press for every letter in string
keyboard.SimulateKeyPress(keys.Enter) // Simulate key press for Enter
keyboard.SimulateKeyPress(keys.CtrlShiftRight) // Simulate key press for Ctrl+Shift+Right
keyboard.SimulateKeyPress('x') // Simulate key press for a single rune
keyboard.SimulateKeyPress('x', keys.Down, 'a') // Simulate key presses for multiple inputs

## Usage
keyboard.SimulateKeyPress(keys.Escape) // Simulate key press for Escape, which quits the program
}()

#### func GetListener
keyboard.Listen(func(key keys.Key) (stop bool, err error) {
if key.Code == keys.Escape || key.Code == keys.CtrlC {
os.Exit(0) // Exit program on Escape
}

```go
func GetListener() (listener chan keys.Key, cancel chan bool)
```
fmt.Println("\r" + key.String()) // Print every key press
return false, nil // Return false to continue listening
})


## Usage

#### func Listen

```go
func Listen(onKeyPress func(keyInfo keys.Key) (stop bool, err error)) error
func Listen(onKeyPress func(key keys.Key) (stop bool, err error)) error
```
Listen calls a callback function when a key is pressed.

Simple example:

keyboard.Listen(func(key keys.Key) (stop bool, err error) {
if key.Code == keys.CtrlC {
return true, nil // Stop listener by returning true on Ctrl+C
}

fmt.Println("\r" + key.String()) // Print every key press
return false, nil // Return false to continue listening
})

#### func MockKey
#### func SimulateKeyPress

```go
func MockKey(key keys.Key) error
func SimulateKeyPress(input ...interface{}) error
```
SimulateKeyPress simulate a key press. It can be used to mock user input and
test your application.

Example:

go func() {
keyboard.SimulateKeyPress("Hello") // Simulate key press for every letter in string
keyboard.SimulateKeyPress(keys.Enter) // Simulate key press for Enter
keyboard.SimulateKeyPress(keys.CtrlShiftRight) // Simulate key press for Ctrl+Shift+Right
keyboard.SimulateKeyPress('x') // Simulate key press for a single rune
keyboard.SimulateKeyPress('x', keys.Down, 'a') // Simulate key presses for multiple inputs
}()

---

Expand Down