From 0293ab5d0c63911c604821bcb8c2b2555a6b62bb Mon Sep 17 00:00:00 2001 From: vcaesar Date: Tue, 1 Nov 2022 10:39:48 -0700 Subject: [PATCH 1/6] Add: add win32 send key event by pid support --- key/keypress.h | 2 +- key/keypress_c.h | 15 ++++++++++++--- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/key/keypress.h b/key/keypress.h index 6e69c710..779fab8e 100644 --- a/key/keypress.h +++ b/key/keypress.h @@ -40,7 +40,7 @@ #if defined(IS_WINDOWS) /* Send win32 key event for given key. */ - void win32KeyEvent(int key, MMKeyFlags flags, int32_t pid); + void win32KeyEvent(int key, MMKeyFlags flags, int32_t pid, int32_t isPid); #endif #endif /* KEYPRESS_H */ diff --git a/key/keypress_c.h b/key/keypress_c.h index 4d4959f1..614cf37d 100644 --- a/key/keypress_c.h +++ b/key/keypress_c.h @@ -16,7 +16,7 @@ /* Convenience wrappers around ugly APIs. */ #if defined(IS_WINDOWS) void WIN32_KEY_EVENT_WAIT(MMKeyCode key, DWORD flags, int32_t pid) { - win32KeyEvent(key, flags, pid); + win32KeyEvent(key, flags, pid, 0); Sleep(DEADBEEF_RANDRANGE(0, 1)); } #elif defined(USE_X11) @@ -68,7 +68,7 @@ static io_connect_t _getAuxiliaryKeyDriver(void) { #endif #if defined(IS_WINDOWS) -void win32KeyEvent(int key, MMKeyFlags flags, int32_t pid) { +void win32KeyEvent(int key, MMKeyFlags flags, int32_t pid, int32_t isPid) { int scan = MapVirtualKey(key & 0xff, MAPVK_VK_TO_VSC); /* Set the scan code for extended keys */ @@ -111,6 +111,15 @@ void win32KeyEvent(int key, MMKeyFlags flags, int32_t pid) { } } + if (pid != 0) { + HWND hwnd = (HWND) pid; + if (isPid == 0) { + hwnd = GetHwndByPid(pid); + } + SendMessage(hwnd, flags == 0 ? WM_KEYDOWN : WM_KEYUP, key, 0); + return; + } + /* Set the scan code for keyup */ // if ( flags & KEYEVENTF_KEYUP ) { // scan |= 0x80; @@ -168,7 +177,7 @@ void toggleKeyCode(MMKeyCode code, const bool down, MMKeyFlags flags, int32_t pi if (flags & MOD_CONTROL) { WIN32_KEY_EVENT_WAIT(K_CONTROL, dwFlags, pid); } if (flags & MOD_SHIFT) { WIN32_KEY_EVENT_WAIT(K_SHIFT, dwFlags, pid); } - win32KeyEvent(code, dwFlags, pid); + win32KeyEvent(code, dwFlags, pid, 0); #elif defined(USE_X11) Display *display = XGetMainDisplay(); const Bool is_press = down ? True : False; /* Just to be safe. */ From a8faa0e90649f8a4793be1835612709ca6d16a53 Mon Sep 17 00:00:00 2001 From: vcaesar Date: Mon, 21 Nov 2022 10:29:09 -0800 Subject: [PATCH 2/6] Update README.md --- README.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 56e36181..331fdb15 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ -> Golang Desktop Automation. Control the mouse, keyboard, bitmap and image, read the screen, process, Window Handle and global event listener. +> Golang Desktop Automation. Control the mouse, keyboard, read the screen, process, Window Handle, image and bitmap and global event listener. RobotGo supports Mac, Windows, and Linux(X11); and robotgo supports arm64 and x86-amd64. @@ -65,6 +65,10 @@ xcode-select --install [MinGW-w64](https://sourceforge.net/projects/mingw-w64/files) (Use recommended) +Download the Mingw, then set system environment variables `C:\mingw64\bin` to the Path. +[Set environment variables to run GCC from command line](https://www.youtube.com/watch?v=1kY5iTK4-H0). + + ``` Or the other GCC (But you should compile the "libpng" with yourself when use the bitmap.) ``` From bb90d71b5f5c7375fc780a7a99e8fd1fc692bb88 Mon Sep 17 00:00:00 2001 From: vcaesar Date: Sun, 27 Nov 2022 18:30:05 -0800 Subject: [PATCH 3/6] Add: add windows key tap and type by pid support --- key.go | 14 ++++++++++---- key/keypress.h | 2 +- key/keypress_c.h | 28 ++++++++++++++++++++++------ 3 files changed, 33 insertions(+), 11 deletions(-) diff --git a/key.go b/key.go index c2e05a72..e59e1cf4 100644 --- a/key.go +++ b/key.go @@ -321,7 +321,7 @@ var keyNames = map[string]C.MMKeyCode{ // { NULL: C.K_NOT_A_KEY } } -func tapKeyCode(code C.MMKeyCode, flags C.MMKeyFlags, pid C.int32_t) { +func tapKeyCode(code C.MMKeyCode, flags C.MMKeyFlags, pid C.uintptr) { C.toggleKeyCode(code, true, flags, pid) MilliSleep(3) C.toggleKeyCode(code, false, flags, pid) @@ -401,7 +401,7 @@ func keyTaps(k string, keyArr []string, pid int) error { return err } - tapKeyCode(key, flags, C.int32_t(pid)) + tapKeyCode(key, flags, C.uintptr(pid)) MilliSleep(KeySleep) return nil } @@ -426,7 +426,7 @@ func keyToggles(k string, keyArr []string, pid int) error { return err } - C.toggleKeyCode(key, C.bool(down), flags, C.int32_t(pid)) + C.toggleKeyCode(key, C.bool(down), flags, C.uintptr(pid)) MilliSleep(KeySleep) return nil } @@ -604,7 +604,13 @@ func UnicodeType(str uint32, args ...int) { if len(args) > 0 { pid = args[0] } - C.unicodeType(cstr, C.int32_t(pid)) + + isPid := 0 + if len(args) > 1 { + isPid = args[1] + } + + C.unicodeType(cstr, C.uintptr(pid), C.int8_t(isPid)) } // ToUC trans string to unicode []string diff --git a/key/keypress.h b/key/keypress.h index 779fab8e..3079d294 100644 --- a/key/keypress.h +++ b/key/keypress.h @@ -40,7 +40,7 @@ #if defined(IS_WINDOWS) /* Send win32 key event for given key. */ - void win32KeyEvent(int key, MMKeyFlags flags, int32_t pid, int32_t isPid); + void win32KeyEvent(int key, MMKeyFlags flags, uintptr pid, int8_t isPid); #endif #endif /* KEYPRESS_H */ diff --git a/key/keypress_c.h b/key/keypress_c.h index 614cf37d..32e8daee 100644 --- a/key/keypress_c.h +++ b/key/keypress_c.h @@ -15,6 +15,8 @@ /* Convenience wrappers around ugly APIs. */ #if defined(IS_WINDOWS) + HWND GetHwndByPid(DWORD dwProcessId); + void WIN32_KEY_EVENT_WAIT(MMKeyCode key, DWORD flags, int32_t pid) { win32KeyEvent(key, flags, pid, 0); Sleep(DEADBEEF_RANDRANGE(0, 1)); @@ -34,7 +36,7 @@ #endif #if defined(IS_MACOSX) - int SendTo(int32_t pid, CGEventRef event) { + int SendTo(uintptr pid, CGEventRef event) { if (pid != 0) { CGEventPostToPid(pid, event); } else { @@ -111,12 +113,15 @@ void win32KeyEvent(int key, MMKeyFlags flags, int32_t pid, int32_t isPid) { } } + // todo: test this if (pid != 0) { HWND hwnd = (HWND) pid; if (isPid == 0) { hwnd = GetHwndByPid(pid); } - SendMessage(hwnd, flags == 0 ? WM_KEYDOWN : WM_KEYUP, key, 0); + int down = (flags == 0 ? WM_KEYDOWN : WM_KEYUP); + // SendMessage(hwnd, down, key, 0); + PostMessageW(hwnd, down, key, 0); return; } @@ -138,7 +143,7 @@ void win32KeyEvent(int key, MMKeyFlags flags, int32_t pid, int32_t isPid) { } #endif -void toggleKeyCode(MMKeyCode code, const bool down, MMKeyFlags flags, int32_t pid) { +void toggleKeyCode(MMKeyCode code, const bool down, MMKeyFlags flags, uintptr pid) { #if defined(IS_MACOSX) /* The media keys all have 1000 added to them to help us detect them. */ if (code >= 1000) { @@ -215,7 +220,7 @@ void toggleKeyCode(MMKeyCode code, const bool down, MMKeyFlags flags, int32_t pi } #endif -void toggleKey(char c, const bool down, MMKeyFlags flags, int32_t pid) { +void toggleKey(char c, const bool down, MMKeyFlags flags, uintptr pid) { MMKeyCode keyCode = keyCodeForChar(c); //Prevent unused variable warning for Mac and Linux. @@ -251,7 +256,7 @@ void toggleKey(char c, const bool down, MMKeyFlags flags, int32_t pid) { // } #if defined(IS_MACOSX) -void toggleUnicode(UniChar ch, const bool down, int32_t pid) { +void toggleUnicode(UniChar ch, const bool down, uintptr pid) { /* This function relies on the convenient CGEventKeyboardSetUnicodeString(), convert characters to a keycode, but does not support adding modifier flags. It is only used in typeString(). @@ -302,7 +307,7 @@ void toggleUnicode(UniChar ch, const bool down, int32_t pid) { #endif // unicode type -void unicodeType(const unsigned value, int32_t pid){ +void unicodeType(const unsigned value, uintptr pid, int8_t isPid){ #if defined(IS_MACOSX) UniChar ch = (UniChar)value; // Convert to unsigned char @@ -310,6 +315,17 @@ void unicodeType(const unsigned value, int32_t pid){ microsleep(5.0); toggleUnicode(ch, false, pid); #elif defined(IS_WINDOWS) + if (pid != 0) { + HWND hwnd = (HWND) pid; + if (isPid == 0) { + hwnd = GetHwndByPid(pid); + } + + // SendMessage(hwnd, down, value, 0); + PostMessageW(hwnd, WM_CHAR, value, 0); + return; + } + INPUT input[2]; memset(input, 0, sizeof(input)); From 7103f6b89113282d7c15bb2dcb2b177e1bda913f Mon Sep 17 00:00:00 2001 From: vcaesar Date: Sun, 27 Nov 2022 18:33:32 -0800 Subject: [PATCH 4/6] Update README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 331fdb15..03f44c4d 100644 --- a/README.md +++ b/README.md @@ -66,7 +66,7 @@ xcode-select --install [MinGW-w64](https://sourceforge.net/projects/mingw-w64/files) (Use recommended) Download the Mingw, then set system environment variables `C:\mingw64\bin` to the Path. -[Set environment variables to run GCC from command line](https://www.youtube.com/watch?v=1kY5iTK4-H0). +[Set environment variables to run GCC from command line](https://www.youtube.com/results?search_query=Set+environment+variables+to+run+GCC+from+command+line). ``` @@ -453,7 +453,7 @@ func main() { fmt.Println("pids... ", fpid) if len(fpid) > 0 { - robotgo.TypeStr("Hi galaxy!", int(fpid[0])) + robotgo.TypeStr("Hi galaxy!", fpid[0]) robotgo.KeyTap("a", fpid[0], "cmd") robotgo.KeyToggle("a", fpid[0]) From 4eea01b88d4c16bd591ee09cf74dafba357ede1e Mon Sep 17 00:00:00 2001 From: vcaesar Date: Sun, 27 Nov 2022 18:38:05 -0800 Subject: [PATCH 5/6] Fixed: fixed windows int type --- key/keypress_c.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/key/keypress_c.h b/key/keypress_c.h index 32e8daee..dd0134f7 100644 --- a/key/keypress_c.h +++ b/key/keypress_c.h @@ -17,7 +17,7 @@ #if defined(IS_WINDOWS) HWND GetHwndByPid(DWORD dwProcessId); - void WIN32_KEY_EVENT_WAIT(MMKeyCode key, DWORD flags, int32_t pid) { + void WIN32_KEY_EVENT_WAIT(MMKeyCode key, DWORD flags, uiprint pid) { win32KeyEvent(key, flags, pid, 0); Sleep(DEADBEEF_RANDRANGE(0, 1)); } @@ -70,7 +70,7 @@ static io_connect_t _getAuxiliaryKeyDriver(void) { #endif #if defined(IS_WINDOWS) -void win32KeyEvent(int key, MMKeyFlags flags, int32_t pid, int32_t isPid) { +void win32KeyEvent(int key, MMKeyFlags flags, uintptr pid, int8_t isPid) { int scan = MapVirtualKey(key & 0xff, MAPVK_VK_TO_VSC); /* Set the scan code for extended keys */ From 7aa5beb8f7e6c3dc59b1e99079681d8a25bafc2e Mon Sep 17 00:00:00 2001 From: vcaesar Date: Sun, 27 Nov 2022 18:40:51 -0800 Subject: [PATCH 6/6] Fixed: fixed typo --- key/keypress_c.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/key/keypress_c.h b/key/keypress_c.h index dd0134f7..53dd3047 100644 --- a/key/keypress_c.h +++ b/key/keypress_c.h @@ -17,7 +17,7 @@ #if defined(IS_WINDOWS) HWND GetHwndByPid(DWORD dwProcessId); - void WIN32_KEY_EVENT_WAIT(MMKeyCode key, DWORD flags, uiprint pid) { + void WIN32_KEY_EVENT_WAIT(MMKeyCode key, DWORD flags, uintptr pid) { win32KeyEvent(key, flags, pid, 0); Sleep(DEADBEEF_RANDRANGE(0, 1)); }