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
This commit is contained in:
Evans 2025-06-03 11:19:24 -07:00 committed by GitHub
commit c431c8f974
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 40 additions and 37 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