package internal import ( "encoding/json" "log" "runtime" "time" "github.com/jcollie/go-streamdeck" "github.com/micmonay/keybd_event" ) // MacroButton . type MacroButton struct { kb keybd_event.KeyBonding kbc chan rune sd *streamdeck.StreamDeck Button ButtonInfo Icon StaticIconInfo Service ServiceInfo } // NewMacroButton . func NewMacroButton( sd *streamdeck.StreamDeck, button ButtonInfo, icon StaticIconInfo, ) *MacroButton { b := new(MacroButton) b.sd = sd b.Button = button b.Icon = icon myButtons = append(myButtons, b) sd.SetCallback(b.Button.X, b.Button.Y, b) return b } // InitializeButton . func (b *MacroButton) InitializeButton() { b.sd.FillIcon(b.Button.X, b.Button.Y, streamdeck.IconButton{ Icon: b.Icon.Icon, IconColor: b.Icon.FgColor, BackgroundColor: b.Icon.BgColor, }) kb, err := keybd_event.NewKeyBonding() if err != nil { panic(err) } b.kb = kb b.kbc = make(chan rune) go b.HandlePresses() } // StateChange . func (b *MacroButton) StateChange(entityID string, newState string) { // do nothing } // ButtonPressed . func (b *MacroButton) ButtonPressed(sd *streamdeck.StreamDeck, x int, y int, timestamp time.Time) { log.Printf("pressed %d %d %s\n", x, y, timestamp) b.sd.FillIcon(b.Button.X, b.Button.Y, streamdeck.IconButton{ Icon: b.Icon.Icon, IconColor: b.Icon.PressedFgColor, BackgroundColor: b.Icon.PressedBgColor, }, ) } // ButtonReleased . func (b *MacroButton) ButtonReleased(sd *streamdeck.StreamDeck, x int, y int, timestamp time.Time) { log.Printf("released %d %d %s\n", x, y, timestamp) b.sd.FillIcon(b.Button.X, b.Button.Y, streamdeck.IconButton{ Icon: b.Icon.Icon, IconColor: b.Icon.FgColor, BackgroundColor: b.Icon.BgColor, }, ) for _, r := range "/kill @e[type=enderman]" { b.kbc <- r } } // HandlePresses . func (b *MacroButton) HandlePresses() { // For linux, it is very important to wait 2 seconds if runtime.GOOS == "linux" { time.Sleep(2 * time.Second) } for r := range b.kbc { switch r { case ' ': b.kb.SetKeys(keybd_event.VK_SPACE) case '=': b.kb.SetKeys(keybd_event.VK_EQUAL) case '@': b.kb.SetKeys(keybd_event.VK_2) b.kb.HasSHIFT(true) case '/': b.kb.SetKeys(keybd_event.VK_SLASH) case '[': b.kb.SetKeys(keybd_event.VK_LEFTBRACE) case ']': b.kb.SetKeys(keybd_event.VK_RIGHTBRACE) case 'a': b.kb.SetKeys(keybd_event.VK_A) case 'b': b.kb.SetKeys(keybd_event.VK_B) case 'c': b.kb.SetKeys(keybd_event.VK_C) case 'd': b.kb.SetKeys(keybd_event.VK_D) case 'e': b.kb.SetKeys(keybd_event.VK_E) case 'f': b.kb.SetKeys(keybd_event.VK_F) case 'g': b.kb.SetKeys(keybd_event.VK_G) case 'h': b.kb.SetKeys(keybd_event.VK_H) case 'i': b.kb.SetKeys(keybd_event.VK_I) case 'j': b.kb.SetKeys(keybd_event.VK_J) case 'k': b.kb.SetKeys(keybd_event.VK_K) case 'l': b.kb.SetKeys(keybd_event.VK_L) case 'm': b.kb.SetKeys(keybd_event.VK_M) case 'n': b.kb.SetKeys(keybd_event.VK_N) case 'o': b.kb.SetKeys(keybd_event.VK_O) case 'p': b.kb.SetKeys(keybd_event.VK_P) case 'q': b.kb.SetKeys(keybd_event.VK_Q) case 'r': b.kb.SetKeys(keybd_event.VK_R) case 's': b.kb.SetKeys(keybd_event.VK_S) case 't': b.kb.SetKeys(keybd_event.VK_T) case 'u': b.kb.SetKeys(keybd_event.VK_U) case 'v': b.kb.SetKeys(keybd_event.VK_V) case 'w': b.kb.SetKeys(keybd_event.VK_W) case 'x': b.kb.SetKeys(keybd_event.VK_X) case 'y': b.kb.SetKeys(keybd_event.VK_Y) case 'z': b.kb.SetKeys(keybd_event.VK_Z) } err := b.kb.Press() if err != nil { log.Printf("error sending keyboard event: %+v", err) } time.Sleep(10 * time.Millisecond) err = b.kb.Release() if err != nil { log.Printf("error sending keyboard event: %+v", err) } b.kb.Clear() time.Sleep(30 * time.Millisecond) } } // HandleResult . func (b *MacroButton) HandleResult(id uint64, success bool, result json.RawMessage) { log.Printf("result: %v %s\n", success, string(result)) } // HandleClose . func (b *MacroButton) HandleClose(id uint64) { log.Printf("home assistant closed") }