-
Notifications
You must be signed in to change notification settings - Fork 18.8k
Description
What version of Go are you using (go version)?
$ go version go version go1.18.2 windows/amd64
Does this issue reproduce with the latest release?
yes
What operating system and processor architecture are you using (go env)?
GOARCH=amd64
GOOS=windows
go env Output
$ go env set GO111MODULE=on set GOARCH=amd64 set GOBIN= set GOCACHE=C:\Users\Admin\AppData\Local\go-build set GOENV=C:\Users\Admin\AppData\Roaming\go\env set GOEXE=.exe set GOEXPERIMENT= set GOFLAGS= set GOHOSTARCH=amd64 set GOHOSTOS=windows set GOINSECURE= set GOMODCACHE=C:\Go\pkg\mod set GOOS=windows set GOPATH=C:\Go set GOROOT=C:\Program Files\Golang set GOSUMDB=sum.golang.google.cn set GOTMPDIR= set GOMOD=C:\Go\src\robotgodemo\go.mod set GOWORK= set CGO_CFLAGS=-g -O2 set CGO_CPPFLAGS= set CGO_CXXFLAGS=-g -O2 set CGO_FFLAGS=-g -O2 set CGO_LDFLAGS=-g -O2 set PKG_CONFIG=pkg-config set GOGCCFLAGS=-m64 -mthreads -fmessage-length=0 -fdebug-prefix-map=C:\Users\Admin\AppData\Local\Temp\go-build2101037137=/tmp/go-build -gno-record-gcc-switches
What did you do?
my code using syscall OR sys/windows :
var (
user32, _ = windows.LoadDLL("user32.dll")
sendInputProc = user32.MustFindProc("SendInput")
getInfoProc = user32.MustFindProc("GetMessageExtraInfo")
)
func scrollTest(scroll int) {
type mouseInput struct {
DwFlags uint32
Time uint32
MouseData uint32
Dx int32
Dy int32
DwExtraInfo uintptr
}
type Input struct {
InputType uint32
Mi mouseInput
}
x, y := robotgo.GetMousePos()
ret, _, _ := getInfoProc.Call()
fmt.Println(ret)
mi := mouseInput{
Dx: int32(x),
Dy: int32(y),
MouseData: uint32(scroll),
DwFlags: w32.MOUSEEVENTF_WHEEL,
Time: 0,
DwExtraInfo: ret,
}
input := Input{
InputType: w32.INPUT_MOUSE,
Mi: mi,
}
ret, _, err := sendInputProc.Call(uintptr(1),
uintptr(unsafe.Pointer(&input)),
unsafe.Sizeof(input))
fmt.Println(ret)
fmt.Println(err)
} // a code to simulate mouse wheel rotation.
I also used w32 wrapper by AllenDang for some constant, and I have make sure that these constant are the same value as the ones in win32api. This code is able to run, and able to scroll my mouse. However, we should focus on the type of MouseData, it is uint type, means the value should not be negative. However, in win32api, it is a DWORD type value ,which means it is a int32 type in golang. The positive or negative of the value means the mouse wheel rotate towards user or forwards user. When I try to change the type into int32, the code seems not correct. I can compile and run it, but this code will leads to an unexpected screen lock. When I run the code, I observed the screen scrolls as expected, and then my screen shutdown and my computer seems to sleep. When I awake it, I found the code run successfully without error. I think this case may be similar to #31685, is the SendInput dll function cause the accident?
Then here is my CGO code, I think there may be something wrong with syscall or windows, so I turned to CGO.
UINT ScrollMouse(int scroll)
{
INPUT input;
POINT pos;
GetCursorPos(&pos);
input.type = INPUT_MOUSE;
input.mi.dwFlags = MOUSEEVENTF_WHEEL;
//input.mi.time = NULL; //Windows will do the timestamp
input.mi.mouseData = (DWORD)scroll; //A positive value indicates that the wheel was rotated forward, away from the user; a negative value indicates that the wheel was rotated backward, toward the user. One wheel click is defined as WHEEL_DELTA, which is 120.
input.mi.dx = pos.x;
input.mi.dy = pos.y;
input.mi.dwExtraInfo = GetMessageExtraInfo();
return SendInput(1, &input, sizeof(INPUT));
}
*/
import "C"
Here is my code, which also leads to the same result. The code run successfully and scroll successfully but my computer sleep again, without any error.
Finally, I have to use the os/exec to run an exe file directly. And the code is just same as the one in CGO, I succeed this time. Why does the syscall leads to my computer sleeping? I confued if it is a bug or just my ignorance to something when coding. Sorry for that I have only one windows PC so I cannot try if it is the problem of my computer.
What did you expect to see?
The screen scrolled by the rotation input of mouse wheel which send by the go program.
What did you see instead?
The mouse wheel rotated, however, my computer sleep unexpected.