Compare commits

...

8 Commits

Author SHA1 Message Date
Sakishum
4e9da867c2
Merge 9aa4059a31 into c431c8f974 2025-06-06 09:15:57 -07:00
Evans
c431c8f974
Merge pull request #727 from go-vgo/bitmap-pr
Some checks failed
Go / test (macOS-latest) (push) Has been cancelled
Go / test (windows-latest) (push) Has been cancelled
Update: simply key code
2025-06-03 11:19:24 -07:00
vcaesar
a183783e9c Update: simply key code 2025-06-03 11:16:33 -07:00
vcaesar
c1115cebc7 Update: update import
Some checks failed
Go / test (macOS-latest) (push) Has been cancelled
Go / test (windows-latest) (push) Has been cancelled
2025-05-23 19:00:53 -07:00
saki.shen
9aa4059a31 [EDIT] By Saki, replace _AXUIElementGetWindow with AXUIElementCopyAttributeValue 2024-03-19 17:01:07 +08:00
saki.shen
31ddf1a959 [EDIT] By Saki, fixed bug of previous definition 2024-03-19 16:46:34 +08:00
saki.shen
de95edb1a8 [EDIT] By Saki, 去除 _AXUIElementGetWindow 的调用 2024-03-19 16:41:19 +08:00
saki.shen
df394a94b8 [EDIT] By Saki, 去除 robotgo.go 的 window/goWindow.h 2024-03-19 16:16:39 +08:00
4 changed files with 80 additions and 41 deletions

75
key.go
View File

@ -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)
} }

View File

@ -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

View File

@ -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;
}
} }
} }

View File

@ -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;