robotgo/robotgo.go

1544 lines
32 KiB
Go
Raw Permalink Normal View History

2018-02-01 16:07:44 +00:00
// Copyright 2016 The go-vgo Project Developers. See the COPYRIGHT
2016-12-12 16:42:37 +00:00
// file at the top-level directory of this distribution and at
2016-12-10 12:07:25 +00:00
// https://github.com/go-vgo/robotgo/blob/master/LICENSE
2016-11-10 06:34:13 +00:00
//
// 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.
2017-02-10 19:57:55 +00:00
/*
2017-02-12 11:08:51 +00:00
2017-02-10 19:57:55 +00:00
Package robotgo Go native cross-platform system automation.
2017-12-08 18:32:23 +00:00
Please make sure Golang, GCC is installed correctly before installing RobotGo;
2017-02-12 11:12:32 +00:00
2017-02-12 11:10:53 +00:00
See Requirements:
https://github.com/go-vgo/robotgo#requirements
2017-02-12 11:08:51 +00:00
Installation:
go get -u github.com/go-vgo/robotgo
2017-02-10 19:57:55 +00:00
*/
2016-10-06 10:41:37 +00:00
package robotgo
/*
//#if defined(IS_MACOSX)
2019-11-13 16:46:15 +00:00
#cgo darwin CFLAGS: -x objective-c -Wno-deprecated-declarations
2017-11-20 14:03:30 +00:00
#cgo darwin LDFLAGS: -framework Cocoa -framework OpenGL -framework IOKit
#cgo darwin LDFLAGS: -framework Carbon -framework CoreFoundation
#cgo darwin LDFLAGS:-L${SRCDIR}/cdeps/mac -lpng -lz
2016-10-06 10:41:37 +00:00
//#elif defined(USE_X11)
// Drop -std=c11
2017-11-20 14:03:30 +00:00
#cgo linux CFLAGS: -I/usr/src
2018-09-16 23:51:21 +00:00
#cgo linux LDFLAGS: -L/usr/src -lpng -lz -lX11 -lXtst -lm
2018-10-07 17:00:16 +00:00
// #cgo linux LDFLAGS: -lX11-xcb -lxcb -lxcb-xkb -lxkbcommon -lxkbcommon-x11
2016-10-06 10:41:37 +00:00
//#endif
2017-11-20 10:07:20 +00:00
// #cgo windows LDFLAGS: -lgdi32 -luser32 -lpng -lz
2017-11-20 13:40:15 +00:00
#cgo windows LDFLAGS: -lgdi32 -luser32
#cgo windows,amd64 LDFLAGS: -L${SRCDIR}/cdeps/win64 -lpng -lz
#cgo windows,386 LDFLAGS: -L${SRCDIR}/cdeps/win32 -lpng -lz
2017-11-20 10:07:20 +00:00
// #include <AppKit/NSEvent.h>
2016-10-06 10:41:37 +00:00
#include "screen/goScreen.h"
#include "mouse/goMouse.h"
#include "key/goKey.h"
2016-10-19 17:14:27 +00:00
#include "bitmap/goBitmap.h"
2018-09-16 23:51:21 +00:00
//#include "event/goEvent.h"
2016-11-05 09:08:35 +00:00
#include "window/goWindow.h"
2016-10-06 10:41:37 +00:00
*/
import "C"
import (
2017-05-20 10:16:14 +00:00
// "fmt"
2018-03-06 09:40:01 +00:00
"image"
2019-05-07 13:42:31 +00:00
// "os"
"reflect"
2017-06-11 12:26:42 +00:00
"runtime"
2018-04-20 14:38:40 +00:00
"strconv"
2017-07-01 11:21:06 +00:00
"strings"
2018-01-16 11:31:19 +00:00
"time"
2016-10-10 07:03:59 +00:00
"unsafe"
2019-05-06 14:39:12 +00:00
2016-10-06 10:41:37 +00:00
// "syscall"
2018-08-08 13:19:17 +00:00
"os/exec"
2017-06-11 11:07:58 +00:00
"github.com/go-vgo/robotgo/clipboard"
2018-03-06 09:40:01 +00:00
"github.com/vcaesar/imgo"
2020-02-11 13:59:36 +00:00
"github.com/vcaesar/tt"
2016-10-06 10:41:37 +00:00
)
2017-01-01 07:25:31 +00:00
const (
2018-07-21 11:42:06 +00:00
// Version get the robotgo version
2019-11-13 16:46:15 +00:00
Version = "v0.90.0.940, Sierra Nevada!"
)
2018-07-21 11:42:06 +00:00
// GetVersion get the robotgo version
func GetVersion() string {
return Version
}
type (
2018-02-01 16:07:44 +00:00
// Map a map[string]interface{}
Map map[string]interface{}
2018-10-09 14:56:30 +00:00
// CHex define CHex as c rgb Hex type (C.MMRGBHex)
CHex C.MMRGBHex
2018-10-09 14:56:30 +00:00
// CBitmap define CBitmap as C.MMBitmapRef type
2017-12-24 15:01:22 +00:00
CBitmap C.MMBitmapRef
2017-01-01 07:25:31 +00:00
)
// Bitmap is Bitmap struct
type Bitmap struct {
2019-03-30 15:58:03 +00:00
ImgBuf *uint8
Width int
Height int
Bytewidth int
2019-03-30 15:58:03 +00:00
BitsPixel uint8
BytesPerPixel uint8
}
// MPoint is MPoint struct
type MPoint struct {
x int
y int
}
// Try handler(err)
func Try(fun func(), handler func(interface{})) {
defer func() {
if err := recover(); err != nil {
handler(err)
}
}()
fun()
}
2018-12-12 20:58:23 +00:00
// 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) {
2018-01-16 11:31:19 +00:00
time.Sleep(time.Duration(tm) * time.Second)
}
2018-03-12 15:05:40 +00:00
// MicroSleep time C.microsleep(tm)
func MicroSleep(tm float64) {
C.microsleep(C.double(tm))
}
// GoString teans C.char to string
func GoString(char *C.char) string {
return C.GoString(char)
}
2016-10-15 11:43:58 +00:00
/*
_______. ______ .______ _______ _______ .__ __.
/ | / || _ \ | ____|| ____|| \ | |
| (----`| ,----'| |_) | | |__ | |__ | \| |
\ \ | | | / | __| | __| | . ` |
.----) | | `----.| |\ \----.| |____ | |____ | |\ |
|_______/ \______|| _| `._____||_______||_______||__| \__|
*/
// ToMMRGBHex trans CHex to C.MMRGBHex
func ToMMRGBHex(hex CHex) C.MMRGBHex {
return C.MMRGBHex(hex)
}
2019-03-28 16:57:46 +00:00
// 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 teans *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
}
// 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))
}
2017-12-23 15:38:35 +00:00
// GetPxColor get pixel color return C.MMRGBHex
func GetPxColor(x, y int) C.MMRGBHex {
cx := C.int32_t(x)
cy := C.int32_t(y)
2017-12-23 15:38:35 +00:00
2017-12-29 12:18:28 +00:00
color := C.get_px_color(cx, cy)
2017-12-23 15:38:35 +00:00
return color
}
// GetPixelColor get pixel color return string
2016-10-24 05:50:42 +00:00
func GetPixelColor(x, y int) string {
cx := C.int32_t(x)
cy := C.int32_t(y)
2017-12-23 18:07:41 +00:00
2017-12-29 12:18:28 +00:00
color := C.get_pixel_color(cx, cy)
2016-10-15 11:43:58 +00:00
gcolor := C.GoString(color)
C.free(unsafe.Pointer(color))
2016-10-15 11:43:58 +00:00
return gcolor
}
2019-03-28 16:56:46 +00:00
// GetMouseColor get the mouse pos's color
func GetMouseColor() string {
x, y := GetMousePos()
return GetPixelColor(x, y)
}
2018-07-09 15:29:27 +00:00
// ScaleX get primary display horizontal DPI scale factor
func ScaleX() int {
return int(C.scale_x())
2018-07-09 15:29:27 +00:00
}
2019-12-04 14:08:09 +00:00
// SysScale get the sys scale
func SysScale() float64 {
s := C.sys_scale()
return float64(s)
}
2020-03-08 14:09:24 +00:00
// Scaled x * sys-scale
func Scaled(x int) int {
return int(float64(x) * SysScale())
}
2018-07-09 15:29:27 +00:00
// ScaleY get primary display vertical DPI scale factor
func ScaleY() int {
return int(C.scale_y())
2018-07-09 15:29:27 +00:00
}
// GetScreenSize get the screen size
2016-11-12 14:45:42 +00:00
func GetScreenSize() (int, int) {
2017-12-29 12:18:28 +00:00
size := C.get_screen_size()
2017-05-20 10:16:14 +00:00
// fmt.Println("...", size, size.width)
return int(size.w), int(size.h)
2016-10-15 11:43:58 +00:00
}
2018-07-09 15:29:27 +00:00
// Scale get the screen scale
func Scale() int {
2018-07-09 15:33:59 +00:00
dpi := map[int]int{
2018-07-24 14:53:16 +00:00
0: 100,
2018-07-24 13:26:53 +00:00
// DPI Scaling Level
2018-07-09 15:29:27 +00:00
96: 100,
120: 125,
144: 150,
2018-10-26 16:06:34 +00:00
168: 175,
2018-07-09 15:29:27 +00:00
192: 200,
2018-10-26 16:06:34 +00:00
216: 225,
2018-07-24 13:26:53 +00:00
// Custom DPI
240: 250,
288: 300,
384: 400,
480: 500,
2018-07-09 15:29:27 +00:00
}
x := ScaleX()
2018-07-09 15:33:59 +00:00
return dpi[x]
2018-07-09 15:29:27 +00:00
}
2019-01-09 17:24:06 +00:00
// Mul mul the scale
func Mul(x int) int {
s := Scale()
return x * s / 100
}
2018-07-09 15:29:27 +00:00
// GetScaleSize get the screen scale size
func GetScaleSize() (int, int) {
x, y := GetScreenSize()
s := Scale()
return x * s / 100, y * s / 100
}
2019-07-07 15:58:22 +00:00
// SetXDisplayName set XDisplay name (Linux)
2016-10-15 11:43:58 +00:00
func SetXDisplayName(name string) string {
cname := C.CString(name)
2017-12-29 12:18:28 +00:00
str := C.set_XDisplay_name(cname)
2019-07-07 15:58:22 +00:00
2016-10-15 11:43:58 +00:00
gstr := C.GoString(str)
C.free(unsafe.Pointer(cname))
2016-10-15 11:43:58 +00:00
return gstr
}
2019-07-07 15:58:22 +00:00
// GetXDisplayName get XDisplay name (Linux)
2017-01-02 15:10:31 +00:00
func GetXDisplayName() string {
2017-12-29 12:18:28 +00:00
name := C.get_XDisplay_name()
2017-01-02 15:10:31 +00:00
gname := C.GoString(name)
C.free(unsafe.Pointer(name))
2017-01-02 15:10:31 +00:00
return gname
}
// CaptureScreen capture the screen return bitmap(c struct),
2018-08-03 15:45:09 +00:00
// use `defer robotgo.FreeBitmap(bitmap)` to free the bitmap
2018-09-10 17:54:35 +00:00
//
// robotgo.CaptureScreen(x, y, w, h int)
2016-10-24 06:06:20 +00:00
func CaptureScreen(args ...int) C.MMBitmapRef {
var x, y, w, h C.int32_t
2017-07-01 12:24:35 +00:00
2018-05-06 10:04:31 +00:00
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])
2018-05-06 10:04:31 +00:00
} else {
2016-10-24 06:06:20 +00:00
x = 0
y = 0
2017-12-20 15:01:56 +00:00
// Get screen size.
var displaySize C.MMSizeInt32
2016-10-24 06:06:20 +00:00
displaySize = C.getMainDisplaySize()
w = displaySize.w
h = displaySize.h
2018-05-06 10:04:31 +00:00
}
2016-10-24 06:06:20 +00:00
2017-11-09 14:40:58 +00:00
bit := C.capture_screen(x, y, w, h)
2016-10-20 09:35:53 +00:00
return bit
}
2017-11-09 14:40:58 +00:00
// GoCaptureScreen capture the screen and return bitmap(go struct)
func GoCaptureScreen(args ...int) Bitmap {
2019-01-09 17:24:06 +00:00
bit := CaptureScreen(args...)
2018-07-28 15:32:43 +00:00
defer FreeBitmap(bit)
2017-11-09 14:40:58 +00:00
2018-04-10 12:33:04 +00:00
return ToBitmap(bit)
2017-11-09 14:40:58 +00:00
}
// SaveCapture capture screen and save
func SaveCapture(spath string, args ...int) {
2018-10-07 17:00:16 +00:00
bit := CaptureScreen(args...)
SaveBitmap(bit, spath)
2018-07-28 15:32:43 +00:00
FreeBitmap(bit)
}
2016-10-06 10:41:37 +00:00
/*
2016-10-27 16:32:55 +00:00
.___ ___. ______ __ __ _______. _______
| \/ | / __ \ | | | | / || ____|
| \ / | | | | | | | | | | (----`| |__
| |\/| | | | | | | | | | \ \ | __|
| | | | | `--' | | `--' | .----) | | |____
|__| |__| \______/ \______/ |_______/ |_______|
2016-10-06 10:41:37 +00:00
*/
// CheckMouse check the mouse button
func CheckMouse(btn string) C.MMMouseButton {
// button = args[0].(C.MMMouseButton)
if btn == "left" {
return C.LEFT_BUTTON
}
if btn == "center" {
return C.CENTER_BUTTON
}
if btn == "right" {
return C.RIGHT_BUTTON
}
return C.LEFT_BUTTON
}
2017-06-11 05:32:23 +00:00
// MoveMouse move the mouse
2016-10-23 11:42:27 +00:00
func MoveMouse(x, y int) {
2017-12-20 15:01:56 +00:00
// C.size_t int
2018-07-19 16:14:21 +00:00
Move(x, y)
2016-10-06 10:41:37 +00:00
}
2017-06-11 05:32:23 +00:00
// Move move the mouse
2016-12-17 08:47:03 +00:00
func Move(x, y int) {
cx := C.int32_t(x)
cy := C.int32_t(y)
C.move_mouse(cx, cy)
2016-12-17 08:47:03 +00:00
}
2017-06-11 05:32:23 +00:00
// DragMouse drag the mouse
2018-12-27 16:44:38 +00:00
func DragMouse(x, y int, args ...string) {
Drag(x, y, args...)
2016-10-06 10:41:37 +00:00
}
2017-06-11 05:32:23 +00:00
// Drag drag the mouse
2018-12-27 16:44:38 +00:00
func Drag(x, y int, args ...string) {
var button C.MMMouseButton = C.LEFT_BUTTON
cx := C.int32_t(x)
cy := C.int32_t(y)
2018-12-27 16:44:38 +00:00
if len(args) > 0 {
button = CheckMouse(args[0])
}
C.drag_mouse(cx, cy, button)
2016-12-17 08:50:57 +00:00
}
2019-06-05 16:59:50 +00:00
// DragSmooth drag the mouse smooth
func DragSmooth(x, y int, args ...interface{}) {
MouseToggle("down")
MoveSmooth(x, y, args...)
MouseToggle("up")
}
2018-02-03 09:32:21 +00:00
// MoveMouseSmooth move the mouse smooth,
// moves mouse to x, y human like, with the mouse button up.
2018-02-06 13:32:39 +00:00
func MoveMouseSmooth(x, y int, args ...interface{}) bool {
2018-07-19 16:14:21 +00:00
return MoveSmooth(x, y, args...)
2016-10-06 10:41:37 +00:00
}
2018-02-03 09:32:21 +00:00
// MoveSmooth move the mouse smooth,
// moves mouse to x, y human like, with the mouse button up.
2018-09-14 21:35:42 +00:00
//
// robotgo.MoveSmooth(x, y int, low, high float64, mouseDelay int)
2018-02-06 13:32:39 +00:00
func MoveSmooth(x, y int, args ...interface{}) bool {
cx := C.int32_t(x)
cy := C.int32_t(y)
2016-12-17 08:53:41 +00:00
var (
mouseDelay = 10
2017-12-29 12:24:47 +00:00
low C.double
high C.double
2016-12-17 08:53:41 +00:00
)
if len(args) > 2 {
mouseDelay = args[2].(int)
}
2017-12-30 09:49:15 +00:00
if len(args) > 1 {
low = C.double(args[0].(float64))
high = C.double(args[1].(float64))
2017-08-12 10:42:19 +00:00
} else {
2018-02-06 10:21:08 +00:00
low = 1.0
high = 3.0
2017-08-12 10:42:19 +00:00
}
2016-12-17 08:53:41 +00:00
2018-02-06 13:32:39 +00:00
cbool := C.move_mouse_smooth(cx, cy, low, high, C.int(mouseDelay))
return bool(cbool)
2016-12-17 08:53:41 +00:00
}
// MoveArgs move mose relative args
func MoveArgs(x, y int) (int, int) {
mx, my := GetMousePos()
mx = mx + x
my = my + y
return mx, my
}
// MoveRelative move mose relative
func MoveRelative(x, y int) {
Move(MoveArgs(x, y))
}
// MoveSmoothRelative move mose smooth relative
func MoveSmoothRelative(x, y int, args ...interface{}) {
mx, my := MoveArgs(x, y)
MoveSmooth(mx, my, args...)
}
2018-06-18 13:13:34 +00:00
// GetMousePos get mouse's portion
2016-10-23 11:42:27 +00:00
func GetMousePos() (int, int) {
2018-02-06 10:21:08 +00:00
pos := C.get_mouse_pos()
2019-01-09 17:24:06 +00:00
2016-10-23 11:42:27 +00:00
x := int(pos.x)
y := int(pos.y)
2019-01-09 17:24:06 +00:00
2016-10-23 11:42:27 +00:00
return x, y
2016-10-06 10:41:37 +00:00
}
2017-06-11 05:32:23 +00:00
// MouseClick click the mouse
2018-09-14 21:32:31 +00:00
//
// robotgo.MouseClick(button string, double bool)
2016-12-17 08:47:03 +00:00
func MouseClick(args ...interface{}) {
Click(args...)
2016-12-17 07:05:48 +00:00
}
2017-06-11 05:32:23 +00:00
// Click click the mouse
2018-09-14 21:32:31 +00:00
//
// robotgo.Click(button string, double bool)
2016-12-17 08:47:03 +00:00
func Click(args ...interface{}) {
2017-07-01 12:24:35 +00:00
var (
button C.MMMouseButton = C.LEFT_BUTTON
2017-07-01 12:24:35 +00:00
double C.bool
)
if len(args) > 0 {
button = CheckMouse(args[0].(string))
}
if len(args) > 1 {
2016-11-12 07:17:52 +00:00
double = C.bool(args[1].(bool))
}
C.mouse_click(button, double)
2016-10-06 10:41:37 +00:00
}
2017-02-24 12:14:04 +00:00
// MoveClick move and click the mouse
2018-09-14 21:35:42 +00:00
//
// robotgo.MoveClick(x, y int, button string, double bool)
2017-01-03 10:39:22 +00:00
func MoveClick(x, y int, args ...interface{}) {
MoveMouse(x, y)
MouseClick(args...)
2017-01-03 10:39:22 +00:00
}
2018-01-13 13:49:13 +00:00
// MovesClick move smooth and click the mouse
func MovesClick(x, y int, args ...interface{}) {
MoveSmooth(x, y)
MouseClick(args...)
2018-01-13 13:49:13 +00:00
}
2017-06-11 05:32:23 +00:00
// MouseToggle toggle the mouse
func MouseToggle(togKey string, args ...interface{}) int {
2018-07-19 16:06:34 +00:00
var button C.MMMouseButton = C.LEFT_BUTTON
2018-07-19 16:06:34 +00:00
if len(args) > 0 {
button = CheckMouse(args[0].(string))
2018-07-19 16:06:34 +00:00
}
2018-06-07 15:36:29 +00:00
down := C.CString(togKey)
i := C.mouse_toggle(down, button)
C.free(unsafe.Pointer(down))
return int(i)
2016-10-06 10:41:37 +00:00
}
2017-06-11 05:32:23 +00:00
// ScrollMouse scroll the mouse
func ScrollMouse(x int, direction string) {
2016-10-23 11:42:27 +00:00
cx := C.size_t(x)
cy := C.CString(direction)
2018-02-25 07:22:52 +00:00
C.scroll_mouse(cx, cy)
C.free(unsafe.Pointer(cy))
2018-02-25 07:22:52 +00:00
}
2018-02-25 07:43:29 +00:00
// Scroll scroll the mouse with x, y
2018-09-10 17:54:35 +00:00
//
// robotgo.Scroll(x, y, msDelay int)
2018-02-25 07:22:52 +00:00
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)
2018-02-25 07:54:12 +00:00
cz := C.int(msDelay)
2018-02-25 07:22:52 +00:00
C.scroll(cx, cy, cz)
2016-10-06 10:41:37 +00:00
}
// SetMouseDelay set mouse delay
func SetMouseDelay(delay int) {
cdelay := C.size_t(delay)
C.set_mouse_delay(cdelay)
}
2016-10-06 10:41:37 +00:00
/*
2016-10-27 16:32:55 +00:00
__ ___ ___________ ____ .______ ______ ___ .______ _______
| |/ / | ____\ \ / / | _ \ / __ \ / \ | _ \ | \
| ' / | |__ \ \/ / | |_) | | | | | / ^ \ | |_) | | .--. |
| < | __| \_ _/ | _ < | | | | / /_\ \ | / | | | |
| . \ | |____ | | | |_) | | `--' | / _____ \ | |\ \----.| '--' |
|__|\__\ |_______| |__| |______/ \______/ /__/ \__\ | _| `._____||_______/
2016-10-06 10:41:37 +00:00
*/
2016-12-09 12:56:56 +00:00
// KeyTap tap the keyboard code;
2017-02-26 10:18:54 +00:00
//
2017-02-24 12:14:04 +00:00
// See keys:
2017-02-24 12:33:58 +00:00
// https://github.com/go-vgo/robotgo/blob/master/docs/keys.md
//
func KeyTap(tapKey string, args ...interface{}) string {
2017-07-01 12:24:35 +00:00
var (
2017-12-23 18:07:41 +00:00
akey string
keyT = "null"
keyArr []string
num int
2017-12-23 17:56:02 +00:00
keyDelay = 10
2017-07-01 12:24:35 +00:00
)
2017-12-20 15:01:56 +00:00
// var ckeyArr []*C.char
ckeyArr := make([](*C.char), 0)
2016-11-13 15:46:33 +00:00
2019-03-29 16:12:52 +00:00
// zkey := C.CString(args[0])
zkey := C.CString(tapKey)
defer C.free(unsafe.Pointer(zkey))
2019-03-30 15:58:03 +00:00
2019-05-15 14:55:44 +00:00
if len(args) > 2 && (reflect.TypeOf(args[2]) != reflect.TypeOf(num)) {
2019-03-29 16:07:32 +00:00
num = len(args)
for i := 0; i < num; i++ {
s := args[i].(string)
ckeyArr = append(ckeyArr, (*C.char)(unsafe.Pointer(C.CString(s))))
}
str := C.key_Taps(zkey, (**C.char)(unsafe.Pointer(&ckeyArr[0])),
C.int(num), 0)
return C.GoString(str)
}
2018-12-11 17:12:16 +00:00
if len(args) > 0 {
2018-05-30 16:05:07 +00:00
if reflect.TypeOf(args[0]) == reflect.TypeOf(keyArr) {
2018-05-30 16:05:07 +00:00
keyArr = args[0].([]string)
2017-12-23 17:56:02 +00:00
num = len(keyArr)
2017-01-14 18:37:49 +00:00
for i := 0; i < num; i++ {
2017-12-23 17:56:02 +00:00
ckeyArr = append(ckeyArr, (*C.char)(unsafe.Pointer(C.CString(keyArr[i]))))
}
2018-05-30 16:05:07 +00:00
if len(args) > 1 {
keyDelay = args[1].(int)
2017-12-23 17:56:02 +00:00
}
} else {
2018-05-30 16:05:07 +00:00
akey = args[0].(string)
2018-05-30 16:05:07 +00:00
if len(args) > 1 {
if reflect.TypeOf(args[1]) == reflect.TypeOf(akey) {
keyT = args[1].(string)
2019-05-15 14:55:44 +00:00
if len(args) > 2 {
keyDelay = args[2].(int)
}
2017-12-23 18:07:41 +00:00
} else {
2018-05-30 16:05:07 +00:00
keyDelay = args[1].(int)
2017-12-23 18:07:41 +00:00
}
2017-12-23 17:56:02 +00:00
}
}
2016-11-13 15:46:33 +00:00
2018-12-11 17:12:16 +00:00
} else {
2016-11-13 10:37:41 +00:00
akey = "null"
2017-12-23 17:56:02 +00:00
keyArr = []string{"null"}
2018-12-11 17:12:16 +00:00
}
2017-12-23 17:56:02 +00:00
if akey == "" && len(keyArr) != 0 {
str := C.key_Taps(zkey, (**C.char)(unsafe.Pointer(&ckeyArr[0])),
2017-12-23 18:07:41 +00:00
C.int(num), C.int(keyDelay))
return C.GoString(str)
}
2016-10-10 07:03:59 +00:00
2018-12-11 17:12:16 +00:00
amod := C.CString(akey)
amodt := C.CString(keyT)
str := C.key_tap(zkey, amod, amodt, C.int(keyDelay))
2018-12-11 17:12:16 +00:00
C.free(unsafe.Pointer(amod))
C.free(unsafe.Pointer(amodt))
return C.GoString(str)
2016-10-08 07:11:16 +00:00
}
2017-06-11 05:32:23 +00:00
// KeyToggle toggle the keyboard
2017-02-26 10:18:54 +00:00
//
// See keys:
// https://github.com/go-vgo/robotgo/blob/master/docs/keys.md
//
func KeyToggle(key string, args ...string) string {
ckey := C.CString(key)
defer C.free(unsafe.Pointer(ckey))
ckeyArr := make([](*C.char), 0)
if len(args) > 3 {
num := len(args)
for i := 0; i < num; i++ {
ckeyArr = append(ckeyArr, (*C.char)(unsafe.Pointer(C.CString(args[i]))))
}
str := C.key_Toggles(ckey, (**C.char)(unsafe.Pointer(&ckeyArr[0])), C.int(num))
return C.GoString(str)
}
2017-07-01 12:24:35 +00:00
var (
2018-10-11 17:07:21 +00:00
down, mKey, mKeyT = "null", "null", "null"
// keyDelay = 10
2017-07-01 12:24:35 +00:00
)
if len(args) > 0 {
down = args[0]
2018-02-03 08:24:49 +00:00
if len(args) > 1 {
mKey = args[1]
2017-12-20 15:01:56 +00:00
if len(args) > 2 {
mKeyT = args[2]
2018-02-03 08:24:49 +00:00
}
2017-12-20 15:01:56 +00:00
}
2018-02-03 08:24:49 +00:00
}
2016-10-08 07:11:16 +00:00
2018-10-11 17:07:21 +00:00
cdown := C.CString(down)
2018-04-30 15:38:34 +00:00
cmKey := C.CString(mKey)
2017-12-23 17:56:02 +00:00
cmKeyT := C.CString(mKeyT)
2018-10-11 17:07:21 +00:00
str := C.key_toggle(ckey, cdown, cmKey, cmKeyT)
// str := C.key_Toggle(ckey, cdown, cmKey, cmKeyT, C.int(keyDelay))
2018-12-26 16:55:17 +00:00
C.free(unsafe.Pointer(cdown))
C.free(unsafe.Pointer(cmKey))
C.free(unsafe.Pointer(cmKeyT))
2016-12-22 16:54:08 +00:00
return C.GoString(str)
2016-10-06 10:41:37 +00:00
}
// ReadAll read string from clipboard
func ReadAll() (string, error) {
return clipboard.ReadAll()
}
// WriteAll write string to clipboard
func WriteAll(text string) error {
return clipboard.WriteAll(text)
}
2018-01-16 10:01:19 +00:00
// CharCodeAt char code at utf-8
func CharCodeAt(s string, n int) rune {
i := 0
for _, r := range s {
if i == n {
return r
}
i++
}
return 0
}
// UnicodeType tap uint32 unicode
func UnicodeType(str uint32) {
cstr := C.uint(str)
C.unicodeType(cstr)
}
2018-04-20 15:19:22 +00:00
func toUC(text string) []string {
2018-04-20 14:38:40 +00:00
var uc []string
2019-12-10 11:40:24 +00:00
for _, r := range text {
textQ := strconv.QuoteToASCII(string(r))
textUnQ := textQ[1 : len(textQ)-1]
2018-04-20 14:38:40 +00:00
2019-12-10 11:40:24 +00:00
st := strings.Replace(textUnQ, "\\u", "U", -1)
uc = append(uc, st)
2018-04-20 14:38:40 +00:00
}
return uc
}
2018-04-20 15:19:22 +00:00
func inputUTF(str string) {
2018-04-20 15:04:40 +00:00
cstr := C.CString(str)
C.input_utf(cstr)
C.free(unsafe.Pointer(cstr))
2018-04-20 15:04:40 +00:00
}
2018-04-22 15:34:04 +00:00
// TypeStr send a string, support UTF-8
2018-09-10 17:54:35 +00:00
//
// robotgo.TypeStr(string: The string to send, float64: microsleep time, x11)
2018-04-22 15:34:04 +00:00
func TypeStr(str string, args ...float64) {
var tm, tm1 = 0.0, 7.0
2018-04-22 15:34:04 +00:00
if len(args) > 0 {
tm = args[0]
}
if len(args) > 1 {
tm1 = args[1]
}
2018-04-22 15:34:04 +00:00
2018-04-20 14:38:40 +00:00
if runtime.GOOS == "linux" {
2018-04-20 15:19:22 +00:00
strUc := toUC(str)
2018-04-20 14:38:40 +00:00
for i := 0; i < len(strUc); i++ {
rstr := []rune(strUc[i])
if len(rstr) <= 1 {
ustr := uint32(CharCodeAt(strUc[i], 0))
UnicodeType(ustr)
} else {
inputUTF(strUc[i])
MicroSleep(tm1)
}
2019-12-15 21:55:15 +00:00
MicroSleep(tm)
2018-04-20 14:38:40 +00:00
}
2018-06-18 13:13:34 +00:00
return
}
for i := 0; i < len([]rune(str)); i++ {
ustr := uint32(CharCodeAt(str, i))
UnicodeType(ustr)
// if len(args) > 0 {
MicroSleep(tm)
2018-06-18 13:13:34 +00:00
// }
2018-01-16 10:01:19 +00:00
}
}
// PasteStr paste a string, support UTF-8
func PasteStr(str string) string {
clipboard.WriteAll(str)
if runtime.GOOS == "darwin" {
return KeyTap("v", "command")
}
return KeyTap("v", "control")
}
2018-04-22 15:34:04 +00:00
// TypeString send a string, support unicode
2019-12-15 21:27:53 +00:00
// TypeStr(string: The string to send), Wno-deprecated
2020-03-08 14:00:42 +00:00
func TypeString(str string, delay ...int) {
2020-02-11 13:59:36 +00:00
tt.Drop("TypeString", "TypeStr")
2020-03-08 14:00:42 +00:00
var cdelay C.size_t
2018-04-22 15:34:04 +00:00
cstr := C.CString(str)
2020-03-08 14:00:42 +00:00
if len(delay) > 0 {
cdelay = C.size_t(delay[0])
}
C.type_string_delayed(cstr, cdelay)
2018-04-22 15:34:04 +00:00
C.free(unsafe.Pointer(cstr))
2016-10-06 10:41:37 +00:00
}
// TypeStrDelay type string delayed
2018-04-22 15:34:04 +00:00
func TypeStrDelay(str string, delay int) {
TypeStr(str)
Sleep(delay)
2016-12-12 16:42:37 +00:00
}
2017-06-11 05:32:23 +00:00
// TypeStringDelayed type string delayed, Wno-deprecated
2018-04-22 15:34:04 +00:00
func TypeStringDelayed(str string, delay int) {
2020-02-11 13:59:36 +00:00
tt.Drop("TypeStringDelayed", "TypeStrDelay")
TypeStrDelay(str, delay)
2016-10-06 10:41:37 +00:00
}
2017-06-11 05:32:23 +00:00
// SetKeyDelay set keyboard delay
2018-04-22 15:55:49 +00:00
func SetKeyDelay(delay int) {
C.set_keyboard_delay(C.size_t(delay))
2016-12-11 09:14:14 +00:00
}
// SetKeyboardDelay set keyboard delay, Wno-deprecated,
// this function will be removed in version v1.0.0
2018-04-22 15:55:49 +00:00
func SetKeyboardDelay(delay int) {
2020-02-11 13:59:36 +00:00
tt.Drop("SetKeyboardDelay", "SetKeyDelay")
SetKeyDelay(delay)
2016-10-06 10:41:37 +00:00
}
// SetDelay set the key and mouse delay
func SetDelay(d ...int) {
v := 10
if len(d) > 0 {
v = d[0]
}
SetMouseDelay(v)
SetKeyDelay(v)
}
2016-10-06 10:41:37 +00:00
/*
2016-10-15 11:43:58 +00:00
.______ __ .___________..___ ___. ___ .______
| _ \ | | | || \/ | / \ | _ \
| |_) | | | `---| |----`| \ / | / ^ \ | |_) |
| _ < | | | | | |\/| | / /_\ \ | ___/
| |_) | | | | | | | | | / _____ \ | |
|______/ |__| |__| |__| |__| /__/ \__\ | _|
2016-10-06 10:41:37 +00:00
*/
2016-12-09 12:56:56 +00:00
2019-03-11 14:26:58 +00:00
// GetText get the image text by tesseract ocr
//
// robotgo.GetText(imgPath, lang string)
func GetText(imgPath string, args ...string) (string, error) {
var lang = "eng"
if len(args) > 0 {
lang = args[0]
if lang == "zh" {
lang = "chi_sim"
}
}
body, err := exec.Command("tesseract", imgPath,
"stdout", "-l", lang).Output()
if err != nil {
return "", err
}
return string(body), nil
}
2018-04-10 12:33:04 +00:00
// ToBitmap trans C.MMBitmapRef to Bitmap
func ToBitmap(bit C.MMBitmapRef) Bitmap {
bitmap := Bitmap{
2019-03-30 15:58:03 +00:00
ImgBuf: (*uint8)(bit.imageBuffer),
2018-04-10 12:33:04 +00:00
Width: int(bit.width),
Height: int(bit.height),
Bytewidth: int(bit.bytewidth),
2019-03-30 15:58:03 +00:00
BitsPixel: uint8(bit.bitsPerPixel),
2018-04-10 12:33:04 +00:00
BytesPerPixel: uint8(bit.bytesPerPixel),
}
return bitmap
}
2018-06-16 11:17:06 +00:00
// ToCBitmap trans Bitmap to C.MMBitmapRef
func ToCBitmap(bit Bitmap) C.MMBitmapRef {
cbitmap := C.createMMBitmap(
2019-03-30 15:58:03 +00:00
(*C.uint8_t)(bit.ImgBuf),
2018-06-16 11:17:06 +00:00
C.size_t(bit.Width),
C.size_t(bit.Height),
C.size_t(bit.Bytewidth),
2019-03-30 15:58:03 +00:00
C.uint8_t(bit.BitsPixel),
2018-06-16 11:17:06 +00:00
C.uint8_t(bit.BytesPerPixel),
)
return cbitmap
}
2019-03-16 13:12:07 +00:00
// ToBitmapBytes saves Bitmap to bitmap format in bytes
func ToBitmapBytes(bit C.MMBitmapRef) []byte {
var len C.size_t
ptr := C.saveMMBitmapAsBytes(bit, &len)
if int(len) < 0 {
return nil
}
2019-03-16 13:23:52 +00:00
2019-03-16 13:12:07 +00:00
bs := C.GoBytes(unsafe.Pointer(ptr), C.int(len))
C.free(unsafe.Pointer(ptr))
return bs
}
2018-04-10 12:33:04 +00:00
// ToMMBitmapRef trans CBitmap to C.MMBitmapRef
func ToMMBitmapRef(bit CBitmap) C.MMBitmapRef {
return C.MMBitmapRef(bit)
}
// TostringBitmap tostring bitmap to string
func TostringBitmap(bit C.MMBitmapRef) string {
strBit := C.tostring_bitmap(bit)
return C.GoString(strBit)
}
// TocharBitmap tostring bitmap to C.char
func TocharBitmap(bit C.MMBitmapRef) *C.char {
strBit := C.tostring_bitmap(bit)
return strBit
}
func internalFindBitmap(bit, sbit C.MMBitmapRef, tolerance float64) (int, int) {
pos := C.find_bitmap(bit, sbit, C.float(tolerance))
// fmt.Println("pos----", pos)
return int(pos.x), int(pos.y)
}
2018-09-30 13:41:35 +00:00
// FindCBitmap find bitmap's pos by CBitmap
func FindCBitmap(bmp CBitmap, args ...interface{}) (int, int) {
return FindBitmap(ToMMBitmapRef(bmp), args...)
}
2018-07-06 11:29:38 +00:00
// FindBitmap find the bitmap's pos
2018-04-09 09:39:47 +00:00
//
2018-04-09 09:45:07 +00:00
// robotgo.FindBitmap(bitmap, subbitamp C.MMBitmapRef, tolerance float64)
2018-04-09 09:39:47 +00:00
//
2018-07-06 11:29:38 +00:00
// |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.
//
2018-08-03 15:45:09 +00:00
// This method only automatically free the internal bitmap,
// use `defer robotgo.FreeBitmap(bit)` to free the bitmap
2018-05-30 15:52:09 +00:00
func FindBitmap(bit C.MMBitmapRef, args ...interface{}) (int, int) {
2017-08-24 12:34:45 +00:00
var (
sbit C.MMBitmapRef
tolerance = 0.01
2017-08-24 12:34:45 +00:00
)
2019-05-15 14:58:39 +00:00
if len(args) > 0 && args[0] != nil {
2018-05-30 15:52:09 +00:00
sbit = args[0].(C.MMBitmapRef)
2017-08-24 12:34:45 +00:00
} else {
sbit = CaptureScreen()
}
2018-05-30 15:52:09 +00:00
if len(args) > 1 {
tolerance = args[1].(float64)
2017-08-24 12:34:45 +00:00
}
fx, fy := internalFindBitmap(bit, sbit, tolerance)
// FreeBitmap(bit)
if len(args) <= 0 {
FreeBitmap(sbit)
}
return fx, fy
2017-08-24 12:34:45 +00:00
}
2018-04-09 09:34:02 +00:00
// FindPic finding the image by path
2018-04-09 09:39:47 +00:00
//
2018-04-09 09:45:07 +00:00
// robotgo.FindPic(path string, subbitamp C.MMBitmapRef, tolerance float64)
2018-04-09 09:39:47 +00:00
//
2018-08-03 15:45:09 +00:00
// This method only automatically free the internal bitmap,
// use `defer robotgo.FreeBitmap(bit)` to free the bitmap
2018-04-09 09:39:47 +00:00
func FindPic(path string, args ...interface{}) (int, int) {
2018-04-09 09:34:02 +00:00
var (
sbit C.MMBitmapRef
tolerance = 0.01
2018-04-09 09:34:02 +00:00
)
openbit := OpenBitmap(path)
2019-05-15 14:58:39 +00:00
if len(args) > 0 && args[0] != nil {
2018-04-09 09:34:02 +00:00
sbit = args[0].(C.MMBitmapRef)
} else {
sbit = CaptureScreen()
}
if len(args) > 1 {
tolerance = args[1].(float64)
}
fx, fy := internalFindBitmap(openbit, sbit, tolerance)
FreeBitmap(openbit)
if len(args) <= 0 {
FreeBitmap(sbit)
}
2018-04-09 09:34:02 +00:00
2018-04-09 09:39:47 +00:00
return fx, fy
2018-04-09 09:34:02 +00:00
}
2017-11-04 13:57:09 +00:00
// FindEveryBitmap find the every bitmap
2018-05-30 15:56:04 +00:00
func FindEveryBitmap(bit C.MMBitmapRef, args ...interface{}) (int, int) {
2017-11-04 13:57:09 +00:00
var (
sbit C.MMBitmapRef
tolerance C.float = 0.01
2017-11-04 13:57:09 +00:00
lpos C.MMPoint
)
2019-05-15 14:58:39 +00:00
if len(args) > 0 && args[0] != nil {
2018-05-30 15:56:04 +00:00
sbit = args[0].(C.MMBitmapRef)
2017-11-04 13:57:09 +00:00
} else {
sbit = CaptureScreen()
}
2018-05-30 15:56:04 +00:00
if len(args) > 1 {
tolerance = C.float(args[1].(float64))
2017-11-04 13:57:09 +00:00
}
2018-05-30 15:56:04 +00:00
if len(args) > 2 {
lpos.x = C.size_t(args[2].(int))
2017-11-04 13:57:09 +00:00
lpos.y = 0
} else {
lpos.x = 0
lpos.y = 0
}
2018-05-30 15:56:04 +00:00
if len(args) > 3 {
lpos.x = C.size_t(args[2].(int))
lpos.y = C.size_t(args[3].(int))
2017-11-04 13:57:09 +00:00
}
pos := C.find_every_bitmap(bit, sbit, tolerance, &lpos)
// FreeBitmap(bit)
if len(args) <= 0 {
FreeBitmap(sbit)
}
2018-07-28 11:24:49 +00:00
2017-11-04 13:57:09 +00:00
// fmt.Println("pos----", pos)
return int(pos.x), int(pos.y)
}
// CountBitmap count of the bitmap
2018-07-29 10:53:30 +00:00
func CountBitmap(bitmap, sbit C.MMBitmapRef, args ...float32) int {
var tolerance C.float = 0.01
if len(args) > 0 {
tolerance = C.float(args[0])
}
count := C.count_of_bitmap(bitmap, sbit, tolerance)
return int(count)
}
2018-01-13 13:49:13 +00:00
// BitmapClick find the bitmap and click
func BitmapClick(bitmap C.MMBitmapRef, args ...interface{}) {
x, y := FindBitmap(bitmap)
2018-10-19 15:19:38 +00:00
MovesClick(x, y, args...)
2018-01-13 13:49:13 +00:00
}
2017-12-20 15:01:56 +00:00
// PointInBounds bitmap point in bounds
func PointInBounds(bitmap C.MMBitmapRef, x, y int) bool {
var point C.MMPoint
point.x = C.size_t(x)
point.y = C.size_t(y)
cbool := C.point_in_bounds(bitmap, point)
return bool(cbool)
}
2018-05-22 12:48:57 +00:00
// OpenBitmap open the bitmap return C.MMBitmapRef
2018-09-14 11:40:58 +00:00
//
// robotgo.OpenBitmap(path string, type int)
2018-05-30 15:52:09 +00:00
func OpenBitmap(gpath string, args ...int) C.MMBitmapRef {
2018-05-22 12:48:57 +00:00
path := C.CString(gpath)
var mtype C.uint16_t = 1
2018-05-22 12:48:57 +00:00
if len(args) > 0 {
2018-05-30 15:52:09 +00:00
mtype = C.uint16_t(args[0])
2018-05-22 12:48:57 +00:00
}
bit := C.bitmap_open(path, mtype)
C.free(unsafe.Pointer(path))
2016-10-19 17:10:10 +00:00
return bit
}
2018-03-06 09:40:01 +00:00
// DecodeImg decode the image to image.Image and return
func DecodeImg(path string) (image.Image, string, error) {
return imgo.DecodeFile(path)
}
// OpenImg open the image return []byte
func OpenImg(path string) []byte {
return imgo.ImgToBytes(path)
}
2020-01-31 16:29:15 +00:00
// SaveImg save the image by []byte
func SaveImg(b []byte, path string) {
imgo.Save(path, b)
}
2017-10-28 13:41:20 +00:00
// BitmapStr bitmap from string
func BitmapStr(str string) C.MMBitmapRef {
2019-03-30 17:23:38 +00:00
return BitmapFromStr(str)
}
// BitmapFromStr bitmap from string
func BitmapFromStr(str string) C.MMBitmapRef {
2017-10-28 13:41:20 +00:00
cs := C.CString(str)
bit := C.bitmap_from_string(cs)
C.free(unsafe.Pointer(cs))
2017-10-28 13:41:20 +00:00
return bit
}
2018-05-22 12:48:57 +00:00
// SaveBitmap save the bitmap to image
2018-09-10 17:54:35 +00:00
//
2018-05-22 12:48:57 +00:00
// robotgo.SaveBimap(bitmap C.MMBitmapRef, path string, type int)
2018-05-30 15:52:09 +00:00
func SaveBitmap(bitmap C.MMBitmapRef, gpath string, args ...int) string {
2018-05-22 12:48:57 +00:00
var mtype C.uint16_t = 1
2018-05-30 15:39:53 +00:00
if len(args) > 0 {
2018-05-30 15:52:09 +00:00
mtype = C.uint16_t(args[0])
2018-05-22 12:48:57 +00:00
}
2016-10-20 09:32:45 +00:00
2018-05-30 15:39:53 +00:00
path := C.CString(gpath)
saveBit := C.bitmap_save(bitmap, path, mtype)
C.free(unsafe.Pointer(path))
2016-12-22 17:01:48 +00:00
2018-05-22 12:48:57 +00:00
return C.GoString(saveBit)
2016-10-19 17:10:10 +00:00
}
2017-12-24 16:42:41 +00:00
// GetPortion get bitmap portion
2018-02-13 11:07:35 +00:00
func GetPortion(bit C.MMBitmapRef, x, y, w, h int) C.MMBitmapRef {
2016-10-23 15:39:09 +00:00
var rect C.MMRect
2018-02-13 11:07:35 +00:00
rect.origin.x = C.size_t(x)
rect.origin.y = C.size_t(y)
rect.size.width = C.size_t(w)
rect.size.height = C.size_t(h)
2016-10-23 15:39:09 +00:00
2017-12-29 11:52:42 +00:00
pos := C.get_portion(bit, rect)
2016-10-23 15:39:09 +00:00
return pos
}
2018-09-14 13:42:43 +00:00
// Convert convert the bitmap
//
// robotgo.Convert(opath, spath string, type int)
2018-05-30 15:52:09 +00:00
func Convert(opath, spath string, args ...int) {
var mtype = 1
if len(args) > 0 {
mtype = args[0]
}
// C.CString()
2016-12-10 15:14:56 +00:00
bitmap := OpenBitmap(opath)
2017-05-20 10:16:14 +00:00
// fmt.Println("a----", bit_map)
2016-12-10 15:14:56 +00:00
SaveBitmap(bitmap, spath, mtype)
2016-10-30 11:16:03 +00:00
}
2017-06-03 14:14:36 +00:00
// FreeBitmap free and dealloc bitmap
func FreeBitmap(bitmap C.MMBitmapRef) {
// C.destroyMMBitmap(bitmap)
C.bitmap_dealloc(bitmap)
2017-06-03 14:14:36 +00:00
}
2017-06-06 14:56:48 +00:00
// ReadBitmap returns false and sets error if |bitmap| is NULL
func ReadBitmap(bitmap C.MMBitmapRef) bool {
abool := C.bitmap_ready(bitmap)
gbool := bool(abool)
return gbool
}
2018-04-30 15:38:34 +00:00
// CopyBitPB copy bitmap to pasteboard
func CopyBitPB(bitmap C.MMBitmapRef) bool {
2017-06-06 14:56:48 +00:00
abool := C.bitmap_copy_to_pboard(bitmap)
gbool := bool(abool)
2018-04-30 15:38:34 +00:00
2017-06-06 14:56:48 +00:00
return gbool
}
2018-04-30 15:38:34 +00:00
// CopyBitpb copy bitmap to pasteboard, Wno-deprecated
func CopyBitpb(bitmap C.MMBitmapRef) bool {
2020-02-11 13:59:36 +00:00
tt.Drop("CopyBitpb", "CopyBitPB")
2018-04-30 15:38:34 +00:00
return CopyBitPB(bitmap)
}
2017-06-29 14:50:53 +00:00
// DeepCopyBit deep copy bitmap
func DeepCopyBit(bitmap C.MMBitmapRef) C.MMBitmapRef {
bit := C.bitmap_deepcopy(bitmap)
return bit
}
2017-09-14 14:47:21 +00:00
// GetColor get bitmap color
func GetColor(bitmap C.MMBitmapRef, x, y int) C.MMRGBHex {
color := C.bitmap_get_color(bitmap, C.size_t(x), C.size_t(y))
return color
}
2018-11-25 14:50:57 +00:00
// GetColors get bitmap color retrun string
func GetColors(bitmap C.MMBitmapRef, x, y int) string {
clo := GetColor(bitmap, x, y)
return PadHex(clo)
}
2017-09-14 15:02:43 +00:00
// FindColor find bitmap color
2018-09-14 13:42:43 +00:00
//
// robotgo.FindColor(color CHex, bitmap C.MMBitmapRef, tolerance float)
func FindColor(color CHex, args ...interface{}) (int, int) {
var (
tolerance C.float = 0.01
bitmap C.MMBitmapRef
)
2017-09-14 15:02:43 +00:00
2019-05-15 15:22:39 +00:00
if len(args) > 0 && args[0] != nil {
bitmap = args[0].(C.MMBitmapRef)
} else {
bitmap = CaptureScreen()
}
if len(args) > 1 {
tolerance = C.float(args[1].(float64))
2017-09-14 15:02:43 +00:00
}
pos := C.bitmap_find_color(bitmap, C.MMRGBHex(color), tolerance)
if len(args) <= 0 {
FreeBitmap(bitmap)
}
2017-09-14 15:02:43 +00:00
x := int(pos.x)
y := int(pos.y)
return x, y
}
// FindColorCS findcolor by CaptureScreen
func FindColorCS(color CHex, x, y, w, h int, args ...float64) (int, int) {
var tolerance = 0.01
if len(args) > 0 {
tolerance = args[0]
}
bitmap := CaptureScreen(x, y, w, h)
rx, ry := FindColor(color, bitmap, tolerance)
2018-07-28 11:24:49 +00:00
FreeBitmap(bitmap)
return rx, ry
}
2018-01-02 13:31:33 +00:00
// CountColor count bitmap color
func CountColor(color CHex, args ...interface{}) int {
var (
tolerance C.float = 0.01
bitmap C.MMBitmapRef
)
2018-01-02 13:31:33 +00:00
2019-05-15 15:22:39 +00:00
if len(args) > 0 && args[0] != nil {
bitmap = args[0].(C.MMBitmapRef)
} else {
bitmap = CaptureScreen()
}
if len(args) > 1 {
tolerance = C.float(args[1].(float64))
2018-01-02 13:31:33 +00:00
}
count := C.bitmap_count_of_color(bitmap, C.MMRGBHex(color), tolerance)
if len(args) <= 0 {
FreeBitmap(bitmap)
}
2018-01-02 13:31:33 +00:00
return int(count)
}
// CountColorCS count bitmap color by CaptureScreen
func CountColorCS(color CHex, x, y, w, h int, args ...float64) int {
var tolerance = 0.01
2018-01-02 13:31:33 +00:00
if len(args) > 0 {
tolerance = args[0]
}
bitmap := CaptureScreen(x, y, w, h)
rx := CountColor(color, bitmap, tolerance)
2018-07-28 11:24:49 +00:00
FreeBitmap(bitmap)
2018-01-02 13:31:33 +00:00
return rx
}
2018-02-13 10:56:57 +00:00
// GetImgSize get the image size
func GetImgSize(imgPath string) (int, int) {
bitmap := OpenBitmap(imgPath)
gbit := ToBitmap(bitmap)
2018-02-13 11:07:35 +00:00
w := gbit.Width / 2
h := gbit.Height / 2
2018-07-28 11:24:49 +00:00
FreeBitmap(bitmap)
2018-02-13 10:56:57 +00:00
2018-02-13 11:07:35 +00:00
return w, h
2018-02-13 10:56:57 +00:00
}
2016-10-27 08:02:03 +00:00
/*
____ __ ____ __ .__ __. _______ ______ ____ __ ____
\ \ / \ / / | | | \ | | | \ / __ \ \ \ / \ / /
\ \/ \/ / | | | \| | | .--. | | | | \ \/ \/ /
\ / | | | . ` | | | | | | | | \ /
\ /\ / | | | |\ | | '--' | `--' | \ /\ /
\__/ \__/ |__| |__| \__| |_______/ \______/ \__/ \__/
*/
2016-12-09 12:56:56 +00:00
2017-06-11 05:32:23 +00:00
// ShowAlert show a alert window
2016-11-07 10:34:21 +00:00
func ShowAlert(title, msg string, args ...string) int {
2016-11-05 08:16:26 +00:00
var (
// title string
// msg string
2018-07-19 16:06:34 +00:00
defaultButton = "Ok"
cancelButton = "Cancel"
2016-11-05 08:16:26 +00:00
)
2018-07-19 16:06:34 +00:00
if len(args) > 0 {
2016-11-05 08:16:26 +00:00
// title = args[0]
// msg = args[1]
defaultButton = args[0]
2018-07-19 16:06:34 +00:00
}
2018-07-19 16:06:34 +00:00
if len(args) > 1 {
2016-11-05 08:16:26 +00:00
cancelButton = args[1]
2018-07-19 16:06:34 +00:00
}
2016-11-05 08:16:26 +00:00
atitle := C.CString(title)
amsg := C.CString(msg)
adefaultButton := C.CString(defaultButton)
acancelButton := C.CString(cancelButton)
2016-11-07 14:43:08 +00:00
2017-12-29 11:52:42 +00:00
cbool := C.show_alert(atitle, amsg, adefaultButton, acancelButton)
2016-11-07 10:34:21 +00:00
ibool := int(cbool)
2018-12-26 16:55:17 +00:00
C.free(unsafe.Pointer(atitle))
C.free(unsafe.Pointer(amsg))
C.free(unsafe.Pointer(adefaultButton))
C.free(unsafe.Pointer(acancelButton))
2016-11-07 10:34:21 +00:00
return ibool
2016-11-05 08:16:26 +00:00
}
2016-11-10 10:59:37 +00:00
2017-06-11 05:32:23 +00:00
// IsValid valid the window
2016-11-10 10:59:37 +00:00
func IsValid() bool {
2017-12-29 11:52:42 +00:00
abool := C.is_valid()
2016-11-10 10:59:37 +00:00
gbool := bool(abool)
2017-05-20 10:16:14 +00:00
// fmt.Println("bool---------", gbool)
2016-11-10 10:59:37 +00:00
return gbool
}
2017-06-11 05:32:23 +00:00
// SetActive set the window active
2016-11-23 12:40:28 +00:00
func SetActive(win C.MData) {
2017-12-29 11:52:42 +00:00
C.set_active(win)
2016-11-23 12:40:28 +00:00
}
2017-06-11 05:32:23 +00:00
// GetActive get the active window
2016-11-10 10:59:37 +00:00
func GetActive() C.MData {
2017-12-29 11:52:42 +00:00
mdata := C.get_active()
2017-05-20 10:16:14 +00:00
// fmt.Println("active----", mdata)
2016-11-10 10:59:37 +00:00
return mdata
}
2018-07-29 15:56:28 +00:00
// MinWindow set the window min
2018-07-29 16:57:31 +00:00
func MinWindow(pid int32, args ...interface{}) {
var (
state = true
hwnd int
)
2018-07-29 15:56:28 +00:00
if len(args) > 0 {
2018-07-29 16:57:31 +00:00
state = args[0].(bool)
}
if len(args) > 1 {
hwnd = args[1].(int)
2018-07-29 15:56:28 +00:00
}
C.min_window(C.uintptr(pid), C.bool(state), C.uintptr(hwnd))
}
// MaxWindow set the window max
2018-07-29 16:57:31 +00:00
func MaxWindow(pid int32, args ...interface{}) {
var (
state = true
hwnd int
)
2018-07-29 15:56:28 +00:00
if len(args) > 0 {
2018-07-29 16:57:31 +00:00
state = args[0].(bool)
}
if len(args) > 1 {
hwnd = args[1].(int)
2018-07-29 15:56:28 +00:00
}
C.max_window(C.uintptr(pid), C.bool(state), C.uintptr(hwnd))
}
2017-06-11 05:32:23 +00:00
// CloseWindow close the window
2018-08-23 16:06:42 +00:00
func CloseWindow(args ...int32) {
if len(args) <= 0 {
C.close_main_window()
return
}
var hwnd, isHwnd int32
if len(args) > 0 {
hwnd = args[0]
}
if len(args) > 1 {
isHwnd = args[1]
}
C.close_window(C.uintptr(hwnd), C.uintptr(isHwnd))
2016-11-10 10:59:37 +00:00
}
2017-06-11 05:32:23 +00:00
// SetHandle set the window handle
2017-01-02 15:10:31 +00:00
func SetHandle(hwnd int) {
chwnd := C.uintptr(hwnd)
2017-12-29 11:52:42 +00:00
C.set_handle(chwnd)
2017-01-02 15:10:31 +00:00
}
// SetHandlePid set the window handle by pid
func SetHandlePid(pid int32, args ...int32) {
var isHwnd int32
if len(args) > 0 {
isHwnd = args[0]
}
C.set_handle_pid_mData(C.uintptr(pid), C.uintptr(isHwnd))
}
// GetHandPid get handle mdata by pid
func GetHandPid(pid int32, args ...int32) C.MData {
var isHwnd int32
if len(args) > 0 {
isHwnd = args[0]
}
return C.set_handle_pid(C.uintptr(pid), C.uintptr(isHwnd))
}
2017-06-11 05:32:23 +00:00
// GetHandle get the window handle
2016-11-10 10:59:37 +00:00
func GetHandle() int {
2017-12-29 11:52:42 +00:00
hwnd := C.get_handle()
2016-11-10 10:59:37 +00:00
ghwnd := int(hwnd)
2017-05-20 10:16:14 +00:00
// fmt.Println("gethwnd---", ghwnd)
2016-11-10 10:59:37 +00:00
return ghwnd
}
2018-04-11 13:14:57 +00:00
// GetBHandle get the window handle, Wno-deprecated
2017-02-06 11:10:10 +00:00
func GetBHandle() int {
2020-02-11 13:59:36 +00:00
tt.Drop("GetBHandle", "GetHandle")
2017-12-29 11:52:42 +00:00
hwnd := C.bget_handle()
2017-02-06 11:10:10 +00:00
ghwnd := int(hwnd)
2017-05-20 10:16:14 +00:00
//fmt.Println("gethwnd---", ghwnd)
2017-02-06 11:10:10 +00:00
return ghwnd
}
func cgetTitle(hwnd, isHwnd int32) string {
title := C.get_title(C.uintptr(hwnd), C.uintptr(isHwnd))
gtitle := C.GoString(title)
return gtitle
}
2017-06-11 05:32:23 +00:00
// GetTitle get the window title
2018-08-21 16:47:31 +00:00
func GetTitle(args ...int32) string {
if len(args) <= 0 {
title := C.get_main_title()
gtitle := C.GoString(title)
return gtitle
2018-08-21 16:47:31 +00:00
}
if len(args) > 1 {
return internalGetTitle(args[0], args[1])
2018-08-21 16:47:31 +00:00
}
return internalGetTitle(args[0])
2016-11-10 10:59:37 +00:00
}
2017-01-18 12:34:35 +00:00
2017-06-11 05:32:23 +00:00
// GetPID get the process id
2018-07-29 15:33:19 +00:00
func GetPID() int32 {
2017-12-29 11:52:42 +00:00
pid := C.get_PID()
2018-07-29 15:33:19 +00:00
return int32(pid)
2017-01-18 12:34:35 +00:00
}
2017-07-01 11:21:06 +00:00
// internalGetBounds get the window bounds
func internalGetBounds(pid int32, hwnd int) (int, int, int, int) {
bounds := C.get_bounds(C.uintptr(pid), C.uintptr(hwnd))
return int(bounds.X), int(bounds.Y), int(bounds.W), int(bounds.H)
}
2019-05-06 14:39:12 +00:00
// Is64Bit determine whether the sys is 64bit
func Is64Bit() bool {
b := C.Is64Bit()
return bool(b)
}
2018-07-10 12:02:18 +00:00
func internalActive(pid int32, hwnd int) {
C.active_PID(C.uintptr(pid), C.uintptr(hwnd))
2017-07-13 14:02:27 +00:00
}
2017-11-29 08:22:16 +00:00
2018-07-10 12:02:18 +00:00
// ActivePID active the window by PID,
// If args[0] > 0 on the Windows platform via a window handle to active
// func ActivePID(pid int32, args ...int) {
// var hwnd int
// if len(args) > 0 {
// hwnd = args[0]
// }
// C.active_PID(C.uintptr(pid), C.uintptr(hwnd))
// }
2018-01-04 12:01:45 +00:00
// ActiveName active window by name
func ActiveName(name string) error {
pids, err := FindIds(name)
if err == nil && len(pids) > 0 {
ActivePID(pids[0])
}
return err
}