I want to read the clipboard data and paste it in the buffer/ scanf so that I can read the data parse accordingly. My application is entirely written in go as a CLI app.
I used the https://github.com/atotto/clipboard to read the data from the clipboard but now I want this function to be invoked only when the user triggers CRTL+V or 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)
}
Use https://github.com/nsf/termbox-go for keyboard event processing.
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)
}
}
}