mirror of
https://github.com/go-vgo/robotgo.git
synced 2025-06-17 13:33:55 +00:00
Compare commits
8 Commits
e9b2f9dcbe
...
4e9da867c2
Author | SHA1 | Date | |
---|---|---|---|
![]() |
4e9da867c2 | ||
![]() |
c431c8f974 | ||
![]() |
a183783e9c | ||
![]() |
c1115cebc7 | ||
![]() |
9aa4059a31 | ||
![]() |
31ddf1a959 | ||
![]() |
de95edb1a8 | ||
![]() |
df394a94b8 |
75
key.go
75
key.go
@ -416,7 +416,7 @@ func keyTaps(k string, keyArr []string, pid int) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func keyToggles(k string, keyArr []string, pid int) error {
|
func getKeyDown(keyArr []string) (bool, []string) {
|
||||||
if len(keyArr) <= 0 {
|
if len(keyArr) <= 0 {
|
||||||
keyArr = append(keyArr, "down")
|
keyArr = append(keyArr, "down")
|
||||||
}
|
}
|
||||||
@ -429,8 +429,11 @@ func keyToggles(k string, keyArr []string, pid int) error {
|
|||||||
if keyArr[0] == "up" || keyArr[0] == "down" {
|
if keyArr[0] == "up" || keyArr[0] == "down" {
|
||||||
keyArr = keyArr[1:]
|
keyArr = keyArr[1:]
|
||||||
}
|
}
|
||||||
flags := getFlagsFromValue(keyArr)
|
return down, keyArr
|
||||||
|
}
|
||||||
|
|
||||||
|
func keyTogglesB(k string, down bool, keyArr []string, pid int) error {
|
||||||
|
flags := getFlagsFromValue(keyArr)
|
||||||
key, err := checkKeyCodes(k)
|
key, err := checkKeyCodes(k)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@ -441,6 +444,11 @@ func keyToggles(k string, keyArr []string, pid int) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func keyToggles(k string, keyArr []string, pid int) error {
|
||||||
|
down, keyArr1 := getKeyDown(keyArr)
|
||||||
|
return keyTogglesB(k, down, keyArr1, pid)
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
__ ___ ___________ ____ .______ ______ ___ .______ _______
|
__ ___ ___________ ____ .______ ______ ___ .______ _______
|
||||||
| |/ / | ____\ \ / / | _ \ / __ \ / \ | _ \ | \
|
| |/ / | ____\ \ / / | _ \ / __ \ / \ | _ \ | \
|
||||||
@ -478,6 +486,22 @@ func toErr(str *C.char) error {
|
|||||||
return errors.New(gstr)
|
return errors.New(gstr)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func appendShift(key string, len1 int, args ...interface{}) []interface{} {
|
||||||
|
if len(key) > 0 && unicode.IsUpper([]rune(key)[0]) {
|
||||||
|
args = append(args, "shift")
|
||||||
|
}
|
||||||
|
|
||||||
|
key = strings.ToLower(key)
|
||||||
|
if _, ok := Special[key]; ok {
|
||||||
|
key = Special[key]
|
||||||
|
if len(args) <= len1 {
|
||||||
|
args = append(args, "shift")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return args
|
||||||
|
}
|
||||||
|
|
||||||
// KeyTap taps the keyboard code;
|
// KeyTap taps the keyboard code;
|
||||||
//
|
//
|
||||||
// See keys supported:
|
// See keys supported:
|
||||||
@ -496,18 +520,7 @@ func toErr(str *C.char) error {
|
|||||||
// robotgo.KeyTap("k", pid int)
|
// robotgo.KeyTap("k", pid int)
|
||||||
func KeyTap(key string, args ...interface{}) error {
|
func KeyTap(key string, args ...interface{}) error {
|
||||||
var keyArr []string
|
var keyArr []string
|
||||||
|
args = appendShift(key, 0, args...)
|
||||||
if len(key) > 0 && unicode.IsUpper([]rune(key)[0]) {
|
|
||||||
args = append(args, "shift")
|
|
||||||
}
|
|
||||||
|
|
||||||
key = strings.ToLower(key)
|
|
||||||
if _, ok := Special[key]; ok {
|
|
||||||
key = Special[key]
|
|
||||||
if len(args) <= 0 {
|
|
||||||
args = append(args, "shift")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pid := 0
|
pid := 0
|
||||||
if len(args) > 0 {
|
if len(args) > 0 {
|
||||||
@ -526,6 +539,16 @@ func KeyTap(key string, args ...interface{}) error {
|
|||||||
return keyTaps(key, keyArr, pid)
|
return keyTaps(key, keyArr, pid)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getToggleArgs(args ...interface{}) (pid int, keyArr []string) {
|
||||||
|
if len(args) > 0 && reflect.TypeOf(args[0]) == reflect.TypeOf(pid) {
|
||||||
|
pid = args[0].(int)
|
||||||
|
keyArr = ToStrings(args[1:])
|
||||||
|
} else {
|
||||||
|
keyArr = ToStrings(args)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// KeyToggle toggles the keyboard, if there not have args default is "down"
|
// KeyToggle toggles the keyboard, if there not have args default is "down"
|
||||||
//
|
//
|
||||||
// See keys:
|
// See keys:
|
||||||
@ -540,28 +563,8 @@ func KeyTap(key string, args ...interface{}) error {
|
|||||||
// robotgo.KeyToggle("a", "up", "alt", "cmd")
|
// robotgo.KeyToggle("a", "up", "alt", "cmd")
|
||||||
// robotgo.KeyToggle("k", pid int)
|
// robotgo.KeyToggle("k", pid int)
|
||||||
func KeyToggle(key string, args ...interface{}) error {
|
func KeyToggle(key string, args ...interface{}) error {
|
||||||
|
args = appendShift(key, 1, args...)
|
||||||
if len(key) > 0 && unicode.IsUpper([]rune(key)[0]) {
|
pid, keyArr := getToggleArgs(args...)
|
||||||
args = append(args, "shift")
|
|
||||||
}
|
|
||||||
|
|
||||||
key = strings.ToLower(key)
|
|
||||||
if _, ok := Special[key]; ok {
|
|
||||||
key = Special[key]
|
|
||||||
if len(args) <= 1 {
|
|
||||||
args = append(args, "shift")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pid := 0
|
|
||||||
var keyArr []string
|
|
||||||
if len(args) > 0 && reflect.TypeOf(args[0]) == reflect.TypeOf(pid) {
|
|
||||||
pid = args[0].(int)
|
|
||||||
keyArr = ToStrings(args[1:])
|
|
||||||
} else {
|
|
||||||
keyArr = ToStrings(args)
|
|
||||||
}
|
|
||||||
|
|
||||||
return keyToggles(key, keyArr, pid)
|
return keyToggles(key, keyArr, pid)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -14,7 +14,7 @@
|
|||||||
package robotgo
|
package robotgo
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/otiai10/gosseract"
|
"github.com/otiai10/gosseract/v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
// GetText get the image text by tesseract ocr
|
// GetText get the image text by tesseract ocr
|
||||||
|
16
window/pub.h
16
window/pub.h
@ -42,7 +42,7 @@ typedef struct _Bounds Bounds;
|
|||||||
static Boolean(*gAXIsProcessTrustedWithOptions) (CFDictionaryRef);
|
static Boolean(*gAXIsProcessTrustedWithOptions) (CFDictionaryRef);
|
||||||
static CFStringRef* gkAXTrustedCheckOptionPrompt;
|
static CFStringRef* gkAXTrustedCheckOptionPrompt;
|
||||||
|
|
||||||
AXError _AXUIElementGetWindow(AXUIElementRef, CGWindowID* out);
|
//AXError _AXUIElementGetWindow(AXUIElementRef, CGWindowID* out);
|
||||||
static AXUIElementRef GetUIElement(CGWindowID win){
|
static AXUIElementRef GetUIElement(CGWindowID win){
|
||||||
intptr pid = 0;
|
intptr pid = 0;
|
||||||
// double_t pid = 0;
|
// double_t pid = 0;
|
||||||
@ -90,13 +90,25 @@ typedef struct _Bounds Bounds;
|
|||||||
AXUIElementRef element = (AXUIElementRef) CFArrayGetValueAtIndex(windows, i);
|
AXUIElementRef element = (AXUIElementRef) CFArrayGetValueAtIndex(windows, i);
|
||||||
CGWindowID temp = 0;
|
CGWindowID temp = 0;
|
||||||
// Use undocumented API to get WindowID
|
// Use undocumented API to get WindowID
|
||||||
_AXUIElementGetWindow(element, &temp);
|
/*_AXUIElementGetWindow(element, &temp);
|
||||||
|
|
||||||
if (temp == win) {
|
if (temp == win) {
|
||||||
// Retain element
|
// Retain element
|
||||||
CFRetain(element);
|
CFRetain(element);
|
||||||
result = element;
|
result = element;
|
||||||
break;
|
break;
|
||||||
|
}*/
|
||||||
|
CFTypeRef cfWindow = NULL;
|
||||||
|
if (AXUIElementCopyAttributeValue(element, kAXWindowAttribute, &cfWindow) == kAXErrorSuccess && cfWindow != NULL) {
|
||||||
|
temp = *(CGWindowID*)CFDataGetBytePtr(cfWindow);
|
||||||
|
CFRelease(cfWindow);
|
||||||
|
|
||||||
|
if (temp == win) {
|
||||||
|
// Retain element
|
||||||
|
CFRetain(element);
|
||||||
|
result = element;
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -366,7 +366,7 @@ MData get_active(void) {
|
|||||||
AXUIElementRef focused = AXUIElementCreateApplication(pid);
|
AXUIElementRef focused = AXUIElementCreateApplication(pid);
|
||||||
if (focused == NULL) { return result; } // Verify
|
if (focused == NULL) { return result; } // Verify
|
||||||
|
|
||||||
AXUIElementRef element;
|
/*AXUIElementRef element;
|
||||||
CGWindowID win = 0;
|
CGWindowID win = 0;
|
||||||
// Retrieve the currently focused window
|
// Retrieve the currently focused window
|
||||||
if (AXUIElementCopyAttributeValue(focused, kAXFocusedWindowAttribute, (CFTypeRef*) &element)
|
if (AXUIElementCopyAttributeValue(focused, kAXFocusedWindowAttribute, (CFTypeRef*) &element)
|
||||||
@ -383,7 +383,31 @@ MData get_active(void) {
|
|||||||
} else {
|
} else {
|
||||||
result.CgID = win;
|
result.CgID = win;
|
||||||
result.AxID = element;
|
result.AxID = element;
|
||||||
|
}*/
|
||||||
|
|
||||||
|
AXUIElementRef windowElement = NULL;
|
||||||
|
CGWindowID win = 0;
|
||||||
|
|
||||||
|
// Retrieve the currently focused window
|
||||||
|
if (AXUIElementCopyAttributeValue(focused, kAXFocusedWindowAttribute, (CFTypeRef*) &windowElement) == kAXErrorSuccess && windowElement) {
|
||||||
|
// Use AXUIElementCopyAttributeValue to get parent window
|
||||||
|
CFTypeRef cfWindow = NULL;
|
||||||
|
if (AXUIElementCopyAttributeValue(windowElement, kAXParentAttribute, &cfWindow) == kAXErrorSuccess && cfWindow != NULL) {
|
||||||
|
AXUIElementRef parentWindow = (AXUIElementRef)cfWindow;
|
||||||
|
if (AXUIElementCopyAttributeValue(parentWindow, kAXWindowAttribute, (CFTypeRef*)&win) == kAXErrorSuccess && win) {
|
||||||
|
// Manually set internals
|
||||||
|
result.CgID = win;
|
||||||
|
result.AxID = windowElement;
|
||||||
|
} else {
|
||||||
|
CFRelease(parentWindow);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
CFRelease(windowElement);
|
||||||
|
} else {
|
||||||
|
result.CgID = 0;
|
||||||
|
result.AxID = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
CFRelease(focused);
|
CFRelease(focused);
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
|
Loading…
Reference in New Issue
Block a user