1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
| package main
import ( "flag" "fmt" "log" "net/http"
"github.com/go-vgo/robotgo" "github.com/gorilla/websocket" hook "github.com/robotn/gohook" )
var addr = flag.String("addr", "localhost:7788", "http service address") var MESSAGE = make(chan string, 1) var HUB = map[string]*websocket.Conn{}
const ( PLAY = "play" FORWARD = "forward" BACKWARD = "backward" )
var upgrader = websocket.Upgrader{ CheckOrigin: func(r *http.Request) bool { return true }, }
func control(w http.ResponseWriter, r *http.Request) { c, _ := upgrader.Upgrade(w, r, nil) key := c.RemoteAddr().String() HUB[key] = c defer func() { delete(HUB, key) c.Close() }() for { _, _, err := c.ReadMessage() if err != nil { break } } }
func sendMessage() { for i := range MESSAGE { for _, v := range HUB { v.WriteMessage(websocket.TextMessage, []byte(i)) } } }
func hotkey() { go func() { robotgo.EventHook(hook.KeyDown, []string{"alt", "left"}, func(e hook.Event) { fmt.Println("left") if len(MESSAGE) == 0 { MESSAGE <- BACKWARD } })
robotgo.EventHook(hook.KeyDown, []string{"alt", "right"}, func(e hook.Event) { fmt.Println("right") if len(MESSAGE) == 0 { MESSAGE <- FORWARD } })
robotgo.EventHook(hook.KeyDown, []string{"alt", "up"}, func(e hook.Event) { fmt.Println("up") if len(MESSAGE) == 0 { MESSAGE <- PLAY } })
s := robotgo.EventStart() <-robotgo.EventProcess(s) }() }
func main() { go sendMessage() hotkey() flag.Parse() log.SetFlags(0) http.HandleFunc("/control", control) log.Fatal(http.ListenAndServe(*addr, nil)) }
|