How can I get keyboard input data in Go?

huangapple go评论117阅读模式
英文:

How can I get keyboard input data in Go?

问题

我想要能够在Go语言中判断用户是否按下类似<kbd>Ctrl</kbd>+[某些键]或<kbd>Esc</kbd>这样的键盘按键。该程序在终端中运行于Linux环境中。

英文:

I want to be able to tell if a user presses things like <kbd>Ctrl</kbd>+[something] or <kbd>Esc</kbd> on their keyboard in Go. The program runs in a Linux environment inside a terminal.

答案1

得分: 3

  1. 如果你只想简单地读取字符串或字节,可以这样做:
  1. in := bufio.NewScanner(os.Stdin)
  2. data := in.Bytes()
  1. 如果你想捕获系统的信号量,可以通过注册信号量监视器来获取它:
  1. signal.Notify(os.Interrupt, os.Kill) // os.Interrupt=0x2 os.Kill=0x9
  1. 从按键获取 ASCII 码(跨平台)(使用 github.com/nsf/termbox-go):
  1. import (
  2. "fmt"
  3. term "github.com/nsf/termbox-go"
  4. )
  5. func main() {
  6. err := term.Init()
  7. if err != nil {
  8. panic(err)
  9. }
  10. defer term.Close()
  11. for {
  12. switch ev := term.PollEvent(); ev.Type {
  13. case term.EventKey:
  14. switch ev.Key {
  15. case term.KeyEsc:
  16. term.Sync()
  17. fmt.Println("ESC pressed")
  18. case term.KeyF1:
  19. term.Sync()
  20. fmt.Println("F1 pressed")
  21. case term.KeyInsert:
  22. term.Sync()
  23. fmt.Println("Insert pressed")
  24. case term.KeyDelete:
  25. term.Sync()
  26. fmt.Println("Delete pressed")
  27. case term.KeyHome:
  28. term.Sync()
  29. fmt.Println("Home pressed")
  30. case term.KeyEnd:
  31. term.Sync()
  32. fmt.Println("End pressed")
  33. case term.KeyPgup:
  34. term.Sync()
  35. case term.KeyArrowRight:
  36. term.Sync()
  37. fmt.Println("Arrow Right pressed")
  38. case term.KeySpace:
  39. term.Sync()
  40. fmt.Println("Space pressed")
  41. case term.KeyBackspace:
  42. term.Sync()
  43. fmt.Println("Backspace pressed")
  44. case term.KeyEnter:
  45. term.Sync()
  46. fmt.Println("Enter pressed")
  47. case term.KeyTab:
  48. term.Sync()
  49. fmt.Println("Tab pressed")
  50. default:
  51. term.Sync()
  52. fmt.Println("ASCII : ", ev.Ch)
  53. }
  54. case term.EventError:
  55. panic(ev.Err)
  56. }
  57. }
  58. }
  1. 读取单个字符:
  1. consoleReader := bufio.NewReaderSize(os.Stdin, 1)
  2. input, _ := consoleReader.ReadByte()
  3. ascii := input
  4. // ESC = 27 and Ctrl-C = 3
  5. if ascii == 27 || ascii == 3 {
  6. fmt.Println("Exiting...")
  7. os.Exit(0)
  8. }
  9. fmt.Println("ASCII : ", ascii)

Golang 从标准输入读取
从按键获取 ASCII 码

英文:
  1. If you just want to simply read the string or bytes, you can do this
  1. in := bufio.NewScanner(os.Stdin)
  2. data := in.Bytes()
  1. If you want to capture the system semaphore, you can get it by registering the semaphore monitor
  1. signal.Notify(os.Interrupt, os.Kill) // os.Interrupt=0x2 os.Kill=0x9
  1. Get ASCII code from a key press(cross-platform) (use github.com/nsf/termbox-go)
  1. import (
  2. &quot;fmt&quot;
  3. term &quot;github.com/nsf/termbox-go&quot;
  4. )
  5. func main() {
  6. err := term.Init()
  7. if err != nil {
  8. panic(err)
  9. }
  10. defer term.Close()
  11. for {
  12. switch ev := term.PollEvent(); ev.Type {
  13. case term.EventKey:
  14. switch ev.Key {
  15. case term.KeyEsc:
  16. term.Sync()
  17. fmt.Println(&quot;ESC pressed&quot;)
  18. case term.KeyF1:
  19. term.Sync()
  20. fmt.Println(&quot;F1 pressed&quot;)
  21. case term.KeyInsert:
  22. term.Sync()
  23. fmt.Println(&quot;Insert pressed&quot;)
  24. case term.KeyDelete:
  25. term.Sync()
  26. fmt.Println(&quot;Delete pressed&quot;)
  27. case term.KeyHome:
  28. term.Sync()
  29. fmt.Println(&quot;Home pressed&quot;)
  30. case term.KeyEnd:
  31. term.Sync()
  32. fmt.Println(&quot;End pressed&quot;)
  33. case term.KeyPgup:
  34. term.Sync()
  35. case term.KeyArrowRight:
  36. term.Sync()
  37. fmt.Println(&quot;Arrow Right pressed&quot;)
  38. case term.KeySpace:
  39. term.Sync()
  40. fmt.Println(&quot;Space pressed&quot;)
  41. case term.KeyBackspace:
  42. term.Sync()
  43. fmt.Println(&quot;Backspace pressed&quot;)
  44. case term.KeyEnter:
  45. term.Sync()
  46. fmt.Println(&quot;Enter pressed&quot;)
  47. case term.KeyTab:
  48. term.Sync()
  49. fmt.Println(&quot;Tab pressed&quot;)
  50. default:
  51. term.Sync()
  52. fmt.Println(&quot;ASCII : &quot;, ev.Ch)
  53. }
  54. case term.EventError:
  55. panic(ev.Err)
  56. }
  57. }
  58. }
  1. Read single characters
  1. consoleReader := bufio.NewReaderSize(os.Stdin, 1)
  2. input, _ := consoleReader.ReadByte()
  3. ascii := input
  4. // ESC = 27 and Ctrl-C = 3
  5. if ascii == 27 || ascii == 3 {
  6. fmt.Println(&quot;Exiting...&quot;)
  7. os.Exit(0)
  8. }
  9. fmt.Println(&quot;ASCII : &quot;, ascii)

> Golang reading from stdin
> Get ASCII code from a key press

huangapple
  • 本文由 发表于 2021年12月21日 13:32:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/70431051.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定