From b543a49322e8fda51641418e79633a11b55bc04f Mon Sep 17 00:00:00 2001 From: vcaesar Date: Fri, 4 Feb 2022 00:02:34 -0800 Subject: [PATCH] Add clear scroll API and Update examples --- README.md | 9 ++++++--- base/MMBitmap.h | 4 ++-- base/inline_keywords.h | 3 +-- base/os.h | 9 +-------- base/types.h | 1 - examples/key/main.go | 7 ++++--- examples/mouse/main.go | 4 +++- examples/window/main.go | 42 +++++++++++++++++++++++------------------ key.go | 14 +++++++------- key/keycode_c.h | 7 +++++-- key/keypress_c.h | 24 +++++++++++------------ robotgo.go | 32 ++++++++++++++++++++++++++++++- robotgo_test.go | 14 +++++++++++++- window/alert_c.h | 3 +-- window/window.h | 4 +--- 15 files changed, 110 insertions(+), 67 deletions(-) diff --git a/README.md b/README.md index 7e03083..da5c027 100644 --- a/README.md +++ b/README.md @@ -158,7 +158,11 @@ import ( ) func main() { - // robotgo.ScrollMouse(10, "up") + robotgo.MouseSleep = 100 + + robotgo.ScrollMouse(10, "up") + robotgo.ScrollMouse(20, "right") + robotgo.Scroll(0, -10) robotgo.Scroll(100, 0) @@ -166,7 +170,6 @@ func main() { robotgo.ScrollSmooth(-10, 6) // robotgo.ScrollRelative(10, -100) - robotgo.MouseSleep = 100 robotgo.Move(10, 20) robotgo.MoveRelative(0, -10) robotgo.Drag(10, 10) @@ -193,7 +196,7 @@ import ( func main() { robotgo.TypeStr("Hello World") - robotgo.TypeStr("だんしゃり", 1.0) + robotgo.TypeStr("だんしゃり", 1) // robotgo.TypeStr("テストする") robotgo.TypeStr("Hi galaxy. こんにちは世界.") diff --git a/base/MMBitmap.h b/base/MMBitmap.h index fbbdfc1..68fccd4 100644 --- a/base/MMBitmap.h +++ b/base/MMBitmap.h @@ -22,8 +22,8 @@ typedef MMBitmap *MMBitmapRef; #define MMBitmapPointInBounds(image, p) ((p).x < (image)->width && (p).y < (image)->height) /* Get pointer to pixel of MMBitmapRef. No bounds checking is performed */ -#define MMRGBColorRefAtPoint(image, x, y) ( \ - MMRGBColor *)(assert(MMBitmapPointInBounds(image, MMPointInt32Make(x, y))), \ +#define MMRGBColorRefAtPoint(image, x, y) \ + (MMRGBColor *)(assert(MMBitmapPointInBounds(image, MMPointInt32Make(x, y))), \ ((image)->imageBuffer) + (((image)->bytewidth * (y)) + ((x) * (image)->bytesPerPixel))) /* Dereference pixel of MMBitmapRef. Again, no bounds checking is performed. */ diff --git a/base/inline_keywords.h b/base/inline_keywords.h index a86b426..db0cc6d 100644 --- a/base/inline_keywords.h +++ b/base/inline_keywords.h @@ -1,7 +1,6 @@ #pragma once -/* A complicated, portable model for declaring inline functions in - * header files. */ +/* A complicated, portable model for declaring inline functions in header files. */ #if !defined(H_INLINE) #if defined(__GNUC__) #define H_INLINE static __inline__ __attribute__((always_inline)) diff --git a/base/os.h b/base/os.h index 6f6e74d..30a063d 100644 --- a/base/os.h +++ b/base/os.h @@ -2,12 +2,6 @@ #ifndef OS_H #define OS_H -/* Python versions under 2.5 don't support this macro, but it's not - * terribly difficult to replicate: */ -#ifndef PyModule_AddIntMacro - #define PyModule_AddIntMacro(module, macro) PyModule_AddIntConstant(module, #macro, macro) -#endif /* PyModule_AddIntMacro */ - #if !defined(IS_MACOSX) && defined(__APPLE__) && defined(__MACH__) #define IS_MACOSX #endif /* IS_MACOSX */ @@ -29,8 +23,7 @@ #error "Sorry, this platform isn't supported yet!" #endif -/* Interval to align by for large buffers (e.g. bitmaps). */ -/* Must be a power of 2. */ +/* Interval to align by for large buffers (e.g. bitmaps). Must be a power of 2. */ #ifndef BYTE_ALIGN #define BYTE_ALIGN 4 /* Bytes to align pixel buffers to. */ /* #include */ diff --git a/base/types.h b/base/types.h index 01080d2..fc2d0a6 100644 --- a/base/types.h +++ b/base/types.h @@ -62,7 +62,6 @@ H_INLINE MMRectInt32 MMRectInt32Make(int32_t x, int32_t y, int32_t w, int32_t h) #define CGPointFromMMPointInt32(p) CGPointMake((CGFloat)(p).x, (CGFloat)(p).y) #define MMPointInt32FromCGPoint(p) MMPointInt32Make((int32_t)(p).x, (int32_t)(p).y) #elif defined(IS_WINDOWS) - #define MMPointFromPOINT(p) MMPointMake((size_t)p.x, (size_t)p.y) #define MMPointInt32FromPOINT(p) MMPointInt32Make((int32_t)p.x, (int32_t)p.y) #endif diff --git a/examples/key/main.go b/examples/key/main.go index 142ae34..b8f141b 100644 --- a/examples/key/main.go +++ b/examples/key/main.go @@ -19,20 +19,21 @@ import ( func typeStr() { // importing "Hello World" - robotgo.TypeStr("Hello World!", 1.0) + robotgo.TypeStr("Hello World!", 1) robotgo.KeySleep = 100 robotgo.TypeStr("だんしゃり") robotgo.TypeStr("Hi galaxy. こんにちは世界. 你好, 再见!") - robotgo.Sleep(1) + robotgo.TypeStr("So, hi, bye!") robotgo.MilliSleep(100) ustr := uint32(robotgo.CharCodeAt("So, hi, bye!", 0)) robotgo.UnicodeType(ustr) - robotgo.PasteStr("paste string") + err := robotgo.PasteStr("paste string") + fmt.Println("PasteStr: ", err) } func keyTap() { diff --git a/examples/mouse/main.go b/examples/mouse/main.go index f797727..3f69a9d 100644 --- a/examples/mouse/main.go +++ b/examples/mouse/main.go @@ -68,7 +68,9 @@ func get() { func toggleAndScroll() { // scrolls the mouse either up - // robotgo.ScrollMouse(10, "up") + robotgo.ScrollMouse(10, "up") + robotgo.ScrollMouse(10, "right") + robotgo.Scroll(100, 10) robotgo.Scroll(0, -10) diff --git a/examples/window/main.go b/examples/window/main.go index 3b53fb8..e2d3e8c 100644 --- a/examples/window/main.go +++ b/examples/window/main.go @@ -49,26 +49,32 @@ func get() { func findIds() { // find the process id by the process name fpid, err := robotgo.FindIds("Google") - if err == nil { - fmt.Println("pids...", fpid) - if len(fpid) > 0 { - robotgo.ActivePID(fpid[0]) + if err != nil { + fmt.Println(err) + return + } - tl := robotgo.GetTitle(fpid[0]) - fmt.Println("pid[0] title is: ", tl) - - x, y, w, h := robotgo.GetBounds(fpid[0]) - fmt.Println("GetBounds is: ", x, y, w, h) - - // Windows - // hwnd := robotgo.FindWindow("google") - // hwnd := robotgo.GetHWND() - robotgo.MinWindow(fpid[0]) - robotgo.MaxWindow(fpid[0]) - robotgo.CloseWindow(fpid[0]) - - robotgo.Kill(fpid[0]) + fmt.Println("pids...", fpid) + if len(fpid) > 0 { + err = robotgo.ActivePID(fpid[0]) + if err != nil { + fmt.Println(err) } + + tl := robotgo.GetTitle(fpid[0]) + fmt.Println("pid[0] title is: ", tl) + + x, y, w, h := robotgo.GetBounds(fpid[0]) + fmt.Println("GetBounds is: ", x, y, w, h) + + // Windows + // hwnd := robotgo.FindWindow("google") + // hwnd := robotgo.GetHWND() + robotgo.MinWindow(fpid[0]) + robotgo.MaxWindow(fpid[0]) + robotgo.CloseWindow(fpid[0]) + + robotgo.Kill(fpid[0]) } } diff --git a/key.go b/key.go index de09a15..6e60151 100644 --- a/key.go +++ b/key.go @@ -575,13 +575,13 @@ func inputUTF(str string) { // TypeStr send a string, support UTF-8 // -// robotgo.TypeStr(string: The string to send, float64: microsleep time, x11 option) +// robotgo.TypeStr(string: The string to send, int: milli_sleep time, x11 option) // // Examples: // robotgo.TypeStr("abc@123, hi, こんにちは") // -func TypeStr(str string, args ...float64) { - var tm, tm1 = 0.0, 7.0 +func TypeStr(str string, args ...int) { + var tm, tm1 = 0, 7 if len(args) > 0 { tm = args[0] @@ -599,10 +599,10 @@ func TypeStr(str string, args ...float64) { UnicodeType(ustr) } else { inputUTF(strUc[i]) - MicroSleep(tm1) + MilliSleep(tm1) } - MicroSleep(tm) + MilliSleep(tm) } return } @@ -611,7 +611,7 @@ func TypeStr(str string, args ...float64) { ustr := uint32(CharCodeAt(str, i)) UnicodeType(ustr) // if len(args) > 0 { - MicroSleep(tm) + MilliSleep(tm) // } } MilliSleep(KeySleep) @@ -635,7 +635,7 @@ func PasteStr(str string) error { // TypeStrDelay type string delayed func TypeStrDelay(str string, delay int) { TypeStr(str) - Sleep(delay) + MilliSleep(delay) } // Deprecated: use the TypeStr(), diff --git a/key/keycode_c.h b/key/keycode_c.h index 4af3175..75b610e 100644 --- a/key/keycode_c.h +++ b/key/keycode_c.h @@ -4,13 +4,16 @@ #include #include /* For kVK_ constants, and TIS functions. */ - /* Returns string representation of key, if it is printable. */ + /* Returns string representation of key, if it is printable. + Ownership follows the Create Rule; + it is the caller's responsibility to release the returned object. */ CFStringRef createStringForKey(CGKeyCode keyCode); #endif MMKeyCode keyCodeForChar(const char c) { #if defined(IS_MACOSX) - /* OS X does not appear to have a built-in function for this, so instead it. */ + /* OS X does not appear to have a built-in function for this, + so instead it to write our own. */ static CFMutableDictionaryRef charToCodeDict = NULL; CGKeyCode code; UniChar character = c; diff --git a/key/keypress_c.h b/key/keypress_c.h index 1f5511d..300663d 100644 --- a/key/keypress_c.h +++ b/key/keypress_c.h @@ -169,11 +169,11 @@ void toggleKeyCode(MMKeyCode code, const bool down, MMKeyFlags flags) { #endif } -void tapKeyCode(MMKeyCode code, MMKeyFlags flags){ - toggleKeyCode(code, true, flags); - microsleep(5.0); - toggleKeyCode(code, false, flags); -} +// void tapKeyCode(MMKeyCode code, MMKeyFlags flags){ +// toggleKeyCode(code, true, flags); +// microsleep(5.0); +// toggleKeyCode(code, false, flags); +// } #if defined(USE_X11) bool toUpper(char c) { @@ -221,11 +221,11 @@ void toggleKey(char c, const bool down, MMKeyFlags flags){ toggleKeyCode(keyCode, down, flags); } -void tapKey(char c, MMKeyFlags flags){ - toggleKey(c, true, flags); - microsleep(5.0); - toggleKey(c, false, flags); -} +// void tapKey(char c, MMKeyFlags flags){ +// toggleKey(c, true, flags); +// microsleep(5.0); +// toggleKey(c, false, flags); +// } #if defined(IS_MACOSX) void toggleUnicode(UniChar ch, const bool down) { @@ -250,9 +250,7 @@ void toggleUnicode(UniChar ch, const bool down) { #define toggleUniKey(c, down) toggleKey(c, down, MOD_NONE) int input_utf(const char *utf) { - Display *dpy; - dpy = XOpenDisplay(NULL); - + Display *dpy = XOpenDisplay(NULL); KeySym sym = XStringToKeysym(utf); // KeySym sym = XKeycodeToKeysym(dpy, utf); diff --git a/robotgo.go b/robotgo.go index 913d2dc..b4b8f6f 100644 --- a/robotgo.go +++ b/robotgo.go @@ -128,7 +128,9 @@ func Sleep(tm int) { time.Sleep(time.Duration(tm) * time.Second) } -// MicroSleep time C.microsleep(tm), use the MilliSleep() +// Deprecated: use the MilliSleep(), +// +// MicroSleep time C.microsleep(tm) func MicroSleep(tm float64) { C.microsleep(C.double(tm)) } @@ -740,6 +742,34 @@ func Scroll(x, y int, args ...int) { MilliSleep(MouseSleep + msDelay) } +// ScrollMouse scroll the mouse to (x, "up") +// supported: "up", "down", "left", "right" +// +// Examples: +// robotgo.ScrollMouse(10, "down") +// robotgo.ScrollMouse(10, "up") +func ScrollMouse(x int, direction ...string) { + d := "down" + if len(direction) > 0 { + d = direction[0] + } + + 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 // diff --git a/robotgo_test.go b/robotgo_test.go index 8e9e1e2..bf3d54b 100644 --- a/robotgo_test.go +++ b/robotgo_test.go @@ -68,7 +68,9 @@ func TestDragMouse(t *testing.T) { } func TestScrollMouse(t *testing.T) { - // ScrollMouse(120, "up") + ScrollMouse(120, "up") + ScrollMouse(100, "right") + Scroll(0, 120) MilliSleep(100) @@ -106,6 +108,12 @@ func TestMouseToggle(t *testing.T) { e = Toggle("right", "up") tt.Nil(t, e) + + e = MouseDown("left") + tt.Nil(t, e) + + e = MouseUp("left") + tt.Nil(t, e) } func TestKey(t *testing.T) { @@ -177,6 +185,10 @@ func TestImage(t *testing.T) { tt.Equal(t, 20, Width(img1)) tt.Equal(t, 20, Height(img1)) + + bit1 := ImgToBitmap(img1) + tt.Equal(t, bit1.Width, Width(img1)) + tt.Equal(t, bit1.Height, Height(img1)) } func TestPs(t *testing.T) { diff --git a/window/alert_c.h b/window/alert_c.h index 5d67733..d5ed2ba 100644 --- a/window/alert_c.h +++ b/window/alert_c.h @@ -32,8 +32,7 @@ int showAlert(const char *title, const char *msg, #elif defined(USE_X11) return 0; #else - /* TODO: Display custom buttons instead of the pre-defined "OK" - * and "Cancel". */ + /* TODO: Display custom buttons instead of the pre-defined "OK" and "Cancel". */ int response = MessageBox(NULL, msg, title, (cancelButton == NULL) ? MB_OK : MB_OKCANCEL ); return (response == IDOK) ? 0 : 1; diff --git a/window/window.h b/window/window.h index 77d7a49..f25c60c 100644 --- a/window/window.h +++ b/window/window.h @@ -85,7 +85,7 @@ bool is_valid() { #if defined(IS_MACOSX) mData.CgID = actdata.CgID; mData.AxID = actdata.AxID; - if (mData.CgID == 0 || mData.AxID == 0){ return false; } + if (mData.CgID == 0 || mData.AxID == 0) { return false; } CFTypeRef r = NULL; // Attempt to get the window role @@ -153,7 +153,6 @@ bool IsAxEnabled(bool options){ // Determine whether the process is actually trusted bool result = (*gAXIsProcessTrustedWithOptions)(o); - // Free memory CFRelease(o); return result; @@ -336,7 +335,6 @@ void set_active(const MData win) { } else { // Attempt to raise the specified window XRaiseWindow(rDisplay, win.XWin); - // Set the specified window's input focus XSetInputFocus(rDisplay, win.XWin, RevertToParent, CurrentTime); }