Get The Module | Documentation | Contributing | Code of Conduct
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. It can also be used to simulate (mock) keypresses for CI testing.
Works nicely with https://atomicgo.dev/cursor
keyboard.Listen(func(key keys.Key) (stopbool, errerror) {
ifkey.Code==keys.CtrlC {
returntrue, nil// Stop listener by returning true on Ctrl+C
}
fmt.Println("\r"+key.String()) // Print every key pressreturnfalse, nil// Return false to continue listening
})// 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) (stopbool, errerror) {
switchkey.Code {
casekeys.CtrlC, keys.Escape:
returntrue, nil// Return true to stop listenercasekeys.RuneKey: // Check if key is a rune key (a, b, c, 1, 2, 3, ...)ifkey.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)
}
returnfalse, nil// Return false to continue listening
})gofunc() {
keyboard.SimulateKeyPress("Hello") // Simulate key press for every letter in stringkeyboard.SimulateKeyPress(keys.Enter) // Simulate key press for Enterkeyboard.SimulateKeyPress(keys.CtrlShiftRight) // Simulate key press for Ctrl+Shift+Rightkeyboard.SimulateKeyPress('x') // Simulate key press for a single runekeyboard.SimulateKeyPress('x', keys.Down, 'a') // Simulate key presses for multiple inputskeyboard.SimulateKeyPress(keys.Escape) // Simulate key press for Escape, which quits the program
}()
keyboard.Listen(func(key keys.Key) (stopbool, errerror) {
ifkey.Code==keys.Escape||key.Code==keys.CtrlC {
os.Exit(0) // Exit program on Escape
}
fmt.Println("\r"+key.String()) // Print every key pressreturnfalse, nil// Return false to continue listening
})funcListen(onKeyPressfunc(key keys.Key) (stopbool, errerror)) errorListen calls a callback function when a key is pressed.
Simple example:
keyboard.Listen(func(key keys.Key) (stopbool, errerror) {
ifkey.Code==keys.CtrlC {
returntrue, nil// Stop listener by returning true on Ctrl+C
}
fmt.Println("\r"+key.String()) // Print every key pressreturnfalse, nil// Return false to continue listening
})funcSimulateKeyPress(input...interface{}) errorSimulateKeyPress simulate a key press. It can be used to mock user input and test your application.
Example:
gofunc() {
keyboard.SimulateKeyPress("Hello") // Simulate key press for every letter in stringkeyboard.SimulateKeyPress(keys.Enter) // Simulate key press for Enterkeyboard.SimulateKeyPress(keys.CtrlShiftRight) // Simulate key press for Ctrl+Shift+Rightkeyboard.SimulateKeyPress('x') // Simulate key press for a single runekeyboard.SimulateKeyPress('x', keys.Down, 'a') // Simulate key presses for multiple inputs
}()AtomicGo.dev · with ❤️ by @MarvinJWendt | MarvinJWendt.com
