How can I get keyboard input data in Go?

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

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. 如果你只想简单地读取字符串或字节,可以这样做:
in := bufio.NewScanner(os.Stdin)
data := in.Bytes()
  1. 如果你想捕获系统的信号量,可以通过注册信号量监视器来获取它:
signal.Notify(os.Interrupt, os.Kill) // os.Interrupt=0x2 os.Kill=0x9
  1. 从按键获取 ASCII 码(跨平台)(使用 github.com/nsf/termbox-go):
import (
	"fmt"
	term "github.com/nsf/termbox-go"
)

func main() {
	err := term.Init()
	if err != nil {
		panic(err)
	}
	defer term.Close()

	for {
		switch ev := term.PollEvent(); ev.Type {
		case term.EventKey:
			switch ev.Key {
			case term.KeyEsc:
				term.Sync()
				fmt.Println("ESC pressed")
			case term.KeyF1:
				term.Sync()
				fmt.Println("F1 pressed")
			case term.KeyInsert:
				term.Sync()
				fmt.Println("Insert pressed")
			case term.KeyDelete:
				term.Sync()
				fmt.Println("Delete pressed")
			case term.KeyHome:
				term.Sync()
				fmt.Println("Home pressed")
			case term.KeyEnd:
				term.Sync()
				fmt.Println("End pressed")
			case term.KeyPgup:
				term.Sync()
			case term.KeyArrowRight:
				term.Sync()
				fmt.Println("Arrow Right pressed")
			case term.KeySpace:
				term.Sync()
				fmt.Println("Space pressed")
			case term.KeyBackspace:
				term.Sync()
				fmt.Println("Backspace pressed")
			case term.KeyEnter:
				term.Sync()
				fmt.Println("Enter pressed")
			case term.KeyTab:
				term.Sync()
				fmt.Println("Tab pressed")

			default:
				term.Sync()
				fmt.Println("ASCII : ", ev.Ch)

			}
		case term.EventError:
			panic(ev.Err)
		}
	}
}
  1. 读取单个字符:
consoleReader := bufio.NewReaderSize(os.Stdin, 1)
input, _ := consoleReader.ReadByte()
ascii := input

// ESC = 27 and Ctrl-C = 3
if ascii == 27 || ascii == 3 {
	fmt.Println("Exiting...")
	os.Exit(0)
}

fmt.Println("ASCII : ", ascii)

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

英文:
  1. If you just want to simply read the string or bytes, you can do this
in := bufio.NewScanner(os.Stdin)
data := in.Bytes()
  1. If you want to capture the system semaphore, you can get it by registering the semaphore monitor
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)
import (
	&quot;fmt&quot;
	term &quot;github.com/nsf/termbox-go&quot;
)

func main() {
	err := term.Init()
	if err != nil {
		panic(err)
	}
	defer term.Close()

	for {
		switch ev := term.PollEvent(); ev.Type {
		case term.EventKey:
			switch ev.Key {
			case term.KeyEsc:
				term.Sync()
				fmt.Println(&quot;ESC pressed&quot;)
			case term.KeyF1:
				term.Sync()
				fmt.Println(&quot;F1 pressed&quot;)
			case term.KeyInsert:
				term.Sync()
				fmt.Println(&quot;Insert pressed&quot;)
			case term.KeyDelete:
				term.Sync()
				fmt.Println(&quot;Delete pressed&quot;)
			case term.KeyHome:
				term.Sync()
				fmt.Println(&quot;Home pressed&quot;)
			case term.KeyEnd:
				term.Sync()
				fmt.Println(&quot;End pressed&quot;)
			case term.KeyPgup:
				term.Sync()
			case term.KeyArrowRight:
				term.Sync()
				fmt.Println(&quot;Arrow Right pressed&quot;)
			case term.KeySpace:
				term.Sync()
				fmt.Println(&quot;Space pressed&quot;)
			case term.KeyBackspace:
				term.Sync()
				fmt.Println(&quot;Backspace pressed&quot;)
			case term.KeyEnter:
				term.Sync()
				fmt.Println(&quot;Enter pressed&quot;)
			case term.KeyTab:
				term.Sync()
				fmt.Println(&quot;Tab pressed&quot;)

			default:
				term.Sync()
				fmt.Println(&quot;ASCII : &quot;, ev.Ch)

			}
		case term.EventError:
			panic(ev.Err)
		}
	}
}
  1. Read single characters
consoleReader := bufio.NewReaderSize(os.Stdin, 1)
input, _ := consoleReader.ReadByte()
ascii := input

// ESC = 27 and Ctrl-C = 3
if ascii == 27 || ascii == 3 {
	fmt.Println(&quot;Exiting...&quot;)
	os.Exit(0)
}

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:

确定