Skip to content

Commit

Permalink
Merge pull request #543 from go-vgo/bitmap-pr
Browse files Browse the repository at this point in the history
Add: add win32 send key event by pid support
  • Loading branch information
vcaesar authored Nov 28, 2022
2 parents be6984f + 7aa5beb commit 2f936e1
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 16 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) -->
<!-- <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.

Expand Down Expand Up @@ -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/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.)
```
Expand Down Expand Up @@ -449,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])
Expand Down
14 changes: 10 additions & 4 deletions key.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion key/keypress.h
Original file line number Diff line number Diff line change
Expand Up @@ -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, uintptr pid, int8_t isPid);
#endif

#endif /* KEYPRESS_H */
43 changes: 34 additions & 9 deletions key/keypress_c.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@

/* 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);
HWND GetHwndByPid(DWORD dwProcessId);

void WIN32_KEY_EVENT_WAIT(MMKeyCode key, DWORD flags, uintptr pid) {
win32KeyEvent(key, flags, pid, 0);
Sleep(DEADBEEF_RANDRANGE(0, 1));
}
#elif defined(USE_X11)
Expand All @@ -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 {
Expand Down Expand Up @@ -68,7 +70,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, uintptr pid, int8_t isPid) {
int scan = MapVirtualKey(key & 0xff, MAPVK_VK_TO_VSC);

/* Set the scan code for extended keys */
Expand Down Expand Up @@ -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 */
// if ( flags & KEYEVENTF_KEYUP ) {
// scan |= 0x80;
Expand All @@ -129,7 +143,7 @@ void win32KeyEvent(int key, MMKeyFlags flags, int32_t pid) {
}
#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) {
Expand Down Expand Up @@ -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_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. */
Expand Down Expand Up @@ -206,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.
Expand Down Expand Up @@ -242,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().
Expand Down Expand Up @@ -293,14 +307,25 @@ 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

toggleUnicode(ch, true, 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));

Expand Down

0 comments on commit 2f936e1

Please sign in to comment.