-
Notifications
You must be signed in to change notification settings - Fork 896
/
robotgo.go
1105 lines (922 loc) · 22.8 KB
/
robotgo.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2016 The go-vgo Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// https://github.com/go-vgo/robotgo/blob/master/LICENSE
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
/*
Package robotgo Go native cross-platform system automation.
Please make sure Golang, GCC is installed correctly before installing RobotGo;
See Requirements:
https://github.com/go-vgo/robotgo#requirements
Installation:
With Go module support (Go 1.11+), just import:
import "github.com/go-vgo/robotgo"
Otherwise, to install the robotgo package, run the command:
go get -u github.com/go-vgo/robotgo
*/
package robotgo
/*
#cgo darwin CFLAGS: -x objective-c -Wno-deprecated-declarations
#cgo darwin LDFLAGS: -framework Cocoa -framework OpenGL -framework IOKit
#cgo darwin LDFLAGS: -framework Carbon -framework CoreFoundation
//
#if __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ > MAC_OS_VERSION_14_4
#cgo darwin LDFLAGS: -framework ScreenCaptureKit
#endif
#cgo linux CFLAGS: -I/usr/src
#cgo linux LDFLAGS: -L/usr/src -lm -lX11 -lXtst
#cgo windows LDFLAGS: -lgdi32 -luser32
//
#include "screen/goScreen.h"
#include "mouse/mouse_c.h"
#include "window/goWindow.h"
*/
import "C"
import (
"errors"
"image"
"runtime"
"time"
"unsafe"
"github.com/vcaesar/tt"
)
const (
// Version get the robotgo version
Version = "v1.00.0.1189, MT. Baker!"
)
// GetVersion get the robotgo version
func GetVersion() string {
return Version
}
var (
// MouseSleep set the mouse default millisecond sleep time
MouseSleep = 0
// KeySleep set the key default millisecond sleep time
KeySleep = 0
// DisplayID set the screen display id
DisplayID = -1
// NotPid used the hwnd not pid in windows
NotPid bool
// Scale option the os screen scale
Scale bool
)
type (
// Map a map[string]interface{}
Map map[string]interface{}
// CHex define CHex as c rgb Hex type (C.MMRGBHex)
CHex C.MMRGBHex
// CBitmap define CBitmap as C.MMBitmapRef type
CBitmap C.MMBitmapRef
// Handle define window Handle as C.MData type
Handle C.MData
)
// Bitmap define the go Bitmap struct
//
// The common type conversion of bitmap:
//
// https://github.com/go-vgo/robotgo/blob/master/docs/keys.md#type-conversion
type Bitmap struct {
ImgBuf *uint8
Width, Height int
Bytewidth int
BitsPixel uint8
BytesPerPixel uint8
}
// Point is point struct
type Point struct {
X int
Y int
}
// Size is size structure
type Size struct {
W, H int
}
// Rect is rect structure
type Rect struct {
Point
Size
}
// Try handler(err)
func Try(fun func(), handler func(interface{})) {
defer func() {
if err := recover(); err != nil {
handler(err)
}
}()
fun()
}
// MilliSleep sleep tm milli second
func MilliSleep(tm int) {
time.Sleep(time.Duration(tm) * time.Millisecond)
}
// Sleep time.Sleep tm second
func Sleep(tm int) {
time.Sleep(time.Duration(tm) * time.Second)
}
// Deprecated: use the MilliSleep(),
//
// MicroSleep time C.microsleep(tm)
func MicroSleep(tm float64) {
C.microsleep(C.double(tm))
}
// GoString trans C.char to string
func GoString(char *C.char) string {
return C.GoString(char)
}
/*
_______. ______ .______ _______ _______ .__ __.
/ | / || _ \ | ____|| ____|| \ | |
| (----`| ,----'| |_) | | |__ | |__ | \| |
\ \ | | | / | __| | __| | . ` |
.----) | | `----.| |\ \----.| |____ | |____ | |\ |
|_______/ \______|| _| `._____||_______||_______||__| \__|
*/
// ToMMRGBHex trans CHex to C.MMRGBHex
func ToMMRGBHex(hex CHex) C.MMRGBHex {
return C.MMRGBHex(hex)
}
// UintToHex trans uint32 to robotgo.CHex
func UintToHex(u uint32) CHex {
hex := U32ToHex(C.uint32_t(u))
return CHex(hex)
}
// U32ToHex trans C.uint32_t to C.MMRGBHex
func U32ToHex(hex C.uint32_t) C.MMRGBHex {
return C.MMRGBHex(hex)
}
// U8ToHex trans *C.uint8_t to C.MMRGBHex
func U8ToHex(hex *C.uint8_t) C.MMRGBHex {
return C.MMRGBHex(*hex)
}
// PadHex trans C.MMRGBHex to string
func PadHex(hex C.MMRGBHex) string {
color := C.pad_hex(hex)
gcolor := C.GoString(color)
C.free(unsafe.Pointer(color))
return gcolor
}
// PadHexs trans CHex to string
func PadHexs(hex CHex) string {
return PadHex(C.MMRGBHex(hex))
}
// HexToRgb trans hex to rgb
func HexToRgb(hex uint32) *C.uint8_t {
return C.color_hex_to_rgb(C.uint32_t(hex))
}
// RgbToHex trans rgb to hex
func RgbToHex(r, g, b uint8) C.uint32_t {
return C.color_rgb_to_hex(C.uint8_t(r), C.uint8_t(g), C.uint8_t(b))
}
// GetPxColor get the pixel color return C.MMRGBHex
func GetPxColor(x, y int, displayId ...int) C.MMRGBHex {
cx := C.int32_t(x)
cy := C.int32_t(y)
display := displayIdx(displayId...)
color := C.get_px_color(cx, cy, C.int32_t(display))
return color
}
// GetPixelColor get the pixel color return string
func GetPixelColor(x, y int, displayId ...int) string {
return PadHex(GetPxColor(x, y, displayId...))
}
// GetLocationColor get the location pos's color
func GetLocationColor(displayId ...int) string {
x, y := Location()
return GetPixelColor(x, y, displayId...)
}
// IsMain is main display
func IsMain(displayId int) bool {
return displayId == GetMainId()
}
func displayIdx(id ...int) int {
display := -1
if DisplayID != -1 {
display = DisplayID
}
if len(id) > 0 {
display = id[0]
}
return display
}
func getNumDisplays() int {
return int(C.get_num_displays())
}
// GetHWNDByPid get the hwnd by pid
func GetHWNDByPid(pid int) int {
return int(C.get_hwnd_by_pid(C.uintptr(pid)))
}
// SysScale get the sys scale
func SysScale(displayId ...int) float64 {
display := displayIdx(displayId...)
s := C.sys_scale(C.int32_t(display))
return float64(s)
}
// Scaled get the screen scaled return scale size
func Scaled(x int, displayId ...int) int {
f := ScaleF(displayId...)
return Scaled0(x, f)
}
// Scaled0 return int(x * f)
func Scaled0(x int, f float64) int {
return int(float64(x) * f)
}
// Scaled1 return int(x / f)
func Scaled1(x int, f float64) int {
return int(float64(x) / f)
}
// GetScreenSize get the screen size
func GetScreenSize() (int, int) {
size := C.getMainDisplaySize()
return int(size.w), int(size.h)
}
// GetScreenRect get the screen rect (x, y, w, h)
func GetScreenRect(displayId ...int) Rect {
display := -1
if len(displayId) > 0 {
display = displayId[0]
}
rect := C.getScreenRect(C.int32_t(display))
x, y, w, h := int(rect.origin.x), int(rect.origin.y),
int(rect.size.w), int(rect.size.h)
if runtime.GOOS == "windows" {
// f := ScaleF(displayId...)
f := ScaleF()
x, y, w, h = Scaled0(x, f), Scaled0(y, f), Scaled0(w, f), Scaled0(h, f)
}
return Rect{
Point{X: x, Y: y},
Size{W: w, H: h},
}
}
// GetScaleSize get the screen scale size
func GetScaleSize(displayId ...int) (int, int) {
x, y := GetScreenSize()
f := ScaleF(displayId...)
return int(float64(x) * f), int(float64(y) * f)
}
// CaptureScreen capture the screen return bitmap(c struct),
// use `defer robotgo.FreeBitmap(bitmap)` to free the bitmap
//
// robotgo.CaptureScreen(x, y, w, h int)
func CaptureScreen(args ...int) CBitmap {
var x, y, w, h C.int32_t
displayId := -1
if DisplayID != -1 {
displayId = DisplayID
}
if len(args) > 4 {
displayId = args[4]
}
if len(args) > 3 {
x = C.int32_t(args[0])
y = C.int32_t(args[1])
w = C.int32_t(args[2])
h = C.int32_t(args[3])
} else {
// Get the main screen rect.
rect := GetScreenRect(displayId)
if runtime.GOOS == "windows" {
x = C.int32_t(rect.X)
y = C.int32_t(rect.Y)
}
w = C.int32_t(rect.W)
h = C.int32_t(rect.H)
}
isPid := 0
if NotPid || len(args) > 5 {
isPid = 1
}
bit := C.capture_screen(x, y, w, h, C.int32_t(displayId), C.int8_t(isPid))
return CBitmap(bit)
}
// CaptureGo capture the screen and return bitmap(go struct)
func CaptureGo(args ...int) Bitmap {
bit := CaptureScreen(args...)
defer FreeBitmap(bit)
return ToBitmap(bit)
}
// CaptureImg capture the screen and return image.Image, error
func CaptureImg(args ...int) (image.Image, error) {
bit := CaptureScreen(args...)
if bit == nil {
return nil, errors.New("Capture image not found.")
}
defer FreeBitmap(bit)
return ToImage(bit), nil
}
// FreeBitmap free and dealloc the C bitmap
func FreeBitmap(bitmap CBitmap) {
// C.destroyMMBitmap(bitmap)
C.bitmap_dealloc(C.MMBitmapRef(bitmap))
}
// FreeBitmapArr free and dealloc the C bitmap array
func FreeBitmapArr(bit ...CBitmap) {
for i := 0; i < len(bit); i++ {
FreeBitmap(bit[i])
}
}
// ToMMBitmapRef trans CBitmap to C.MMBitmapRef
func ToMMBitmapRef(bit CBitmap) C.MMBitmapRef {
return C.MMBitmapRef(bit)
}
// ToBitmap trans C.MMBitmapRef to Bitmap
func ToBitmap(bit CBitmap) Bitmap {
bitmap := Bitmap{
ImgBuf: (*uint8)(bit.imageBuffer),
Width: int(bit.width),
Height: int(bit.height),
Bytewidth: int(bit.bytewidth),
BitsPixel: uint8(bit.bitsPerPixel),
BytesPerPixel: uint8(bit.bytesPerPixel),
}
return bitmap
}
// ToCBitmap trans Bitmap to C.MMBitmapRef
func ToCBitmap(bit Bitmap) CBitmap {
cbitmap := C.createMMBitmap_c(
(*C.uint8_t)(bit.ImgBuf),
C.int32_t(bit.Width),
C.int32_t(bit.Height),
C.int32_t(bit.Bytewidth),
C.uint8_t(bit.BitsPixel),
C.uint8_t(bit.BytesPerPixel),
)
return CBitmap(cbitmap)
}
// ToImage convert C.MMBitmapRef to standard image.Image
func ToImage(bit CBitmap) image.Image {
return ToRGBA(bit)
}
// ToRGBA convert C.MMBitmapRef to standard image.RGBA
func ToRGBA(bit CBitmap) *image.RGBA {
bmp1 := ToBitmap(bit)
return ToRGBAGo(bmp1)
}
// ImgToCBitmap trans image.Image to CBitmap
func ImgToCBitmap(img image.Image) CBitmap {
return ToCBitmap(ImgToBitmap(img))
}
// ByteToCBitmap trans []byte to CBitmap
func ByteToCBitmap(by []byte) CBitmap {
img, _ := ByteToImg(by)
return ImgToCBitmap(img)
}
// SetXDisplayName set XDisplay name (Linux)
func SetXDisplayName(name string) error {
cname := C.CString(name)
str := C.set_XDisplay_name(cname)
C.free(unsafe.Pointer(cname))
return toErr(str)
}
// GetXDisplayName get XDisplay name (Linux)
func GetXDisplayName() string {
name := C.get_XDisplay_name()
gname := C.GoString(name)
C.free(unsafe.Pointer(name))
return gname
}
// CloseMainDisplay close the main X11 display
func CloseMainDisplay() {
C.close_main_display()
}
// Deprecated: use the ScaledF(),
//
// ScaleX get the primary display horizontal DPI scale factor, drop
func ScaleX() int {
return int(C.scaleX())
}
/*
.___ ___. ______ __ __ _______. _______
| \/ | / __ \ | | | | / || ____|
| \ / | | | | | | | | | | (----`| |__
| |\/| | | | | | | | | | \ \ | __|
| | | | | `--' | | `--' | .----) | | |____
|__| |__| \______/ \______/ |_______/ |_______|
*/
// CheckMouse check the mouse button
func CheckMouse(btn string) C.MMMouseButton {
// button = args[0].(C.MMMouseButton)
m1 := map[string]C.MMMouseButton{
"left": C.LEFT_BUTTON,
"center": C.CENTER_BUTTON,
"right": C.RIGHT_BUTTON,
"wheelDown": C.WheelDown,
"wheelUp": C.WheelUp,
"wheelLeft": C.WheelLeft,
"wheelRight": C.WheelRight,
}
if v, ok := m1[btn]; ok {
return v
}
return C.LEFT_BUTTON
}
// MoveScale calculate the os scale factor x, y
func MoveScale(x, y int, displayId ...int) (int, int) {
if Scale && runtime.GOOS == "windows" {
f := ScaleF()
x, y = Scaled1(x, f), Scaled1(y, f)
}
return x, y
}
// Move move the mouse to (x, y)
//
// Examples:
//
// robotgo.MouseSleep = 100 // 100 millisecond
// robotgo.Move(10, 10)
func Move(x, y int, displayId ...int) {
x, y = MoveScale(x, y, displayId...)
cx := C.int32_t(x)
cy := C.int32_t(y)
C.moveMouse(C.MMPointInt32Make(cx, cy))
MilliSleep(MouseSleep)
}
// Deprecated: use the DragSmooth(),
//
// Drag drag the mouse to (x, y),
// It's not valid now, use the DragSmooth()
func Drag(x, y int, args ...string) {
x, y = MoveScale(x, y)
var button C.MMMouseButton = C.LEFT_BUTTON
cx := C.int32_t(x)
cy := C.int32_t(y)
if len(args) > 0 {
button = CheckMouse(args[0])
}
C.dragMouse(C.MMPointInt32Make(cx, cy), button)
MilliSleep(MouseSleep)
}
// DragSmooth drag the mouse like smooth to (x, y)
//
// Examples:
//
// robotgo.DragSmooth(10, 10)
func DragSmooth(x, y int, args ...interface{}) {
Toggle("left")
MilliSleep(50)
MoveSmooth(x, y, args...)
Toggle("left", "up")
}
// MoveSmooth move the mouse smooth,
// moves mouse to x, y human like, with the mouse button up.
//
// robotgo.MoveSmooth(x, y int, low, high float64, mouseDelay int)
//
// Examples:
//
// robotgo.MoveSmooth(10, 10)
// robotgo.MoveSmooth(10, 10, 1.0, 2.0)
func MoveSmooth(x, y int, args ...interface{}) bool {
// if runtime.GOOS == "windows" {
// f := ScaleF()
// x, y = Scaled0(x, f), Scaled0(y, f)
// }
x, y = MoveScale(x, y)
cx := C.int32_t(x)
cy := C.int32_t(y)
var (
mouseDelay = 1
low C.double
high C.double
)
if len(args) > 2 {
mouseDelay = args[2].(int)
}
if len(args) > 1 {
low = C.double(args[0].(float64))
high = C.double(args[1].(float64))
} else {
low = 1.0
high = 3.0
}
cbool := C.smoothlyMoveMouse(C.MMPointInt32Make(cx, cy), low, high)
MilliSleep(MouseSleep + mouseDelay)
return bool(cbool)
}
// MoveArgs get the mouse relative args
func MoveArgs(x, y int) (int, int) {
mx, my := Location()
mx = mx + x
my = my + y
return mx, my
}
// MoveRelative move mouse with relative
func MoveRelative(x, y int) {
Move(MoveArgs(x, y))
}
// MoveSmoothRelative move mouse smooth with relative
func MoveSmoothRelative(x, y int, args ...interface{}) {
mx, my := MoveArgs(x, y)
MoveSmooth(mx, my, args...)
}
// Location get the mouse location position return x, y
func Location() (int, int) {
pos := C.location()
x := int(pos.x)
y := int(pos.y)
if runtime.GOOS == "windows" {
f := ScaleF()
x, y = Scaled0(x, f), Scaled0(y, f)
}
return x, y
}
// Click click the mouse button
//
// robotgo.Click(button string, double bool)
//
// Examples:
//
// robotgo.Click() // default is left button
// robotgo.Click("right")
// robotgo.Click("wheelLeft")
func Click(args ...interface{}) {
var (
button C.MMMouseButton = C.LEFT_BUTTON
double bool
)
if len(args) > 0 {
button = CheckMouse(args[0].(string))
}
if len(args) > 1 {
double = args[1].(bool)
}
if !double {
C.clickMouse(button)
} else {
C.doubleClick(button)
}
MilliSleep(MouseSleep)
}
// MoveClick move and click the mouse
//
// robotgo.MoveClick(x, y int, button string, double bool)
//
// Examples:
//
// robotgo.MouseSleep = 100
// robotgo.MoveClick(10, 10)
func MoveClick(x, y int, args ...interface{}) {
Move(x, y)
MilliSleep(50)
Click(args...)
}
// MovesClick move smooth and click the mouse
//
// use the `robotgo.MouseSleep = 100`
func MovesClick(x, y int, args ...interface{}) {
MoveSmooth(x, y)
MilliSleep(50)
Click(args...)
}
// Toggle toggle the mouse, support button:
//
// "left", "center", "right",
// "wheelDown", "wheelUp", "wheelLeft", "wheelRight"
//
// Examples:
//
// robotgo.Toggle("left") // default is down
// robotgo.Toggle("left", "up")
func Toggle(key ...interface{}) error {
var button C.MMMouseButton = C.LEFT_BUTTON
if len(key) > 0 {
button = CheckMouse(key[0].(string))
}
down := true
if len(key) > 1 && key[1].(string) == "up" {
down = false
}
C.toggleMouse(C.bool(down), button)
if len(key) > 2 {
MilliSleep(MouseSleep)
}
return nil
}
// MouseDown send mouse down event
func MouseDown(key ...interface{}) error {
return Toggle(key...)
}
// MouseUp send mouse up event
func MouseUp(key ...interface{}) error {
if len(key) <= 0 {
key = append(key, "left")
}
return Toggle(append(key, "up")...)
}
// Scroll scroll the mouse to (x, y)
//
// robotgo.Scroll(x, y, msDelay int)
//
// Examples:
//
// robotgo.Scroll(10, 10)
func Scroll(x, y int, args ...int) {
var msDelay = 10
if len(args) > 0 {
msDelay = args[0]
}
cx := C.int(x)
cy := C.int(y)
C.scrollMouseXY(cx, cy)
MilliSleep(MouseSleep + msDelay)
}
// ScrollDir scroll the mouse with direction to (x, "up")
// supported: "up", "down", "left", "right"
//
// Examples:
//
// robotgo.ScrollDir(10, "down")
// robotgo.ScrollDir(10, "up")
func ScrollDir(x int, direction ...interface{}) {
d := "down"
if len(direction) > 0 {
d = direction[0].(string)
}
if d == "down" {
Scroll(0, -x)
}
if d == "up" {
Scroll(0, x)
}
if d == "left" {
Scroll(x, 0)
}
if d == "right" {
Scroll(-x, 0)
}
// MilliSleep(MouseSleep)
}
// ScrollSmooth scroll the mouse smooth,
// default scroll 5 times and sleep 100 millisecond
//
// robotgo.ScrollSmooth(toy, num, sleep, tox)
//
// Examples:
//
// robotgo.ScrollSmooth(-10)
// robotgo.ScrollSmooth(-10, 6, 200, -10)
func ScrollSmooth(to int, args ...int) {
i := 0
num := 5
if len(args) > 0 {
num = args[0]
}
tm := 100
if len(args) > 1 {
tm = args[1]
}
tox := 0
if len(args) > 2 {
tox = args[2]
}
for {
Scroll(tox, to)
MilliSleep(tm)
i++
if i == num {
break
}
}
MilliSleep(MouseSleep)
}
// ScrollRelative scroll mouse with relative
//
// Examples:
//
// robotgo.ScrollRelative(10, 10)
func ScrollRelative(x, y int, args ...int) {
mx, my := MoveArgs(x, y)
Scroll(mx, my, args...)
}
/*
____ __ ____ __ .__ __. _______ ______ ____ __ ____
\ \ / \ / / | | | \ | | | \ / __ \ \ \ / \ / /
\ \/ \/ / | | | \| | | .--. | | | | \ \/ \/ /
\ / | | | . ` | | | | | | | | \ /
\ /\ / | | | |\ | | '--' | `--' | \ /\ /
\__/ \__/ |__| |__| \__| |_______/ \______/ \__/ \__/
*/
func alertArgs(args ...string) (string, string) {
var (
defaultBtn = "Ok"
cancelBtn = "Cancel"
)
if len(args) > 0 {
defaultBtn = args[0]
}
if len(args) > 1 {
cancelBtn = args[1]
}
return defaultBtn, cancelBtn
}
func showAlert(title, msg string, args ...string) bool {
defaultBtn, cancelBtn := alertArgs(args...)
cTitle := C.CString(title)
cMsg := C.CString(msg)
defaultButton := C.CString(defaultBtn)
cancelButton := C.CString(cancelBtn)
cbool := C.showAlert(cTitle, cMsg, defaultButton, cancelButton)
ibool := int(cbool)
C.free(unsafe.Pointer(cTitle))
C.free(unsafe.Pointer(cMsg))
C.free(unsafe.Pointer(defaultButton))
C.free(unsafe.Pointer(cancelButton))
return ibool == 0
}
// IsValid valid the window
func IsValid() bool {
abool := C.is_valid()
gbool := bool(abool)
return gbool
}
// SetActive set the window active
func SetActive(win Handle) {
SetActiveC(C.MData(win))
}
// SetActiveC set the window active
func SetActiveC(win C.MData) {
C.set_active(win)
}
// GetActive get the active window
func GetActive() Handle {
return Handle(GetActiveC())
}
// GetActiveC get the active window
func GetActiveC() C.MData {
mdata := C.get_active()
// fmt.Println("active----", mdata)
return mdata
}
// MinWindow set the window min
func MinWindow(pid int, args ...interface{}) {
var (
state = true
isPid int
)
if len(args) > 0 {
state = args[0].(bool)
}
if len(args) > 1 || NotPid {
isPid = 1
}
C.min_window(C.uintptr(pid), C.bool(state), C.int8_t(isPid))
}
// MaxWindow set the window max
func MaxWindow(pid int, args ...interface{}) {
var (
state = true
isPid int
)
if len(args) > 0 {
state = args[0].(bool)
}
if len(args) > 1 || NotPid {
isPid = 1
}
C.max_window(C.uintptr(pid), C.bool(state), C.int8_t(isPid))
}
// CloseWindow close the window
func CloseWindow(args ...int) {
if len(args) <= 0 {
C.close_main_window()
return
}
var pid, isPid int
if len(args) > 0 {
pid = args[0]
}
if len(args) > 1 || NotPid {
isPid = 1
}
C.close_window_by_PId(C.uintptr(pid), C.int8_t(isPid))
}
// SetHandle set the window handle
func SetHandle(hwnd int) {
chwnd := C.uintptr(hwnd)
C.setHandle(chwnd)
}
// SetHandlePid set the window handle by pid
func SetHandlePid(pid int, args ...int) {
var isPid int
if len(args) > 0 || NotPid {
isPid = 1
}
C.set_handle_pid_mData(C.uintptr(pid), C.int8_t(isPid))
}
// GetHandById get handle mdata by id
func GetHandById(id int, args ...int) Handle {
isPid := 1
if len(args) > 0 {
isPid = args[0]
}
return GetHandByPid(id, isPid)
}
// GetHandByPid get handle mdata by pid
func GetHandByPid(pid int, args ...int) Handle {
return Handle(GetHandByPidC(pid, args...))
}
// Deprecated: use the GetHandByPid(),
//
// GetHandPid get handle mdata by pid
func GetHandPid(pid int, args ...int) Handle {
return GetHandByPid(pid, args...)
}
// GetHandByPidC get handle mdata by pid
func GetHandByPidC(pid int, args ...int) C.MData {
var isPid int
if len(args) > 0 || NotPid {
isPid = 1
}