Merge pull request #377 from go-vgo/bitmap-pr

add all mouse and key sleep option and CaptureImg(), Scale0(), ScrollRelative() function
This commit is contained in:
Evans 2021-10-13 15:27:10 -04:00 committed by GitHub
commit a640442a1c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 118 additions and 84 deletions

View File

@ -131,8 +131,12 @@ import (
func main() { func main() {
robotgo.ScrollMouse(10, "up") robotgo.ScrollMouse(10, "up")
robotgo.Scroll(100, 200) robotgo.Scroll(100, 200)
robotgo.MilliSleep(100)
robotgo.ScrollRelative(10, -100)
robotgo.Move(10, 10) robotgo.MouseSleep = 100
robotgo.Move(10, 20)
robotgo.MoveRelative(0, -10)
robotgo.Drag(10, 10) robotgo.Drag(10, 10)
robotgo.Click("left", true) robotgo.Click("left", true)
@ -163,6 +167,7 @@ func main() {
// ustr := uint32(robotgo.CharCodeAt("Test", 0)) // ustr := uint32(robotgo.CharCodeAt("Test", 0))
// robotgo.UnicodeType(ustr) // robotgo.UnicodeType(ustr)
robotgo.KeySleep = 100
robotgo.KeyTap("enter") robotgo.KeyTap("enter")
// robotgo.TypeStr("en") // robotgo.TypeStr("en")
robotgo.KeyTap("i", "alt", "command") robotgo.KeyTap("i", "alt", "command")
@ -170,6 +175,7 @@ func main() {
arr := []string{"alt", "command"} arr := []string{"alt", "command"}
robotgo.KeyTap("i", arr) robotgo.KeyTap("i", arr)
robotgo.MilliSleep(100)
robotgo.KeyToggle("a", "down") robotgo.KeyToggle("a", "down")
robotgo.WriteAll("Test") robotgo.WriteAll("Test")
@ -249,25 +255,29 @@ func main() {
func opencv() { func opencv() {
name := "test.png" name := "test.png"
name1 := "test_001.png" name1 := "test_001.png"
robotgo.SaveCapture(name, 10, 10, 30, 30) robotgo.SaveCapture(name1, 10, 10, 30, 30)
robotgo.SaveCapture(name1) robotgo.SaveCapture(name)
fmt.Print("gcv find image: ") fmt.Print("gcv find image: ")
fmt.Println(gcv.FindImgFile(name, name1)) fmt.Println(gcv.FindImgFile(name1, name))
fmt.Println(gcv.FindAllImgFile(name1, name))
bit := robotgo.OpenBitmap(name) bit := robotgo.OpenBitmap(name1)
defer robotgo.FindBitmap(bit) defer robotgo.FindBitmap(bit)
fmt.Print("find bitmap: ") fmt.Print("find bitmap: ")
fmt.Println(robotgo.FindBitmap(bit)) fmt.Println(robotgo.FindBitmap(bit))
bit0 := robotgo.CaptureScreen() // bit0 := robotgo.CaptureScreen()
img := robotgo.ToImage(bit0) // img := robotgo.ToImage(bit0)
bit1 := robotgo.CaptureScreen(10, 10, 30, 30) // bit1 := robotgo.CaptureScreen(10, 10, 30, 30)
img1 := robotgo.ToImage(bit1) // img1 := robotgo.ToImage(bit1)
defer robotgo.FreeBitmapArr(bit0, bit1) // defer robotgo.FreeBitmapArr(bit0, bit1)
img := robotgo.CaptureImg()
img1 := robotgo.CaptureImg(10, 10, 30, 30)
fmt.Print("gcv find image: ") fmt.Print("gcv find image: ")
fmt.Println(gcv.FindImg(img1, img)) fmt.Println(gcv.FindImg(img1, img))
fmt.Println(gcv.FindAllImg(img1, img))
} }
``` ```

View File

@ -129,8 +129,12 @@ import (
func main() { func main() {
robotgo.ScrollMouse(10, "up") robotgo.ScrollMouse(10, "up")
robotgo.Scroll(100, 200) robotgo.Scroll(100, 200)
robotgo.MilliSleep(100)
robotgo.ScrollRelative(10, -100)
robotgo.Move(10, 10) robotgo.MouseSleep = 100
robotgo.Move(10, 20)
robotgo.MoveRelative(0, -10)
robotgo.Drag(10, 10) robotgo.Drag(10, 10)
robotgo.Click("left", true) robotgo.Click("left", true)
@ -161,6 +165,7 @@ func main() {
// ustr := uint32(robotgo.CharCodeAt("テストする", 0)) // ustr := uint32(robotgo.CharCodeAt("テストする", 0))
// robotgo.UnicodeType(ustr) // robotgo.UnicodeType(ustr)
robotgo.KeySleep = 100
robotgo.KeyTap("enter") robotgo.KeyTap("enter")
// robotgo.TypeStr("en") // robotgo.TypeStr("en")
robotgo.KeyTap("i", "alt", "command") robotgo.KeyTap("i", "alt", "command")
@ -168,6 +173,7 @@ func main() {
arr := []string{"alt", "command"} arr := []string{"alt", "command"}
robotgo.KeyTap("i", arr) robotgo.KeyTap("i", arr)
robotgo.MilliSleep(100)
robotgo.KeyToggle("a", "down") robotgo.KeyToggle("a", "down")
robotgo.WriteAll("テストする") robotgo.WriteAll("テストする")
@ -247,25 +253,29 @@ func main() {
func opencv() { func opencv() {
name := "test.png" name := "test.png"
name1 := "test_001.png" name1 := "test_001.png"
robotgo.SaveCapture(name, 10, 10, 30, 30) robotgo.SaveCapture(name1, 10, 10, 30, 30)
robotgo.SaveCapture(name1) robotgo.SaveCapture(name)
fmt.Print("gcv find image: ") fmt.Print("gcv find image: ")
fmt.Println(gcv.FindImgFile(name, name1)) fmt.Println(gcv.FindImgFile(name1, name))
fmt.Println(gcv.FindAllImgFile(name1, name))
bit := robotgo.OpenBitmap(name) bit := robotgo.OpenBitmap(name1)
defer robotgo.FindBitmap(bit) defer robotgo.FindBitmap(bit)
fmt.Print("find bitmap: ") fmt.Print("find bitmap: ")
fmt.Println(robotgo.FindBitmap(bit)) fmt.Println(robotgo.FindBitmap(bit))
bit0 := robotgo.CaptureScreen() // bit0 := robotgo.CaptureScreen()
img := robotgo.ToImage(bit0) // img := robotgo.ToImage(bit0)
bit1 := robotgo.CaptureScreen(10, 10, 30, 30) // bit1 := robotgo.CaptureScreen(10, 10, 30, 30)
img1 := robotgo.ToImage(bit1) // img1 := robotgo.ToImage(bit1)
defer robotgo.FreeBitmapArr(bit0, bit1) // defer robotgo.FreeBitmapArr(bit0, bit1)
img := robotgo.CaptureImg()
img1 := robotgo.CaptureImg(10, 10, 30, 30)
fmt.Print("gcv find image: ") fmt.Print("gcv find image: ")
fmt.Println(gcv.FindImg(img1, img)) fmt.Println(gcv.FindImg(img1, img))
fmt.Println(gcv.FindAllImg(img1, img))
} }
``` ```

View File

@ -80,6 +80,13 @@ func GetVersion() string {
return Version return Version
} }
var (
// MouseSleep set the mouse default millisecond sleep time
MouseSleep = 0
// KeySleep set the key default millisecond sleep time
KeySleep = 0
)
type ( type (
// Map a map[string]interface{} // Map a map[string]interface{}
Map map[string]interface{} Map map[string]interface{}
@ -222,7 +229,7 @@ func SysScale() float64 {
return float64(s) return float64(s)
} }
// Scaled x * sys-scale // Scaled return x * sys_scale
func Scaled(x int) int { func Scaled(x int) int {
return int(float64(x) * SysScale()) return int(float64(x) * SysScale())
} }
@ -261,6 +268,11 @@ func Scale() int {
return dpi[x] return dpi[x]
} }
// Scale0 return ScaleX() / 0.96
func Scale0() int {
return int(float64(ScaleX()) / 0.96)
}
// Mul mul the scale // Mul mul the scale
func Mul(x int) int { func Mul(x int) int {
s := Scale() s := Scale()
@ -328,6 +340,14 @@ func GoCaptureScreen(args ...int) Bitmap {
return ToBitmap(bit) return ToBitmap(bit)
} }
// CaptureImg capture the screen and return image.Image
func CaptureImg(args ...int) image.Image {
bit := CaptureScreen(args...)
defer FreeBitmap(bit)
return ToImage(bit)
}
// SaveCapture capture screen and save // SaveCapture capture screen and save
func SaveCapture(spath string, args ...int) { func SaveCapture(spath string, args ...int) {
bit := CaptureScreen(args...) bit := CaptureScreen(args...)
@ -370,22 +390,23 @@ func MoveMouse(x, y int) {
Move(x, y) Move(x, y)
} }
// Move move the mouse // Move move the mouse to (x, y)
func Move(x, y int) { func Move(x, y int) {
cx := C.int32_t(x) cx := C.int32_t(x)
cy := C.int32_t(y) cy := C.int32_t(y)
C.move_mouse(cx, cy) C.move_mouse(cx, cy)
MilliSleep(MouseSleep)
} }
// DragMouse drag the mouse // DragMouse drag the mouse to (x, y)
func DragMouse(x, y int, args ...string) { func DragMouse(x, y int, args ...string) {
Drag(x, y, args...) Drag(x, y, args...)
} }
// Drag drag the mouse // Drag drag the mouse to (x, y)
func Drag(x, y int, args ...string) { func Drag(x, y int, args ...string) {
var button C.MMMouseButton = C.LEFT_BUTTON var button C.MMMouseButton = C.LEFT_BUTTON
cx := C.int32_t(x) cx := C.int32_t(x)
cy := C.int32_t(y) cy := C.int32_t(y)
@ -394,6 +415,7 @@ func Drag(x, y int, args ...string) {
} }
C.drag_mouse(cx, cy, button) C.drag_mouse(cx, cy, button)
MilliSleep(MouseSleep)
} }
// DragSmooth drag the mouse smooth // DragSmooth drag the mouse smooth
@ -436,6 +458,7 @@ func MoveSmooth(x, y int, args ...interface{}) bool {
} }
cbool := C.move_mouse_smooth(cx, cy, low, high, C.int(mouseDelay)) cbool := C.move_mouse_smooth(cx, cy, low, high, C.int(mouseDelay))
MilliSleep(MouseSleep)
return bool(cbool) return bool(cbool)
} }
@ -449,12 +472,12 @@ func MoveArgs(x, y int) (int, int) {
return mx, my return mx, my
} }
// MoveRelative move mose relative // MoveRelative move mouse with relative
func MoveRelative(x, y int) { func MoveRelative(x, y int) {
Move(MoveArgs(x, y)) Move(MoveArgs(x, y))
} }
// MoveSmoothRelative move mose smooth relative // MoveSmoothRelative move mouse smooth with relative
func MoveSmoothRelative(x, y int, args ...interface{}) { func MoveSmoothRelative(x, y int, args ...interface{}) {
mx, my := MoveArgs(x, y) mx, my := MoveArgs(x, y)
MoveSmooth(mx, my, args...) MoveSmooth(mx, my, args...)
@ -495,6 +518,7 @@ func Click(args ...interface{}) {
} }
C.mouse_click(button, double) C.mouse_click(button, double)
MilliSleep(MouseSleep)
} }
// MoveClick move and click the mouse // MoveClick move and click the mouse
@ -523,6 +547,7 @@ func MouseToggle(togKey string, args ...interface{}) int {
i := C.mouse_toggle(down, button) i := C.mouse_toggle(down, button)
C.free(unsafe.Pointer(down)) C.free(unsafe.Pointer(down))
MilliSleep(MouseSleep)
return int(i) return int(i)
} }
@ -533,9 +558,10 @@ func ScrollMouse(x int, direction string) {
C.scroll_mouse(cx, cy) C.scroll_mouse(cx, cy)
C.free(unsafe.Pointer(cy)) C.free(unsafe.Pointer(cy))
MilliSleep(MouseSleep)
} }
// Scroll scroll the mouse with x, y // Scroll scroll the mouse to (x, y)
// //
// robotgo.Scroll(x, y, msDelay int) // robotgo.Scroll(x, y, msDelay int)
func Scroll(x, y int, args ...int) { func Scroll(x, y int, args ...int) {
@ -549,6 +575,13 @@ func Scroll(x, y int, args ...int) {
cz := C.int(msDelay) cz := C.int(msDelay)
C.scroll(cx, cy, cz) C.scroll(cx, cy, cz)
MilliSleep(MouseSleep)
}
// ScrollRelative scroll mouse with relative
func ScrollRelative(x, y int, args ...int) {
mx, my := MoveArgs(x, y)
Scroll(mx, my, args...)
} }
// SetMouseDelay set mouse delay // SetMouseDelay set mouse delay
@ -645,6 +678,7 @@ func KeyTap(tapKey string, args ...interface{}) string {
C.free(unsafe.Pointer(amod)) C.free(unsafe.Pointer(amod))
C.free(unsafe.Pointer(amodt)) C.free(unsafe.Pointer(amodt))
MilliSleep(KeySleep)
return C.GoString(str) return C.GoString(str)
} }
@ -695,6 +729,7 @@ func KeyToggle(key string, args ...string) string {
C.free(unsafe.Pointer(cmKey)) C.free(unsafe.Pointer(cmKey))
C.free(unsafe.Pointer(cmKeyT)) C.free(unsafe.Pointer(cmKeyT))
MilliSleep(KeySleep)
return C.GoString(str) return C.GoString(str)
} }
@ -811,6 +846,7 @@ func TypeStr(str string, args ...float64) {
MicroSleep(tm) MicroSleep(tm)
// } // }
} }
MilliSleep(KeySleep)
} }
// PasteStr paste a string, support UTF-8 // PasteStr paste a string, support UTF-8
@ -989,7 +1025,7 @@ func FindCBitmap(bmp CBitmap, args ...interface{}) (int, int) {
// FindBitmap find the bitmap's pos // FindBitmap find the bitmap's pos
// //
// robotgo.FindBitmap(bitmap, subbitamp C.MMBitmapRef, tolerance float64) // robotgo.FindBitmap(bitmap, source_bitamp C.MMBitmapRef, tolerance float64)
// //
// |tolerance| should be in the range 0.0f - 1.0f, denoting how closely the // |tolerance| should be in the range 0.0f - 1.0f, denoting how closely the
// colors in the bitmaps need to match, with 0 being exact and 1 being any. // colors in the bitmaps need to match, with 0 being exact and 1 being any.
@ -1023,7 +1059,7 @@ func FindBitmap(bit C.MMBitmapRef, args ...interface{}) (int, int) {
// FindPic finding the image by path // FindPic finding the image by path
// //
// robotgo.FindPic(path string, subbitamp C.MMBitmapRef, tolerance float64) // robotgo.FindPic(path string, source_bitamp C.MMBitmapRef, tolerance float64)
// //
// This method only automatically free the internal bitmap, // This method only automatically free the internal bitmap,
// use `defer robotgo.FreeBitmap(bit)` to free the bitmap // use `defer robotgo.FreeBitmap(bit)` to free the bitmap
@ -1435,32 +1471,32 @@ func ShowAlert(title, msg string, args ...string) bool {
var ( var (
// title string // title string
// msg string // msg string
defaultButton = "Ok" defaultBtn = "Ok"
cancelButton = "Cancel" cancelBtn = "Cancel"
) )
if len(args) > 0 { if len(args) > 0 {
// title = args[0] // title = args[0]
// msg = args[1] // msg = args[1]
defaultButton = args[0] defaultBtn = args[0]
} }
if len(args) > 1 { if len(args) > 1 {
cancelButton = args[1] cancelBtn = args[1]
} }
atitle := C.CString(title) cTitle := C.CString(title)
amsg := C.CString(msg) cMsg := C.CString(msg)
adefaultButton := C.CString(defaultButton) defaultButton := C.CString(defaultBtn)
acancelButton := C.CString(cancelButton) cancelButton := C.CString(cancelBtn)
cbool := C.show_alert(atitle, amsg, adefaultButton, acancelButton) cbool := C.show_alert(cTitle, cMsg, defaultButton, cancelButton)
ibool := int(cbool) ibool := int(cbool)
C.free(unsafe.Pointer(atitle)) C.free(unsafe.Pointer(cTitle))
C.free(unsafe.Pointer(amsg)) C.free(unsafe.Pointer(cMsg))
C.free(unsafe.Pointer(adefaultButton)) C.free(unsafe.Pointer(defaultButton))
C.free(unsafe.Pointer(acancelButton)) C.free(unsafe.Pointer(cancelButton))
return ibool == 0 return ibool == 0
} }

View File

@ -27,10 +27,7 @@ MMBitmapRef copyMMBitmapFromDisplayInRect(MMRectInt32 rect){
CGDirectDisplayID displayID = CGMainDisplayID(); CGDirectDisplayID displayID = CGMainDisplayID();
CGImageRef image = CGDisplayCreateImageForRect(displayID, CGImageRef image = CGDisplayCreateImageForRect(displayID,
CGRectMake(rect.origin.x, CGRectMake(rect.origin.x, rect.origin.y, rect.size.w, rect.size.h));
rect.origin.y,
rect.size.w,
rect.size.h));
if (!image) { return NULL; } if (!image) { return NULL; }
@ -43,15 +40,12 @@ MMBitmapRef copyMMBitmapFromDisplayInRect(MMRectInt32 rect){
CFDataGetBytes(imageData, CFRangeMake(0, bufferSize), buffer); CFDataGetBytes(imageData, CFRangeMake(0, bufferSize), buffer);
bitmap = createMMBitmap(buffer, bitmap = createMMBitmap(buffer,
CGImageGetWidth(image), CGImageGetWidth(image),CGImageGetHeight(image),
CGImageGetHeight(image), CGImageGetBytesPerRow(image), CGImageGetBitsPerPixel(image),
CGImageGetBytesPerRow(image),
CGImageGetBitsPerPixel(image),
CGImageGetBitsPerPixel(image) / 8); CGImageGetBitsPerPixel(image) / 8);
CFRelease(imageData); CFRelease(imageData);
CGImageRelease(image); CGImageRelease(image);
return bitmap; return bitmap;
@ -59,22 +53,16 @@ MMBitmapRef copyMMBitmapFromDisplayInRect(MMRectInt32 rect){
MMBitmapRef bitmap; MMBitmapRef bitmap;
Display *display = XOpenDisplay(NULL); Display *display = XOpenDisplay(NULL);
XImage *image = XGetImage(display, XImage *image = XGetImage(display, XDefaultRootWindow(display),
XDefaultRootWindow(display), (int)rect.origin.x, (int)rect.origin.y,
(int)rect.origin.x, (unsigned int)rect.size.w,(unsigned int)rect.size.h,
(int)rect.origin.y, AllPlanes, ZPixmap);
(unsigned int)rect.size.w,
(unsigned int)rect.size.h,
AllPlanes, ZPixmap);
XCloseDisplay(display); XCloseDisplay(display);
if (image == NULL) return NULL; if (image == NULL) { return NULL; }
bitmap = createMMBitmap((uint8_t *)image->data, bitmap = createMMBitmap((uint8_t *)image->data,
rect.size.w, rect.size.w, rect.size.h, (size_t)image->bytes_per_line,
rect.size.h, (uint8_t)image->bits_per_pixel, (uint8_t)image->bits_per_pixel / 8);
(size_t)image->bytes_per_line,
(uint8_t)image->bits_per_pixel,
(uint8_t)image->bits_per_pixel / 8);
image->data = NULL; /* Steal ownership of bitmap data so we don't have to image->data = NULL; /* Steal ownership of bitmap data so we don't have to
* copy it. */ * copy it. */
XDestroyImage(image); XDestroyImage(image);
@ -101,7 +89,7 @@ MMBitmapRef copyMMBitmapFromDisplayInRect(MMRectInt32 rect){
bi.bmiHeader.biClrImportant = 0; bi.bmiHeader.biClrImportant = 0;
screen = GetDC(NULL); /* Get entire screen */ screen = GetDC(NULL); /* Get entire screen */
if (screen == NULL) return NULL; if (screen == NULL) { return NULL; }
/* Get screen data in display device context. */ /* Get screen data in display device context. */
dib = CreateDIBSection(screen, &bi, DIB_RGB_COLORS, &data, NULL, 0); dib = CreateDIBSection(screen, &bi, DIB_RGB_COLORS, &data, NULL, 0);
@ -109,30 +97,20 @@ MMBitmapRef copyMMBitmapFromDisplayInRect(MMRectInt32 rect){
/* Copy the data into a bitmap struct. */ /* Copy the data into a bitmap struct. */
if ((screenMem = CreateCompatibleDC(screen)) == NULL || if ((screenMem = CreateCompatibleDC(screen)) == NULL ||
SelectObject(screenMem, dib) == NULL || SelectObject(screenMem, dib) == NULL ||
!BitBlt(screenMem, !BitBlt(screenMem, (int)0, (int)0, (int)rect.size.w, (int)rect.size.h,
(int)0, screen, rect.origin.x, rect.origin.y, SRCCOPY)
(int)0, ) {
(int)rect.size.w,
(int)rect.size.h,
screen,
rect.origin.x,
rect.origin.y,
SRCCOPY)) {
/* Error copying data. */ /* Error copying data. */
ReleaseDC(NULL, screen); ReleaseDC(NULL, screen);
DeleteObject(dib); DeleteObject(dib);
if (screenMem != NULL) DeleteDC(screenMem); if (screenMem != NULL) { DeleteDC(screenMem); }
return NULL; return NULL;
} }
bitmap = createMMBitmap(NULL, bitmap = createMMBitmap(NULL, rect.size.w, rect.size.h, 4 * rect.size.w,
rect.size.w, (uint8_t)bi.bmiHeader.biBitCount, 4);
rect.size.h,
4 * rect.size.w,
(uint8_t)bi.bmiHeader.biBitCount,
4);
/* Copy the data to our pixel buffer. */ /* Copy the data to our pixel buffer. */
if (bitmap != NULL) { if (bitmap != NULL) {