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

View File

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

View File

@ -80,6 +80,13 @@ 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
)
type (
// Map a map[string]interface{}
Map map[string]interface{}
@ -222,7 +229,7 @@ func SysScale() float64 {
return float64(s)
}
// Scaled x * sys-scale
// Scaled return x * sys_scale
func Scaled(x int) int {
return int(float64(x) * SysScale())
}
@ -261,6 +268,11 @@ func Scale() int {
return dpi[x]
}
// Scale0 return ScaleX() / 0.96
func Scale0() int {
return int(float64(ScaleX()) / 0.96)
}
// Mul mul the scale
func Mul(x int) int {
s := Scale()
@ -328,6 +340,14 @@ func GoCaptureScreen(args ...int) Bitmap {
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
func SaveCapture(spath string, args ...int) {
bit := CaptureScreen(args...)
@ -370,22 +390,23 @@ func MoveMouse(x, y int) {
Move(x, y)
}
// Move move the mouse
// Move move the mouse to (x, y)
func Move(x, y int) {
cx := C.int32_t(x)
cy := C.int32_t(y)
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) {
Drag(x, y, args...)
}
// Drag drag the mouse
// Drag drag the mouse to (x, y)
func Drag(x, y int, args ...string) {
var button C.MMMouseButton = C.LEFT_BUTTON
cx := C.int32_t(x)
cy := C.int32_t(y)
@ -394,6 +415,7 @@ func Drag(x, y int, args ...string) {
}
C.drag_mouse(cx, cy, button)
MilliSleep(MouseSleep)
}
// 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))
MilliSleep(MouseSleep)
return bool(cbool)
}
@ -449,12 +472,12 @@ func MoveArgs(x, y int) (int, int) {
return mx, my
}
// MoveRelative move mose relative
// MoveRelative move mouse with relative
func MoveRelative(x, y int) {
Move(MoveArgs(x, y))
}
// MoveSmoothRelative move mose smooth relative
// MoveSmoothRelative move mouse smooth with relative
func MoveSmoothRelative(x, y int, args ...interface{}) {
mx, my := MoveArgs(x, y)
MoveSmooth(mx, my, args...)
@ -495,6 +518,7 @@ func Click(args ...interface{}) {
}
C.mouse_click(button, double)
MilliSleep(MouseSleep)
}
// MoveClick move and click the mouse
@ -523,6 +547,7 @@ func MouseToggle(togKey string, args ...interface{}) int {
i := C.mouse_toggle(down, button)
C.free(unsafe.Pointer(down))
MilliSleep(MouseSleep)
return int(i)
}
@ -533,9 +558,10 @@ func ScrollMouse(x int, direction string) {
C.scroll_mouse(cx, 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)
func Scroll(x, y int, args ...int) {
@ -549,6 +575,13 @@ func Scroll(x, y int, args ...int) {
cz := C.int(msDelay)
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
@ -645,6 +678,7 @@ func KeyTap(tapKey string, args ...interface{}) string {
C.free(unsafe.Pointer(amod))
C.free(unsafe.Pointer(amodt))
MilliSleep(KeySleep)
return C.GoString(str)
}
@ -695,6 +729,7 @@ func KeyToggle(key string, args ...string) string {
C.free(unsafe.Pointer(cmKey))
C.free(unsafe.Pointer(cmKeyT))
MilliSleep(KeySleep)
return C.GoString(str)
}
@ -811,6 +846,7 @@ func TypeStr(str string, args ...float64) {
MicroSleep(tm)
// }
}
MilliSleep(KeySleep)
}
// 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
//
// 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
// 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
//
// 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,
// use `defer robotgo.FreeBitmap(bit)` to free the bitmap
@ -1435,32 +1471,32 @@ func ShowAlert(title, msg string, args ...string) bool {
var (
// title string
// msg string
defaultButton = "Ok"
cancelButton = "Cancel"
defaultBtn = "Ok"
cancelBtn = "Cancel"
)
if len(args) > 0 {
// title = args[0]
// msg = args[1]
defaultButton = args[0]
defaultBtn = args[0]
}
if len(args) > 1 {
cancelButton = args[1]
cancelBtn = args[1]
}
atitle := C.CString(title)
amsg := C.CString(msg)
adefaultButton := C.CString(defaultButton)
acancelButton := C.CString(cancelButton)
cTitle := C.CString(title)
cMsg := C.CString(msg)
defaultButton := C.CString(defaultBtn)
cancelButton := C.CString(cancelBtn)
cbool := C.show_alert(atitle, amsg, adefaultButton, acancelButton)
cbool := C.show_alert(cTitle, cMsg, defaultButton, cancelButton)
ibool := int(cbool)
C.free(unsafe.Pointer(atitle))
C.free(unsafe.Pointer(amsg))
C.free(unsafe.Pointer(adefaultButton))
C.free(unsafe.Pointer(acancelButton))
C.free(unsafe.Pointer(cTitle))
C.free(unsafe.Pointer(cMsg))
C.free(unsafe.Pointer(defaultButton))
C.free(unsafe.Pointer(cancelButton))
return ibool == 0
}

View File

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