mirror of
https://github.com/go-vgo/robotgo.git
synced 2025-05-31 06:13:55 +00:00
Update: update and remove some old code
This commit is contained in:
parent
6fa2f418fa
commit
5c3cbd6e17
262
key/keypress_c.h
262
key/keypress_c.h
@ -65,100 +65,99 @@
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static io_connect_t _getAuxiliaryKeyDriver(void) {
|
static io_connect_t _getAuxiliaryKeyDriver(void) {
|
||||||
static mach_port_t sEventDrvrRef = 0;
|
static mach_port_t sEventDrvrRef = 0;
|
||||||
mach_port_t masterPort, service, iter;
|
mach_port_t masterPort, service, iter;
|
||||||
kern_return_t kr;
|
kern_return_t kr;
|
||||||
|
|
||||||
if (!sEventDrvrRef) {
|
if (!sEventDrvrRef) {
|
||||||
kr = IOMasterPort(bootstrap_port, &masterPort);
|
kr = IOMasterPort(bootstrap_port, &masterPort);
|
||||||
assert(KERN_SUCCESS == kr);
|
assert(KERN_SUCCESS == kr);
|
||||||
kr = IOServiceGetMatchingServices(masterPort, IOServiceMatching(kIOHIDSystemClass), &iter);
|
kr = IOServiceGetMatchingServices(masterPort, IOServiceMatching(kIOHIDSystemClass), &iter);
|
||||||
assert(KERN_SUCCESS == kr);
|
assert(KERN_SUCCESS == kr);
|
||||||
|
|
||||||
service = IOIteratorNext(iter);
|
service = IOIteratorNext(iter);
|
||||||
assert(service);
|
assert(service);
|
||||||
|
|
||||||
kr = IOServiceOpen(service, mach_task_self(), kIOHIDParamConnectType, &sEventDrvrRef);
|
|
||||||
assert(KERN_SUCCESS == kr);
|
|
||||||
|
|
||||||
IOObjectRelease(service);
|
kr = IOServiceOpen(service, mach_task_self(), kIOHIDParamConnectType, &sEventDrvrRef);
|
||||||
IOObjectRelease(iter);
|
assert(KERN_SUCCESS == kr);
|
||||||
}
|
|
||||||
return sEventDrvrRef;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(IS_WINDOWS)
|
IOObjectRelease(service);
|
||||||
void win32KeyEvent(int key, MMKeyFlags flags, uintptr pid, int8_t isPid) {
|
IOObjectRelease(iter);
|
||||||
int scan = MapVirtualKey(key & 0xff, MAPVK_VK_TO_VSC);
|
|
||||||
|
|
||||||
/* Set the scan code for extended keys */
|
|
||||||
switch (key){
|
|
||||||
case VK_RCONTROL:
|
|
||||||
case VK_SNAPSHOT: /* Print Screen */
|
|
||||||
case VK_RMENU: /* Right Alt / Alt Gr */
|
|
||||||
case VK_PAUSE: /* Pause / Break */
|
|
||||||
case VK_HOME:
|
|
||||||
case VK_UP:
|
|
||||||
case VK_PRIOR: /* Page up */
|
|
||||||
case VK_LEFT:
|
|
||||||
case VK_RIGHT:
|
|
||||||
case VK_END:
|
|
||||||
case VK_DOWN:
|
|
||||||
case VK_NEXT: /* 'Page Down' */
|
|
||||||
case VK_INSERT:
|
|
||||||
case VK_DELETE:
|
|
||||||
case VK_LWIN:
|
|
||||||
case VK_RWIN:
|
|
||||||
case VK_APPS: /* Application */
|
|
||||||
case VK_VOLUME_MUTE:
|
|
||||||
case VK_VOLUME_DOWN:
|
|
||||||
case VK_VOLUME_UP:
|
|
||||||
case VK_MEDIA_NEXT_TRACK:
|
|
||||||
case VK_MEDIA_PREV_TRACK:
|
|
||||||
case VK_MEDIA_STOP:
|
|
||||||
case VK_MEDIA_PLAY_PAUSE:
|
|
||||||
case VK_BROWSER_BACK:
|
|
||||||
case VK_BROWSER_FORWARD:
|
|
||||||
case VK_BROWSER_REFRESH:
|
|
||||||
case VK_BROWSER_STOP:
|
|
||||||
case VK_BROWSER_SEARCH:
|
|
||||||
case VK_BROWSER_FAVORITES:
|
|
||||||
case VK_BROWSER_HOME:
|
|
||||||
case VK_LAUNCH_MAIL:
|
|
||||||
{
|
|
||||||
flags |= KEYEVENTF_EXTENDEDKEY;
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
return sEventDrvrRef;
|
||||||
}
|
}
|
||||||
|
#elif defined(IS_WINDOWS)
|
||||||
|
|
||||||
// todo: test this
|
void win32KeyEvent(int key, MMKeyFlags flags, uintptr pid, int8_t isPid) {
|
||||||
if (pid != 0) {
|
int scan = MapVirtualKey(key & 0xff, MAPVK_VK_TO_VSC);
|
||||||
HWND hwnd = getHwnd(pid, isPid);
|
|
||||||
|
|
||||||
int down = (flags == 0 ? WM_KEYDOWN : WM_KEYUP);
|
/* Set the scan code for extended keys */
|
||||||
// SendMessage(hwnd, down, key, 0);
|
switch (key){
|
||||||
PostMessageW(hwnd, down, key, 0);
|
case VK_RCONTROL:
|
||||||
return;
|
case VK_SNAPSHOT: /* Print Screen */
|
||||||
|
case VK_RMENU: /* Right Alt / Alt Gr */
|
||||||
|
case VK_PAUSE: /* Pause / Break */
|
||||||
|
case VK_HOME:
|
||||||
|
case VK_UP:
|
||||||
|
case VK_PRIOR: /* Page up */
|
||||||
|
case VK_LEFT:
|
||||||
|
case VK_RIGHT:
|
||||||
|
case VK_END:
|
||||||
|
case VK_DOWN:
|
||||||
|
case VK_NEXT: /* 'Page Down' */
|
||||||
|
case VK_INSERT:
|
||||||
|
case VK_DELETE:
|
||||||
|
case VK_LWIN:
|
||||||
|
case VK_RWIN:
|
||||||
|
case VK_APPS: /* Application */
|
||||||
|
case VK_VOLUME_MUTE:
|
||||||
|
case VK_VOLUME_DOWN:
|
||||||
|
case VK_VOLUME_UP:
|
||||||
|
case VK_MEDIA_NEXT_TRACK:
|
||||||
|
case VK_MEDIA_PREV_TRACK:
|
||||||
|
case VK_MEDIA_STOP:
|
||||||
|
case VK_MEDIA_PLAY_PAUSE:
|
||||||
|
case VK_BROWSER_BACK:
|
||||||
|
case VK_BROWSER_FORWARD:
|
||||||
|
case VK_BROWSER_REFRESH:
|
||||||
|
case VK_BROWSER_STOP:
|
||||||
|
case VK_BROWSER_SEARCH:
|
||||||
|
case VK_BROWSER_FAVORITES:
|
||||||
|
case VK_BROWSER_HOME:
|
||||||
|
case VK_LAUNCH_MAIL:
|
||||||
|
{
|
||||||
|
flags |= KEYEVENTF_EXTENDEDKEY;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// todo: test this
|
||||||
|
if (pid != 0) {
|
||||||
|
HWND hwnd = getHwnd(pid, isPid);
|
||||||
|
|
||||||
|
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;
|
||||||
|
// }
|
||||||
|
// keybd_event(key, scan, flags, 0);
|
||||||
|
|
||||||
|
INPUT keyInput;
|
||||||
|
|
||||||
|
keyInput.type = INPUT_KEYBOARD;
|
||||||
|
keyInput.ki.wVk = key;
|
||||||
|
keyInput.ki.wScan = scan;
|
||||||
|
keyInput.ki.dwFlags = flags;
|
||||||
|
keyInput.ki.time = 0;
|
||||||
|
keyInput.ki.dwExtraInfo = 0;
|
||||||
|
SendInput(1, &keyInput, sizeof(keyInput));
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Set the scan code for keyup */
|
|
||||||
// if ( flags & KEYEVENTF_KEYUP ) {
|
|
||||||
// scan |= 0x80;
|
|
||||||
// }
|
|
||||||
// keybd_event(key, scan, flags, 0);
|
|
||||||
|
|
||||||
INPUT keyInput;
|
|
||||||
|
|
||||||
keyInput.type = INPUT_KEYBOARD;
|
|
||||||
keyInput.ki.wVk = key;
|
|
||||||
keyInput.ki.wScan = scan;
|
|
||||||
keyInput.ki.dwFlags = flags;
|
|
||||||
keyInput.ki.time = 0;
|
|
||||||
keyInput.ki.dwExtraInfo = 0;
|
|
||||||
SendInput(1, &keyInput, sizeof(keyInput));
|
|
||||||
}
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
void toggleKeyCode(MMKeyCode code, const bool down, MMKeyFlags flags, uintptr pid) {
|
void toggleKeyCode(MMKeyCode code, const bool down, MMKeyFlags flags, uintptr pid) {
|
||||||
@ -269,57 +268,27 @@ void toggleKey(char c, const bool down, MMKeyFlags flags, uintptr pid) {
|
|||||||
// }
|
// }
|
||||||
|
|
||||||
#if defined(IS_MACOSX)
|
#if defined(IS_MACOSX)
|
||||||
void toggleUnicode(UniChar ch, const bool down, uintptr 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().
|
||||||
-- if you need modifier keys, use the above functions instead. */
|
-- if you need modifier keys, use the above functions instead. */
|
||||||
CGEventRef keyEvent = CGEventCreateKeyboardEvent(NULL, 0, down);
|
CGEventRef keyEvent = CGEventCreateKeyboardEvent(NULL, 0, down);
|
||||||
if (keyEvent == NULL) {
|
if (keyEvent == NULL) {
|
||||||
fputs("Could not create keyboard event.\n", stderr);
|
fputs("Could not create keyboard event.\n", stderr);
|
||||||
return;
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
CGEventKeyboardSetUnicodeString(keyEvent, 1, &ch);
|
||||||
|
|
||||||
|
SendTo(pid, keyEvent);
|
||||||
}
|
}
|
||||||
|
#else
|
||||||
CGEventKeyboardSetUnicodeString(keyEvent, 1, &ch);
|
|
||||||
|
|
||||||
SendTo(pid, keyEvent);
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(USE_X11)
|
|
||||||
#define toggleUniKey(c, down) toggleKey(c, down, MOD_NONE, 0)
|
#define toggleUniKey(c, down) toggleKey(c, down, MOD_NONE, 0)
|
||||||
|
|
||||||
int input_utf(const char *utf) {
|
|
||||||
Display *dpy = XOpenDisplay(NULL);
|
|
||||||
KeySym sym = XStringToKeysym(utf);
|
|
||||||
// KeySym sym = XKeycodeToKeysym(dpy, utf);
|
|
||||||
|
|
||||||
int min, max, numcodes;
|
|
||||||
XDisplayKeycodes(dpy, &min, &max);
|
|
||||||
KeySym *keysym;
|
|
||||||
keysym = XGetKeyboardMapping(dpy, min, max-min+1, &numcodes);
|
|
||||||
keysym[(max-min-1)*numcodes] = sym;
|
|
||||||
XChangeKeyboardMapping(dpy, min, numcodes, keysym, (max-min));
|
|
||||||
XFree(keysym);
|
|
||||||
XFlush(dpy);
|
|
||||||
|
|
||||||
KeyCode code = XKeysymToKeycode(dpy, sym);
|
|
||||||
XTestFakeKeyEvent(dpy, code, True, 1);
|
|
||||||
XTestFakeKeyEvent(dpy, code, False, 1);
|
|
||||||
|
|
||||||
XFlush(dpy);
|
|
||||||
XCloseDisplay(dpy);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
#if !defined(USE_X11)
|
|
||||||
int input_utf(const char *utf){
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// unicode type
|
// unicode type
|
||||||
void unicodeType(const unsigned value, uintptr pid, int8_t isPid){
|
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
|
||||||
|
|
||||||
@ -354,4 +323,33 @@ void unicodeType(const unsigned value, uintptr pid, int8_t isPid){
|
|||||||
microsleep(5.0);
|
microsleep(5.0);
|
||||||
toggleUniKey(value, false);
|
toggleUniKey(value, false);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if defined(USE_X11)
|
||||||
|
int input_utf(const char *utf) {
|
||||||
|
Display *dpy = XOpenDisplay(NULL);
|
||||||
|
KeySym sym = XStringToKeysym(utf);
|
||||||
|
// KeySym sym = XKeycodeToKeysym(dpy, utf);
|
||||||
|
|
||||||
|
int min, max, numcodes;
|
||||||
|
XDisplayKeycodes(dpy, &min, &max);
|
||||||
|
KeySym *keysym;
|
||||||
|
keysym = XGetKeyboardMapping(dpy, min, max-min+1, &numcodes);
|
||||||
|
keysym[(max-min-1)*numcodes] = sym;
|
||||||
|
XChangeKeyboardMapping(dpy, min, numcodes, keysym, (max-min));
|
||||||
|
XFree(keysym);
|
||||||
|
XFlush(dpy);
|
||||||
|
|
||||||
|
KeyCode code = XKeysymToKeycode(dpy, sym);
|
||||||
|
XTestFakeKeyEvent(dpy, code, True, 1);
|
||||||
|
XTestFakeKeyEvent(dpy, code, False, 1);
|
||||||
|
|
||||||
|
XFlush(dpy);
|
||||||
|
XCloseDisplay(dpy);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
int input_utf(const char *utf){
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
#endif
|
@ -15,69 +15,69 @@
|
|||||||
|
|
||||||
/* Some convenience macros for converting our enums to the system API types. */
|
/* Some convenience macros for converting our enums to the system API types. */
|
||||||
#if defined(IS_MACOSX)
|
#if defined(IS_MACOSX)
|
||||||
CGEventType MMMouseDownToCGEventType(MMMouseButton button) {
|
CGEventType MMMouseDownToCGEventType(MMMouseButton button) {
|
||||||
if (button == LEFT_BUTTON) {
|
if (button == LEFT_BUTTON) {
|
||||||
return kCGEventLeftMouseDown;
|
return kCGEventLeftMouseDown;
|
||||||
|
}
|
||||||
|
if (button == RIGHT_BUTTON) {
|
||||||
|
return kCGEventRightMouseDown;
|
||||||
|
}
|
||||||
|
return kCGEventOtherMouseDown;
|
||||||
}
|
}
|
||||||
if (button == RIGHT_BUTTON) {
|
|
||||||
return kCGEventRightMouseDown;
|
|
||||||
}
|
|
||||||
return kCGEventOtherMouseDown;
|
|
||||||
}
|
|
||||||
|
|
||||||
CGEventType MMMouseUpToCGEventType(MMMouseButton button) {
|
CGEventType MMMouseUpToCGEventType(MMMouseButton button) {
|
||||||
if (button == LEFT_BUTTON) { return kCGEventLeftMouseUp; }
|
if (button == LEFT_BUTTON) { return kCGEventLeftMouseUp; }
|
||||||
if (button == RIGHT_BUTTON) { return kCGEventRightMouseUp; }
|
if (button == RIGHT_BUTTON) { return kCGEventRightMouseUp; }
|
||||||
return kCGEventOtherMouseUp;
|
return kCGEventOtherMouseUp;
|
||||||
}
|
}
|
||||||
|
|
||||||
CGEventType MMMouseDragToCGEventType(MMMouseButton button) {
|
CGEventType MMMouseDragToCGEventType(MMMouseButton button) {
|
||||||
if (button == LEFT_BUTTON) { return kCGEventLeftMouseDragged; }
|
if (button == LEFT_BUTTON) { return kCGEventLeftMouseDragged; }
|
||||||
if (button == RIGHT_BUTTON) { return kCGEventRightMouseDragged; }
|
if (button == RIGHT_BUTTON) { return kCGEventRightMouseDragged; }
|
||||||
return kCGEventOtherMouseDragged;
|
return kCGEventOtherMouseDragged;
|
||||||
}
|
}
|
||||||
|
|
||||||
CGEventType MMMouseToCGEventType(bool down, MMMouseButton button) {
|
CGEventType MMMouseToCGEventType(bool down, MMMouseButton button) {
|
||||||
if (down) { return MMMouseDownToCGEventType(button); }
|
if (down) { return MMMouseDownToCGEventType(button); }
|
||||||
return MMMouseUpToCGEventType(button);
|
return MMMouseUpToCGEventType(button);
|
||||||
}
|
}
|
||||||
|
|
||||||
#elif defined(IS_WINDOWS)
|
#elif defined(IS_WINDOWS)
|
||||||
|
|
||||||
DWORD MMMouseUpToMEventF(MMMouseButton button) {
|
DWORD MMMouseUpToMEventF(MMMouseButton button) {
|
||||||
if (button == LEFT_BUTTON) { return MOUSEEVENTF_LEFTUP; }
|
if (button == LEFT_BUTTON) { return MOUSEEVENTF_LEFTUP; }
|
||||||
if (button == RIGHT_BUTTON) { return MOUSEEVENTF_RIGHTUP; }
|
if (button == RIGHT_BUTTON) { return MOUSEEVENTF_RIGHTUP; }
|
||||||
return MOUSEEVENTF_MIDDLEUP;
|
return MOUSEEVENTF_MIDDLEUP;
|
||||||
}
|
}
|
||||||
|
|
||||||
DWORD MMMouseDownToMEventF(MMMouseButton button) {
|
DWORD MMMouseDownToMEventF(MMMouseButton button) {
|
||||||
if (button == LEFT_BUTTON) { return MOUSEEVENTF_LEFTDOWN; }
|
if (button == LEFT_BUTTON) { return MOUSEEVENTF_LEFTDOWN; }
|
||||||
if (button == RIGHT_BUTTON) { return MOUSEEVENTF_RIGHTDOWN; }
|
if (button == RIGHT_BUTTON) { return MOUSEEVENTF_RIGHTDOWN; }
|
||||||
return MOUSEEVENTF_MIDDLEDOWN;
|
return MOUSEEVENTF_MIDDLEDOWN;
|
||||||
}
|
}
|
||||||
|
|
||||||
DWORD MMMouseToMEventF(bool down, MMMouseButton button) {
|
DWORD MMMouseToMEventF(bool down, MMMouseButton button) {
|
||||||
if (down) { return MMMouseDownToMEventF(button); }
|
if (down) { return MMMouseDownToMEventF(button); }
|
||||||
return MMMouseUpToMEventF(button);
|
return MMMouseUpToMEventF(button);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if defined(IS_MACOSX)
|
#if defined(IS_MACOSX)
|
||||||
/* Calculate the delta for a mouse move and add them to the event. */
|
/* Calculate the delta for a mouse move and add them to the event. */
|
||||||
void calculateDeltas(CGEventRef *event, MMPointInt32 point){
|
void calculateDeltas(CGEventRef *event, MMPointInt32 point) {
|
||||||
/* The next few lines are a workaround for games not detecting mouse moves. */
|
/* The next few lines are a workaround for games not detecting mouse moves. */
|
||||||
CGEventRef get = CGEventCreate(NULL);
|
CGEventRef get = CGEventCreate(NULL);
|
||||||
CGPoint mouse = CGEventGetLocation(get);
|
CGPoint mouse = CGEventGetLocation(get);
|
||||||
|
|
||||||
// Calculate the deltas.
|
// Calculate the deltas.
|
||||||
int64_t deltaX = point.x - mouse.x;
|
int64_t deltaX = point.x - mouse.x;
|
||||||
int64_t deltaY = point.y - mouse.y;
|
int64_t deltaY = point.y - mouse.y;
|
||||||
|
|
||||||
CGEventSetIntegerValueField(*event, kCGMouseEventDeltaX, deltaX);
|
CGEventSetIntegerValueField(*event, kCGMouseEventDeltaX, deltaX);
|
||||||
CGEventSetIntegerValueField(*event, kCGMouseEventDeltaY, deltaY);
|
CGEventSetIntegerValueField(*event, kCGMouseEventDeltaY, deltaY);
|
||||||
|
|
||||||
CFRelease(get);
|
CFRelease(get);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* Move the mouse to a specific point. */
|
/* Move the mouse to a specific point. */
|
||||||
|
@ -54,7 +54,8 @@ MMBitmapRef copyMMBitmapFromDisplayInRect(MMRectInt32 rect, int32_t display_id,
|
|||||||
|
|
||||||
MMPointInt32 o = rect.origin; MMSizeInt32 s = rect.size;
|
MMPointInt32 o = rect.origin; MMSizeInt32 s = rect.size;
|
||||||
XImage *image = XGetImage(display, XDefaultRootWindow(display),
|
XImage *image = XGetImage(display, XDefaultRootWindow(display),
|
||||||
(int)o.x, (int)o.y, (unsigned int)s.w, (unsigned int)s.h, AllPlanes, ZPixmap);
|
(int)o.x, (int)o.y, (unsigned int)s.w, (unsigned int)s.h,
|
||||||
|
AllPlanes, ZPixmap);
|
||||||
XCloseDisplay(display);
|
XCloseDisplay(display);
|
||||||
if (image == NULL) { return NULL; }
|
if (image == NULL) { return NULL; }
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user