Suggest a feature
×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Go
Posted by: matias s..
Added: Sep 28, 2020 4:39 AM
Views: 54
  1. package main
  2.  
  3. import (
  4.         "fmt"
  5.         "os"
  6.         "os/exec"
  7. )
  8.  
  9. //ReadKey ReadKey
  10. func main() {
  11.         ch := make(chan string)
  12.         go func(ch chan string) {
  13.                 // disable input buffering
  14.                 exec.Command("stty", "-F", "/dev/tty", "cbreak", "min", "1").Run()
  15.                 // do not display entered characters on the screen
  16.                 exec.Command("stty", "-F", "/dev/tty", "-echo").Run()
  17.                 var b = make([]byte, 1)
  18.                 for {
  19.                         os.Stdin.Read(b)
  20.                         ch <- string(b)
  21.                 }
  22.         }(ch)
  23.  
  24.         for {
  25.                 stdin, _ := <-ch
  26.  
  27.                 fmt.Println("Keys pressed:", stdin)
  28.  
  29.         }
  30.  
  31. }
  32.  
Comments disabled