mirror of
https://github.com/go-vgo/robotgo.git
synced 2025-05-29 21:43:55 +00:00
Add clear scroll API and Update examples
This commit is contained in:
parent
b439c59d85
commit
b543a49322
@ -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. こんにちは世界.")
|
||||
|
@ -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. */
|
||||
|
@ -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))
|
||||
|
@ -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 <stddef.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
|
||||
|
||||
|
@ -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() {
|
||||
|
@ -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)
|
||||
|
||||
|
@ -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])
|
||||
}
|
||||
}
|
||||
|
||||
|
14
key.go
14
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(),
|
||||
|
@ -4,13 +4,16 @@
|
||||
#include <CoreFoundation/CoreFoundation.h>
|
||||
#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);
|
||||
#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;
|
||||
|
@ -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);
|
||||
|
||||
|
32
robotgo.go
32
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
|
||||
//
|
||||
|
@ -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) {
|
||||
|
@ -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;
|
||||
|
@ -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);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user