Add clear scroll API and Update examples

This commit is contained in:
vcaesar 2022-02-04 00:02:34 -08:00
parent b439c59d85
commit b543a49322
15 changed files with 110 additions and 67 deletions

View File

@ -158,7 +158,11 @@ import (
) )
func main() { func main() {
// robotgo.ScrollMouse(10, "up") robotgo.MouseSleep = 100
robotgo.ScrollMouse(10, "up")
robotgo.ScrollMouse(20, "right")
robotgo.Scroll(0, -10) robotgo.Scroll(0, -10)
robotgo.Scroll(100, 0) robotgo.Scroll(100, 0)
@ -166,7 +170,6 @@ func main() {
robotgo.ScrollSmooth(-10, 6) robotgo.ScrollSmooth(-10, 6)
// robotgo.ScrollRelative(10, -100) // robotgo.ScrollRelative(10, -100)
robotgo.MouseSleep = 100
robotgo.Move(10, 20) robotgo.Move(10, 20)
robotgo.MoveRelative(0, -10) robotgo.MoveRelative(0, -10)
robotgo.Drag(10, 10) robotgo.Drag(10, 10)
@ -193,7 +196,7 @@ import (
func main() { func main() {
robotgo.TypeStr("Hello World") robotgo.TypeStr("Hello World")
robotgo.TypeStr("だんしゃり", 1.0) robotgo.TypeStr("だんしゃり", 1)
// robotgo.TypeStr("テストする") // robotgo.TypeStr("テストする")
robotgo.TypeStr("Hi galaxy. こんにちは世界.") robotgo.TypeStr("Hi galaxy. こんにちは世界.")

View File

@ -22,8 +22,8 @@ typedef MMBitmap *MMBitmapRef;
#define MMBitmapPointInBounds(image, p) ((p).x < (image)->width && (p).y < (image)->height) #define MMBitmapPointInBounds(image, p) ((p).x < (image)->width && (p).y < (image)->height)
/* Get pointer to pixel of MMBitmapRef. No bounds checking is performed */ /* Get pointer to pixel of MMBitmapRef. No bounds checking is performed */
#define MMRGBColorRefAtPoint(image, x, y) ( \ #define MMRGBColorRefAtPoint(image, x, y) \
MMRGBColor *)(assert(MMBitmapPointInBounds(image, MMPointInt32Make(x, y))), \ (MMRGBColor *)(assert(MMBitmapPointInBounds(image, MMPointInt32Make(x, y))), \
((image)->imageBuffer) + (((image)->bytewidth * (y)) + ((x) * (image)->bytesPerPixel))) ((image)->imageBuffer) + (((image)->bytewidth * (y)) + ((x) * (image)->bytesPerPixel)))
/* Dereference pixel of MMBitmapRef. Again, no bounds checking is performed. */ /* Dereference pixel of MMBitmapRef. Again, no bounds checking is performed. */

View File

@ -1,7 +1,6 @@
#pragma once #pragma once
/* A complicated, portable model for declaring inline functions in /* A complicated, portable model for declaring inline functions in header files. */
* header files. */
#if !defined(H_INLINE) #if !defined(H_INLINE)
#if defined(__GNUC__) #if defined(__GNUC__)
#define H_INLINE static __inline__ __attribute__((always_inline)) #define H_INLINE static __inline__ __attribute__((always_inline))

View File

@ -2,12 +2,6 @@
#ifndef OS_H #ifndef OS_H
#define 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__) #if !defined(IS_MACOSX) && defined(__APPLE__) && defined(__MACH__)
#define IS_MACOSX #define IS_MACOSX
#endif /* IS_MACOSX */ #endif /* IS_MACOSX */
@ -29,8 +23,7 @@
#error "Sorry, this platform isn't supported yet!" #error "Sorry, this platform isn't supported yet!"
#endif #endif
/* Interval to align by for large buffers (e.g. bitmaps). */ /* Interval to align by for large buffers (e.g. bitmaps). Must be a power of 2. */
/* Must be a power of 2. */
#ifndef BYTE_ALIGN #ifndef BYTE_ALIGN
#define BYTE_ALIGN 4 /* Bytes to align pixel buffers to. */ #define BYTE_ALIGN 4 /* Bytes to align pixel buffers to. */
/* #include <stddef.h> */ /* #include <stddef.h> */

View File

@ -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 CGPointFromMMPointInt32(p) CGPointMake((CGFloat)(p).x, (CGFloat)(p).y)
#define MMPointInt32FromCGPoint(p) MMPointInt32Make((int32_t)(p).x, (int32_t)(p).y) #define MMPointInt32FromCGPoint(p) MMPointInt32Make((int32_t)(p).x, (int32_t)(p).y)
#elif defined(IS_WINDOWS) #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) #define MMPointInt32FromPOINT(p) MMPointInt32Make((int32_t)p.x, (int32_t)p.y)
#endif #endif

View File

@ -19,20 +19,21 @@ import (
func typeStr() { func typeStr() {
// importing "Hello World" // importing "Hello World"
robotgo.TypeStr("Hello World!", 1.0) robotgo.TypeStr("Hello World!", 1)
robotgo.KeySleep = 100 robotgo.KeySleep = 100
robotgo.TypeStr("だんしゃり") robotgo.TypeStr("だんしゃり")
robotgo.TypeStr("Hi galaxy. こんにちは世界. 你好, 再见!") robotgo.TypeStr("Hi galaxy. こんにちは世界. 你好, 再见!")
robotgo.Sleep(1) robotgo.Sleep(1)
robotgo.TypeStr("So, hi, bye!") robotgo.TypeStr("So, hi, bye!")
robotgo.MilliSleep(100) robotgo.MilliSleep(100)
ustr := uint32(robotgo.CharCodeAt("So, hi, bye!", 0)) ustr := uint32(robotgo.CharCodeAt("So, hi, bye!", 0))
robotgo.UnicodeType(ustr) robotgo.UnicodeType(ustr)
robotgo.PasteStr("paste string") err := robotgo.PasteStr("paste string")
fmt.Println("PasteStr: ", err)
} }
func keyTap() { func keyTap() {

View File

@ -68,7 +68,9 @@ func get() {
func toggleAndScroll() { func toggleAndScroll() {
// scrolls the mouse either up // scrolls the mouse either up
// robotgo.ScrollMouse(10, "up") robotgo.ScrollMouse(10, "up")
robotgo.ScrollMouse(10, "right")
robotgo.Scroll(100, 10) robotgo.Scroll(100, 10)
robotgo.Scroll(0, -10) robotgo.Scroll(0, -10)

View File

@ -49,26 +49,32 @@ func get() {
func findIds() { func findIds() {
// find the process id by the process name // find the process id by the process name
fpid, err := robotgo.FindIds("Google") fpid, err := robotgo.FindIds("Google")
if err == nil { if err != nil {
fmt.Println("pids...", fpid) fmt.Println(err)
if len(fpid) > 0 { return
robotgo.ActivePID(fpid[0]) }
tl := robotgo.GetTitle(fpid[0]) fmt.Println("pids...", fpid)
fmt.Println("pid[0] title is: ", tl) if len(fpid) > 0 {
err = robotgo.ActivePID(fpid[0])
x, y, w, h := robotgo.GetBounds(fpid[0]) if err != nil {
fmt.Println("GetBounds is: ", x, y, w, h) fmt.Println(err)
// Windows
// hwnd := robotgo.FindWindow("google")
// hwnd := robotgo.GetHWND()
robotgo.MinWindow(fpid[0])
robotgo.MaxWindow(fpid[0])
robotgo.CloseWindow(fpid[0])
robotgo.Kill(fpid[0])
} }
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])
} }
} }

14
key.go
View File

@ -575,13 +575,13 @@ func inputUTF(str string) {
// TypeStr send a string, support UTF-8 // 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: // Examples:
// robotgo.TypeStr("abc@123, hi, こんにちは") // robotgo.TypeStr("abc@123, hi, こんにちは")
// //
func TypeStr(str string, args ...float64) { func TypeStr(str string, args ...int) {
var tm, tm1 = 0.0, 7.0 var tm, tm1 = 0, 7
if len(args) > 0 { if len(args) > 0 {
tm = args[0] tm = args[0]
@ -599,10 +599,10 @@ func TypeStr(str string, args ...float64) {
UnicodeType(ustr) UnicodeType(ustr)
} else { } else {
inputUTF(strUc[i]) inputUTF(strUc[i])
MicroSleep(tm1) MilliSleep(tm1)
} }
MicroSleep(tm) MilliSleep(tm)
} }
return return
} }
@ -611,7 +611,7 @@ func TypeStr(str string, args ...float64) {
ustr := uint32(CharCodeAt(str, i)) ustr := uint32(CharCodeAt(str, i))
UnicodeType(ustr) UnicodeType(ustr)
// if len(args) > 0 { // if len(args) > 0 {
MicroSleep(tm) MilliSleep(tm)
// } // }
} }
MilliSleep(KeySleep) MilliSleep(KeySleep)
@ -635,7 +635,7 @@ func PasteStr(str string) error {
// TypeStrDelay type string delayed // TypeStrDelay type string delayed
func TypeStrDelay(str string, delay int) { func TypeStrDelay(str string, delay int) {
TypeStr(str) TypeStr(str)
Sleep(delay) MilliSleep(delay)
} }
// Deprecated: use the TypeStr(), // Deprecated: use the TypeStr(),

View File

@ -4,13 +4,16 @@
#include <CoreFoundation/CoreFoundation.h> #include <CoreFoundation/CoreFoundation.h>
#include <Carbon/Carbon.h> /* For kVK_ constants, and TIS functions. */ #include <Carbon/Carbon.h> /* 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); CFStringRef createStringForKey(CGKeyCode keyCode);
#endif #endif
MMKeyCode keyCodeForChar(const char c) { MMKeyCode keyCodeForChar(const char c) {
#if defined(IS_MACOSX) #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; static CFMutableDictionaryRef charToCodeDict = NULL;
CGKeyCode code; CGKeyCode code;
UniChar character = c; UniChar character = c;

View File

@ -169,11 +169,11 @@ void toggleKeyCode(MMKeyCode code, const bool down, MMKeyFlags flags) {
#endif #endif
} }
void tapKeyCode(MMKeyCode code, MMKeyFlags flags){ // void tapKeyCode(MMKeyCode code, MMKeyFlags flags){
toggleKeyCode(code, true, flags); // toggleKeyCode(code, true, flags);
microsleep(5.0); // microsleep(5.0);
toggleKeyCode(code, false, flags); // toggleKeyCode(code, false, flags);
} // }
#if defined(USE_X11) #if defined(USE_X11)
bool toUpper(char c) { bool toUpper(char c) {
@ -221,11 +221,11 @@ void toggleKey(char c, const bool down, MMKeyFlags flags){
toggleKeyCode(keyCode, down, flags); toggleKeyCode(keyCode, down, flags);
} }
void tapKey(char c, MMKeyFlags flags){ // void tapKey(char c, MMKeyFlags flags){
toggleKey(c, true, flags); // toggleKey(c, true, flags);
microsleep(5.0); // microsleep(5.0);
toggleKey(c, false, flags); // toggleKey(c, false, flags);
} // }
#if defined(IS_MACOSX) #if defined(IS_MACOSX)
void toggleUnicode(UniChar ch, const bool down) { 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) #define toggleUniKey(c, down) toggleKey(c, down, MOD_NONE)
int input_utf(const char *utf) { int input_utf(const char *utf) {
Display *dpy; Display *dpy = XOpenDisplay(NULL);
dpy = XOpenDisplay(NULL);
KeySym sym = XStringToKeysym(utf); KeySym sym = XStringToKeysym(utf);
// KeySym sym = XKeycodeToKeysym(dpy, utf); // KeySym sym = XKeycodeToKeysym(dpy, utf);

View File

@ -128,7 +128,9 @@ func Sleep(tm int) {
time.Sleep(time.Duration(tm) * time.Second) 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) { func MicroSleep(tm float64) {
C.microsleep(C.double(tm)) C.microsleep(C.double(tm))
} }
@ -740,6 +742,34 @@ func Scroll(x, y int, args ...int) {
MilliSleep(MouseSleep + msDelay) 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, // ScrollSmooth scroll the mouse smooth,
// default scroll 5 times and sleep 100 millisecond // default scroll 5 times and sleep 100 millisecond
// //

View File

@ -68,7 +68,9 @@ func TestDragMouse(t *testing.T) {
} }
func TestScrollMouse(t *testing.T) { func TestScrollMouse(t *testing.T) {
// ScrollMouse(120, "up") ScrollMouse(120, "up")
ScrollMouse(100, "right")
Scroll(0, 120) Scroll(0, 120)
MilliSleep(100) MilliSleep(100)
@ -106,6 +108,12 @@ func TestMouseToggle(t *testing.T) {
e = Toggle("right", "up") e = Toggle("right", "up")
tt.Nil(t, e) tt.Nil(t, e)
e = MouseDown("left")
tt.Nil(t, e)
e = MouseUp("left")
tt.Nil(t, e)
} }
func TestKey(t *testing.T) { func TestKey(t *testing.T) {
@ -177,6 +185,10 @@ func TestImage(t *testing.T) {
tt.Equal(t, 20, Width(img1)) tt.Equal(t, 20, Width(img1))
tt.Equal(t, 20, Height(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) { func TestPs(t *testing.T) {

View File

@ -32,8 +32,7 @@ int showAlert(const char *title, const char *msg,
#elif defined(USE_X11) #elif defined(USE_X11)
return 0; return 0;
#else #else
/* TODO: Display custom buttons instead of the pre-defined "OK" /* TODO: Display custom buttons instead of the pre-defined "OK" and "Cancel". */
* and "Cancel". */
int response = MessageBox(NULL, msg, title, int response = MessageBox(NULL, msg, title,
(cancelButton == NULL) ? MB_OK : MB_OKCANCEL ); (cancelButton == NULL) ? MB_OK : MB_OKCANCEL );
return (response == IDOK) ? 0 : 1; return (response == IDOK) ? 0 : 1;

View File

@ -85,7 +85,7 @@ bool is_valid() {
#if defined(IS_MACOSX) #if defined(IS_MACOSX)
mData.CgID = actdata.CgID; mData.CgID = actdata.CgID;
mData.AxID = actdata.AxID; 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; CFTypeRef r = NULL;
// Attempt to get the window role // Attempt to get the window role
@ -153,7 +153,6 @@ bool IsAxEnabled(bool options){
// Determine whether the process is actually trusted // Determine whether the process is actually trusted
bool result = (*gAXIsProcessTrustedWithOptions)(o); bool result = (*gAXIsProcessTrustedWithOptions)(o);
// Free memory // Free memory
CFRelease(o); CFRelease(o);
return result; return result;
@ -336,7 +335,6 @@ void set_active(const MData win) {
} else { } else {
// Attempt to raise the specified window // Attempt to raise the specified window
XRaiseWindow(rDisplay, win.XWin); XRaiseWindow(rDisplay, win.XWin);
// Set the specified window's input focus // Set the specified window's input focus
XSetInputFocus(rDisplay, win.XWin, RevertToParent, CurrentTime); XSetInputFocus(rDisplay, win.XWin, RevertToParent, CurrentTime);
} }