-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
138 lines (124 loc) · 2.89 KB
/
main.go
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package main
import (
"log"
"os"
"syscall"
"time"
"unsafe"
ps "github.com/mitchellh/go-ps"
)
var logger = log.Default()
const (
ModAlt = 1 << iota
ModCtrl
ModShift
ModWin
)
type Hotkey struct {
Id int // Unique id
Modifiers int // Mask of modifiers
KeyCode int // Key code, e.g. 'A'
}
type MSG struct {
HWND uintptr
UINT uintptr
WPARAM int16
LPARAM int64
DWORD int32
POINT struct{ X, Y int64 }
}
func main() {
logger.Println("This script was created by ThEldeRS - A.K.A Hans.")
logger.Println("To stop the execution press CTRL+SHIFT+Q")
user32 := syscall.MustLoadDLL("user32")
keys := registerKeyboardShortcut(user32)
peekmsg := user32.MustFindProc("PeekMessageW")
ticker := time.NewTicker(5 * time.Second)
done := make(chan bool)
defer func() {
user32.Release()
logger.Println("Released DLL")
ticker.Stop()
logger.Println("Deregistered timer")
logger.Println("Thank you for using this script! Follow me on https://twitch.tv/ThEldeRS")
time.Sleep(5 * time.Second)
}()
go handleWork(ticker, done)
for {
var msg = &MSG{}
peekmsg.Call(uintptr(unsafe.Pointer(msg)), 0, 0, 0, 1)
// Registered id is in the WPARAM field:
if id := msg.WPARAM; id != 0 {
logger.Println("Hotkey pressed:", keys[id])
if id == 1 { // CTRL+SHIFT+Q = Exit
done <- true
return
}
}
}
}
func registerKeyboardShortcut(user32 *syscall.DLL) map[int16]*Hotkey {
reghotkey := user32.MustFindProc("RegisterHotKey")
keys := map[int16]*Hotkey{
1: {1, ModShift + ModCtrl, 'Q'}, // SHIFT+CTRL+Q
}
for _, v := range keys {
r1, _, err := reghotkey.Call(
0, uintptr(v.Id), uintptr(v.Modifiers), uintptr(v.KeyCode))
if r1 == 1 {
logger.Println("Registered", v)
} else {
logger.Println("Failed to register", v, ", error:", err)
}
}
return keys
}
func handleWork(t *time.Ticker, c chan bool) {
for {
select {
case <-c:
return
case <-t.C:
pID, err := getApplicationFrameHostPID()
if err != nil {
logger.Println(err)
logger.Println("Trying again in 1 second...")
}
if pID != 0 {
err = closeApplicationFrameHost(pID)
if err != nil {
logger.Println(err)
}
}
}
}
}
func closeApplicationFrameHost(pId int) error {
process, err := os.FindProcess(pId)
if err != nil {
return err
}
err = process.Kill()
if err != nil {
return err
}
logger.Println("Application Frame Host successfully exited!")
return nil
}
func getApplicationFrameHostPID() (int, error) {
processes, err := ps.Processes()
var applicationFrameHostPID int
if err != nil {
return applicationFrameHostPID, err
}
for _, v := range processes {
if v.Executable() == "ApplicationFrameHost.exe" {
applicationFrameHostPID = v.Pid()
logger.Println("Found ApplicationFrameHost.exe PID: ", applicationFrameHostPID)
}
}
if applicationFrameHostPID == 0 {
logger.Println("ApplicationFrameHost.exe is not running")
}
return applicationFrameHostPID, nil
}