Merge pull request #543 from go-vgo/bitmap-pr

Add: add win32 send key event by pid support
This commit is contained in:
Evans 2022-11-27 18:43:34 -08:00 committed by GitHub
commit 2f936e1f49
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 51 additions and 16 deletions

View File

@ -15,7 +15,7 @@
<!-- [![Release](https://github-release-version.herokuapp.com/github/go-vgo/robotgo/release.svg?style=flat)](https://github.com/go-vgo/robotgo/releases/latest) --> <!-- [![Release](https://github-release-version.herokuapp.com/github/go-vgo/robotgo/release.svg?style=flat)](https://github.com/go-vgo/robotgo/releases/latest) -->
<!-- <a href="https://github.com/go-vgo/robotgo/releases"><img src="https://img.shields.io/badge/%20version%20-%206.0.0%20-blue.svg?style=flat-square" alt="Releases"></a> --> <!-- <a href="https://github.com/go-vgo/robotgo/releases"><img src="https://img.shields.io/badge/%20version%20-%206.0.0%20-blue.svg?style=flat-square" alt="Releases"></a> -->
> 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. 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) [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/results?search_query=Set+environment+variables+to+run+GCC+from+command+line).
``` ```
Or the other GCC (But you should compile the "libpng" with yourself when use the bitmap.) Or the other GCC (But you should compile the "libpng" with yourself when use the bitmap.)
``` ```
@ -449,7 +453,7 @@ func main() {
fmt.Println("pids... ", fpid) fmt.Println("pids... ", fpid)
if len(fpid) > 0 { if len(fpid) > 0 {
robotgo.TypeStr("Hi galaxy!", int(fpid[0])) robotgo.TypeStr("Hi galaxy!", fpid[0])
robotgo.KeyTap("a", fpid[0], "cmd") robotgo.KeyTap("a", fpid[0], "cmd")
robotgo.KeyToggle("a", fpid[0]) robotgo.KeyToggle("a", fpid[0])

14
key.go
View File

@ -321,7 +321,7 @@ var keyNames = map[string]C.MMKeyCode{
// { NULL: C.K_NOT_A_KEY } // { 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) C.toggleKeyCode(code, true, flags, pid)
MilliSleep(3) MilliSleep(3)
C.toggleKeyCode(code, false, flags, pid) C.toggleKeyCode(code, false, flags, pid)
@ -401,7 +401,7 @@ func keyTaps(k string, keyArr []string, pid int) error {
return err return err
} }
tapKeyCode(key, flags, C.int32_t(pid)) tapKeyCode(key, flags, C.uintptr(pid))
MilliSleep(KeySleep) MilliSleep(KeySleep)
return nil return nil
} }
@ -426,7 +426,7 @@ func keyToggles(k string, keyArr []string, pid int) error {
return err 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) MilliSleep(KeySleep)
return nil return nil
} }
@ -604,7 +604,13 @@ func UnicodeType(str uint32, args ...int) {
if len(args) > 0 { if len(args) > 0 {
pid = 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 // ToUC trans string to unicode []string

View File

@ -40,7 +40,7 @@
#if defined(IS_WINDOWS) #if defined(IS_WINDOWS)
/* Send win32 key event for given key. */ /* Send win32 key event for given key. */
void win32KeyEvent(int key, MMKeyFlags flags, int32_t pid); void win32KeyEvent(int key, MMKeyFlags flags, uintptr pid, int8_t isPid);
#endif #endif
#endif /* KEYPRESS_H */ #endif /* KEYPRESS_H */

View File

@ -15,8 +15,10 @@
/* Convenience wrappers around ugly APIs. */ /* Convenience wrappers around ugly APIs. */
#if defined(IS_WINDOWS) #if defined(IS_WINDOWS)
void WIN32_KEY_EVENT_WAIT(MMKeyCode key, DWORD flags, int32_t pid) { HWND GetHwndByPid(DWORD dwProcessId);
win32KeyEvent(key, flags, pid);
void WIN32_KEY_EVENT_WAIT(MMKeyCode key, DWORD flags, uintptr pid) {
win32KeyEvent(key, flags, pid, 0);
Sleep(DEADBEEF_RANDRANGE(0, 1)); Sleep(DEADBEEF_RANDRANGE(0, 1));
} }
#elif defined(USE_X11) #elif defined(USE_X11)
@ -34,7 +36,7 @@
#endif #endif
#if defined(IS_MACOSX) #if defined(IS_MACOSX)
int SendTo(int32_t pid, CGEventRef event) { int SendTo(uintptr pid, CGEventRef event) {
if (pid != 0) { if (pid != 0) {
CGEventPostToPid(pid, event); CGEventPostToPid(pid, event);
} else { } else {
@ -68,7 +70,7 @@ static io_connect_t _getAuxiliaryKeyDriver(void) {
#endif #endif
#if defined(IS_WINDOWS) #if defined(IS_WINDOWS)
void win32KeyEvent(int key, MMKeyFlags flags, int32_t pid) { void win32KeyEvent(int key, MMKeyFlags flags, uintptr pid, int8_t isPid) {
int scan = MapVirtualKey(key & 0xff, MAPVK_VK_TO_VSC); int scan = MapVirtualKey(key & 0xff, MAPVK_VK_TO_VSC);
/* Set the scan code for extended keys */ /* Set the scan code for extended keys */
@ -111,6 +113,18 @@ void win32KeyEvent(int key, MMKeyFlags flags, int32_t pid) {
} }
} }
// todo: test this
if (pid != 0) {
HWND hwnd = (HWND) pid;
if (isPid == 0) {
hwnd = GetHwndByPid(pid);
}
int down = (flags == 0 ? WM_KEYDOWN : WM_KEYUP);
// SendMessage(hwnd, down, key, 0);
PostMessageW(hwnd, down, key, 0);
return;
}
/* Set the scan code for keyup */ /* Set the scan code for keyup */
// if ( flags & KEYEVENTF_KEYUP ) { // if ( flags & KEYEVENTF_KEYUP ) {
// scan |= 0x80; // scan |= 0x80;
@ -129,7 +143,7 @@ void win32KeyEvent(int key, MMKeyFlags flags, int32_t pid) {
} }
#endif #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) #if defined(IS_MACOSX)
/* The media keys all have 1000 added to them to help us detect them. */ /* The media keys all have 1000 added to them to help us detect them. */
if (code >= 1000) { if (code >= 1000) {
@ -168,7 +182,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_CONTROL) { WIN32_KEY_EVENT_WAIT(K_CONTROL, dwFlags, pid); }
if (flags & MOD_SHIFT) { WIN32_KEY_EVENT_WAIT(K_SHIFT, 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) #elif defined(USE_X11)
Display *display = XGetMainDisplay(); Display *display = XGetMainDisplay();
const Bool is_press = down ? True : False; /* Just to be safe. */ const Bool is_press = down ? True : False; /* Just to be safe. */
@ -206,7 +220,7 @@ void toggleKeyCode(MMKeyCode code, const bool down, MMKeyFlags flags, int32_t pi
} }
#endif #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); MMKeyCode keyCode = keyCodeForChar(c);
//Prevent unused variable warning for Mac and Linux. //Prevent unused variable warning for Mac and Linux.
@ -242,7 +256,7 @@ void toggleKey(char c, const bool down, MMKeyFlags flags, int32_t pid) {
// } // }
#if defined(IS_MACOSX) #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(), /* This function relies on the convenient CGEventKeyboardSetUnicodeString(),
convert characters to a keycode, but does not support adding modifier flags. convert characters to a keycode, but does not support adding modifier flags.
It is only used in typeString(). It is only used in typeString().
@ -293,7 +307,7 @@ void toggleUnicode(UniChar ch, const bool down, int32_t pid) {
#endif #endif
// unicode type // unicode type
void unicodeType(const unsigned value, int32_t pid){ void unicodeType(const unsigned value, uintptr pid, int8_t isPid){
#if defined(IS_MACOSX) #if defined(IS_MACOSX)
UniChar ch = (UniChar)value; // Convert to unsigned char UniChar ch = (UniChar)value; // Convert to unsigned char
@ -301,6 +315,17 @@ void unicodeType(const unsigned value, int32_t pid){
microsleep(5.0); microsleep(5.0);
toggleUnicode(ch, false, pid); toggleUnicode(ch, false, pid);
#elif defined(IS_WINDOWS) #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]; INPUT input[2];
memset(input, 0, sizeof(input)); memset(input, 0, sizeof(input));