我想读取剪贴板数据并将其粘贴到 buffer/scanf 中,以便我可以相应地读取数据解析。我的应用程序完全作为 CLI 应用程序用 go 编写。
我使用了 https://github.com/atotto/clipboard从剪贴板读取数据,但现在我希望仅在用户触发 CRTL+V 或 SHIFT+INSERT 时调用此函数。
package main
import (
"fmt"
"github.com/atotto/clipboard"
)
func main() {
// I want this module to be invoked only when user clicks CTRL+V or SHIFT+INSERT
text, err := clipboard.ReadAll()
if err != nil {
fmt.Println(err)
return
}
fmt.Println(text)
}
最佳答案
使用https://github.com/nsf/termbox-go用于键盘事件处理。
package main
import (
"fmt"
"github.com/atotto/clipboard"
termbox "github.com/nsf/termbox-go"
)
func main() {
err := termbox.Init()
if err != nil {
panic(err)
}
defer termbox.Close()
// clear
termbox.Flush()
loop:
for {
switch ev := termbox.PollEvent(); ev.Type {
case termbox.EventKey:
// program exit
if ev.Key == termbox.KeyCtrlX {
break loop
}
// past clipboard
if ev.Key == termbox.KeyCtrlV {
text, err := clipboard.ReadAll()
if err == nil {
fmt.Println(text)
}
}
termbox.Flush()
case termbox.EventError:
panic(ev.Err)
}
}
}
关于go - 是否有任何系统调用可以在 golang 中捕获 ctrl+v 或 shift+insert?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55333215/