-
Notifications
You must be signed in to change notification settings - Fork 0
/
mouse.go
76 lines (65 loc) · 2.23 KB
/
mouse.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
package main
import (
"fmt"
"strconv"
"time"
"github.com/dakaraphi/mouse-monitor/winapi"
"github.com/fatih/color"
)
const messagesPerFrequencyCalculation = 100
type MouseData struct {
xPos int32
yPos int32
msgCount int64
msgHz float64
msgHzLastTime time.Time
msgHzLastCount int64
}
var displaySignal chan bool
var mouseTracking MouseData
func handleMouseInput(mouseInput winapi.RAWMOUSE) {
mouseTracking.xPos += mouseInput.LastX
mouseTracking.yPos += mouseInput.LastY
mouseTracking.msgCount++
if mouseInput.ButtonData == 1 {
displaySignal <- true // send display notification
<-displaySignal // wait for display complete
mouseTracking = MouseData{}
} else if mouseTracking.msgCount > mouseTracking.msgHzLastCount+messagesPerFrequencyCalculation {
currentTime := time.Now()
currentHz := float64(1) / (currentTime.Sub(mouseTracking.msgHzLastTime).Seconds() / messagesPerFrequencyCalculation)
if currentHz > mouseTracking.msgHz {
mouseTracking.msgHz = currentHz
}
mouseTracking.msgHzLastTime = currentTime
mouseTracking.msgHzLastCount = mouseTracking.msgCount
}
}
func consoleDisplayLoop() {
valuesToColorStrings := func() (string, string, string) {
x := color.GreenString(strconv.FormatInt(int64(mouseTracking.xPos), 10))
y := color.GreenString(strconv.FormatInt(int64(mouseTracking.yPos), 10))
hz := color.CyanString(strconv.FormatFloat(mouseTracking.msgHz, 'f', 2, 64))
return x, y, hz
}
for {
select {
case <-time.After(time.Millisecond * 50):
x, y, hz := valuesToColorStrings()
fmt.Fprintf(color.Output, "current position: x %v y %v max update Hz %v \r", x, y, hz)
case <-displaySignal:
x, y, hz := valuesToColorStrings()
fmt.Fprintf(color.Output, "position delta: x %v y %v max update Hz %v \r\n", x, y, hz)
displaySignal <- true
}
}
}
func main() {
fmt.Println("Mouse Monitor v1.0 - https://github.com/dakaraphi/mouse-monitor")
fmt.Println("Left click to print current relative position and set new starting position for measurement")
displaySignal = make(chan bool)
mouseTracking.msgHzLastTime = time.Now()
go winapi.StartWindowsMessageLoop(winapi.MakeMouseRawInputReceiver(handleMouseInput))
consoleDisplayLoop()
return
}