Skip to content

Commit 1cabaab

Browse files
authored
Merge pull request magicmonkey#27 from lornajane/add-screenshot-example
Add an example of a custom action handler
2 parents 172d816 + 5f45f95 commit 1cabaab

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

examples/screenshot-command/main.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"io/ioutil"
6+
"os/exec"
7+
"sync"
8+
9+
"github.com/magicmonkey/go-streamdeck"
10+
"github.com/magicmonkey/go-streamdeck/actionhandlers"
11+
"github.com/magicmonkey/go-streamdeck/buttons"
12+
// needed to get the device definitions
13+
_ "github.com/magicmonkey/go-streamdeck/devices"
14+
)
15+
16+
func main() {
17+
// add a streamdeck
18+
sd, err := streamdeck.New()
19+
if err != nil {
20+
panic(err)
21+
}
22+
23+
// create a text button labelled "Pic"
24+
myButton := buttons.NewTextButton("Pic")
25+
26+
// create a custom action with a function as the action handler
27+
shotaction := &actionhandlers.CustomAction{}
28+
shotaction.SetHandler(func(btn streamdeck.Button) {
29+
// a goroutine so that we don't wait for the command to return
30+
go takeScreenshot()
31+
})
32+
33+
// attach action to button
34+
myButton.SetActionHandler(shotaction)
35+
// put button in top left slot
36+
sd.AddButton(0, myButton)
37+
38+
// now run and keep running
39+
var wg sync.WaitGroup
40+
wg.Add(1)
41+
wg.Wait()
42+
}
43+
44+
func takeScreenshot() {
45+
fmt.Println("Taking screenshot with delay...")
46+
cmd := exec.Command("/usr/bin/gnome-screenshot", "-w", "-d", "2")
47+
stderr, _ := cmd.StderrPipe()
48+
stdout, _ := cmd.StdoutPipe()
49+
if err := cmd.Run(); err != nil {
50+
panic(err)
51+
}
52+
53+
slurp, _ := ioutil.ReadAll(stderr)
54+
fmt.Printf("%s\n", slurp)
55+
slurp2, _ := ioutil.ReadAll(stdout)
56+
fmt.Printf("%s\n", slurp2)
57+
58+
fmt.Println("Taken screenshot")
59+
}

0 commit comments

Comments
 (0)