58 lines
1.1 KiB
Go
58 lines
1.1 KiB
Go
package raw
|
|
|
|
import (
|
|
"io"
|
|
"log"
|
|
"os"
|
|
"os/exec"
|
|
"runtime"
|
|
)
|
|
|
|
type Keyboard struct {
|
|
}
|
|
|
|
func NewKeyboard() Keyboard {
|
|
switch runtime.GOOS {
|
|
case "linux":
|
|
if err := exec.Command("stty", "-F", "/dev/tty", "cbreak", "min", "1").Run(); err != nil {
|
|
panic(err)
|
|
} else if err := exec.Command("stty", "-F", "/dev/tty", "-echo").Run(); err != nil {
|
|
panic(err)
|
|
}
|
|
case "darwin":
|
|
if err := exec.Command("stty", "-f", "/dev/tty", "cbreak", "min", "1").Run(); err != nil {
|
|
panic(err)
|
|
} else if err := exec.Command("stty", "-f", "/dev/tty", "-echo").Run(); err != nil {
|
|
panic(err)
|
|
}
|
|
default:
|
|
panic(runtime.GOOS)
|
|
}
|
|
return Keyboard{}
|
|
}
|
|
|
|
func (kb Keyboard) Close() {
|
|
switch runtime.GOOS {
|
|
case "linux":
|
|
if err := exec.Command("stty", "-F", "/dev/tty", "echo").Run(); err != nil {
|
|
panic(err)
|
|
}
|
|
case "darwin":
|
|
if err := exec.Command("stty", "-f", "/dev/tty", "echo").Run(); err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func (kb Keyboard) Read() []byte {
|
|
b := make([]byte, 5)
|
|
n, err := os.Stdin.Read(b)
|
|
if err != nil && err != io.EOF {
|
|
panic(err)
|
|
}
|
|
if FlagDebug {
|
|
log.Printf("raw.Keyboard.Read() %s", b[:n])
|
|
}
|
|
return b[:n]
|
|
}
|