AtomicGo | keyboard

Latest Release Tests Coverage Unit test count Issues License: MIT


| Documentation | Contributing | Code of Conduct


Description

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.

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

    keyboard.StartListener()
    defer keyboard.StopListener()

    for {
    	keyInfo, _ := keyboard.GetKey()
    	key := keyInfo.Code

    	if key == keys.CtrlC {
    		break
    	}

    	fmt.Println("\r", keyInfo.String())
    }

Install

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

go get atomicgo.dev/keyboard

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

// Add this to your imports
import "atomicgo.dev/keyboard"

Usage

func GetKey

func GetKey() (keys.Key, error)

GetKey blocks until a key is pressed and returns the key info.

Example:

keyboard.StartListener()

for {
  keyInfo, _ := keyboard.GetKey()
  key := keyInfo.Code

  if key == keys.CtrlC {
    break
  }

  fmt.Println("\r", keyInfo.String())
}

keyboard.StopListener()

func StartListener

func StartListener() error

StartListener starts the keyboard listener

Example:

keyboard.StartListener()

for {
  keyInfo, _ := keyboard.GetKey()
  key := keyInfo.Code

  if key == keys.CtrlC {
    break
  }

  fmt.Println("\r", keyInfo.String())
}

keyboard.StopListener()

func StopListener

func StopListener() error

StopListener stops the keyboard listener

Example:

keyboard.StartListener()

for {
  keyInfo, _ := keyboard.GetKey()
  key := keyInfo.Code

  if key == keys.CtrlC {
    break
  }

  fmt.Println("\r", keyInfo.String())
}

keyboard.StopListener()

 ·  with ❤️ by @MarvinJWendt | MarvinJWendt.com

GitHub

View Github