//-------------------------------------------------------------- void testApp::update(){ openNIDevice.update(); int holdTime = 300; currTime = ofGetElapsedTimeMillis(); if((currTime - keyTime) > holdTime && leftKeyFlag){ cout << "RELEASE LEFT KEY" << endl; CGEventRef b = CGEventCreateKeyboardEvent(NULL, 123, 0); CGEventPost(kCGHIDEventTap, b); leftKeyFlag = false; } if((currTime - keyTime) > holdTime && rightKeyFlag){ cout << "RELEASE RIGHT KEY" << endl; CGEventRef f = CGEventCreateKeyboardEvent(NULL, 124, 0); CGEventPost(kCGHIDEventTap, f); rightKeyFlag = false; } if((currTime - keyTime) > holdTime && upKeyFlag){ cout << "RELEASE UP KEY" << endl; CGEventRef d = CGEventCreateKeyboardEvent(NULL, 126, 0); CGEventPost(kCGHIDEventTap, d); upKeyFlag = false; } if (upKeyFlag == 1) { leftByRun = 1; rightByRun = 1; } else { leftByRun = 0; rightByRun = 0;} }
void KwmEmitKeystroke(modifiers Mod, std::string Key) { CGEventFlags Flags = 0; if(Mod.CmdKey) Flags |= kCGEventFlagMaskCommand; if(Mod.CtrlKey) Flags |= kCGEventFlagMaskControl; if(Mod.AltKey) Flags |= kCGEventFlagMaskAlternate; if(Mod.ShiftKey) Flags |= kCGEventFlagMaskShift; CGKeyCode Keycode; bool Result = GetLayoutIndependentKeycode(Key, &Keycode); if(!Result) Result = KeycodeForChar(Key[0], &Keycode); if(Result) { CGEventRef EventKeyDown = CGEventCreateKeyboardEvent(NULL, Keycode, true); CGEventRef EventKeyUp = CGEventCreateKeyboardEvent(NULL, Keycode, false); CGEventSetFlags(EventKeyDown, Flags); CGEventSetFlags(EventKeyUp, Flags); CGEventPost(kCGHIDEventTap, EventKeyDown); CGEventPost(kCGHIDEventTap, EventKeyUp); CFRelease(EventKeyDown); CFRelease(EventKeyUp); } }
void UBKeyboardButton::sendControlSymbol(int nSymbol) { CGEventRef event1 = CGEventCreateKeyboardEvent(NULL, nSymbol, true); CGEventRef event2 = CGEventCreateKeyboardEvent(NULL, nSymbol, false); CGEventPost(kCGHIDEventTap, event1); CGEventPost(kCGHIDEventTap, event2); }
void SendString(CFStringRef str, unsigned delayMS) { //virtual keycodes copied from 10.6 SDK. Could not find them in 10.4 SDK enum { VK_RETURN = 0x24 /*KVK_Return*/, VK_TAB = 0x30 /*kVK_Tab*/, VK_SPACE = 0x31/*kVK_Space*/}; //A list of chars for which we must specify the virtual keycode static const CFStringRef specialChars = CFSTR("\n\t "); static const UniChar verticalTab = CFStringGetCharacterAtIndex(CFSTR("\v"), 0); //each keycode must correspond to the correct char in 'specialChars' CGKeyCode specialKeyCodes[] = {VK_RETURN, VK_TAB, VK_SPACE }; assert(CFStringGetLength(specialChars) == NumberOf(specialKeyCodes)); for (unsigned i = 0, len = CFStringGetLength(str); i < len; ++i) { //The next char to send UniChar c = CFStringGetCharacterAtIndex(str, i); //throw away 'vertical tab' chars which are only used on Windows to send a shift+tab //as a workaround for some issues with IE if (verticalTab == c) continue; //see if we need to specify the virtual keycode for this char CGKeyCode vKey = 0; //0 = kVK_ANSI_A, but I don't know of a more appropriate default value for (size_t j = 0; j < NumberOf(specialKeyCodes); ++j) { if ( CFStringGetCharacterAtIndex(specialChars, j) == c) { vKey = specialKeyCodes[j]; break; } } CGEventRef keyDown = CGEventCreateKeyboardEvent(NULL, vKey, true); CGEventRef keyUp = CGEventCreateKeyboardEvent(NULL, vKey, false); if (keyDown && keyUp) { //may be we should not do this if we found the virtual keycode? CGEventKeyboardSetUnicodeString(keyDown, 1, &c); CGEventKeyboardSetUnicodeString(keyUp, 1, &c); CGEventPost(kCGSessionEventTap, keyDown); CGEventPost(kCGSessionEventTap, keyUp); pws_os::sleep_ms(delayMS); CFRelease(keyDown); CFRelease(keyUp); } else { if (keyDown) CFRelease(keyDown); if (keyUp) CFRelease(keyUp); pws_os::IssueError(_T("Out of memory trying to allocate CGEventRef")); return; } } }
int main() { CGEventRef mkey = CGEventCreateKeyboardEvent(NULL, (CGKeyCode)7, true); CGEventPost(kCGSessionEventTap, mkey); CFRelease(mkey); CGEventRef fkey = CGEventCreateKeyboardEvent(NULL, (CGKeyCode)7, false); CGEventPost(kCGSessionEventTap, fkey); CFRelease(fkey); return 0; }
void UBKeyboardButton::sendUnicodeSymbol(KEYCODE keycode) { if (keycode.modifier) CGEventPost(kCGSessionEventTap, CGEventCreateKeyboardEvent(NULL, 56, true)); CGEventPost(kCGSessionEventTap, CGEventCreateKeyboardEvent(NULL, keycode.code, true)); CGEventPost(kCGSessionEventTap, CGEventCreateKeyboardEvent(NULL, keycode.code, false)); if (keycode.modifier) CGEventPost(kCGSessionEventTap, CGEventCreateKeyboardEvent(NULL, 56, false)); }
void sendPress(int kc) { CGEventSourceRef src = NULL; // CGEventSourceCreate(kCGEventSourceStateHIDSystemState); CGEventRef down, up; down = CGEventCreateKeyboardEvent(src,(CGKeyCode)kc, true); up = CGEventCreateKeyboardEvent(src,(CGKeyCode)kc, false); CGEventPost(kCGSessionEventTap, down); CGEventPost(kCGSessionEventTap, up); CFRelease(down); CFRelease(up); }
void Utilities::PostKey(CGKeyCode key) { CGEventSourceRef source = CGEventSourceCreate(kCGEventSourceStateCombinedSessionState); CGEventRef keyDown = CGEventCreateKeyboardEvent(source, key, TRUE); CGEventRef keyUp = CGEventCreateKeyboardEvent(source, key, FALSE); CGEventPost(kCGAnnotatedSessionEventTap, keyDown); CGEventPost(kCGAnnotatedSessionEventTap, keyUp); CFRelease(keyUp); CFRelease(keyDown); CFRelease(source); }
void toggleKeyCode(MMKeyCode code, const bool down, MMKeyFlags flags) { #if defined(IS_MACOSX) CGEventRef keyEvent = CGEventCreateKeyboardEvent(NULL, (CGKeyCode)code, down); assert(keyEvent != NULL); CGEventSetType(keyEvent, down ? kCGEventKeyDown : kCGEventKeyUp); CGEventSetFlags(keyEvent, flags); CGEventPost(kCGSessionEventTap, keyEvent); CFRelease(keyEvent); #elif defined(IS_WINDOWS) const DWORD dwFlags = down ? 0 : KEYEVENTF_KEYUP; /* Parse modifier keys. */ if (flags & MOD_META) WIN32_KEY_EVENT_WAIT(K_META, dwFlags); if (flags & MOD_ALT) WIN32_KEY_EVENT_WAIT(K_ALT, dwFlags); if (flags & MOD_CONTROL) WIN32_KEY_EVENT_WAIT(K_CONTROL, dwFlags); if (flags & MOD_SHIFT) WIN32_KEY_EVENT_WAIT(K_SHIFT, dwFlags); win32KeyEvent(code, dwFlags); #elif defined(USE_X11) Display *display = XGetMainDisplay(); const Bool is_press = down ? True : False; /* Just to be safe. */ /* Parse modifier keys. */ if (flags & MOD_META) X_KEY_EVENT_WAIT(display, K_META, is_press); if (flags & MOD_ALT) X_KEY_EVENT_WAIT(display, K_ALT, is_press); if (flags & MOD_CONTROL) X_KEY_EVENT_WAIT(display, K_CONTROL, is_press); if (flags & MOD_SHIFT) X_KEY_EVENT_WAIT(display, K_SHIFT, is_press); X_KEY_EVENT(display, code, is_press); #endif }
void convertAndSendVK(PRUint8 vk, bool down) { CGKeyCode key = 0; // Hack usleep(100); switch (vk) { case SPACE: key = kVK_Space; break; case BKSP: key = kVK_Delete; break; case TAB: key = kVK_Tab; break; case ENTER: key = kVK_Return; break; case SHIFT: key = kVK_Shift; break; default: return; } CGEventRef event; CGEventSourceRef eventSource = CGEventSourceCreate(kCGEventSourceStateHIDSystemState); event = CGEventCreateKeyboardEvent (eventSource, key, down); CGEventPost(kCGHIDEventTap, event); CFRelease(eventSource); CFRelease(event); }
bool pws_os::MacSimulateApplicationSwitch(unsigned delayMS) { enum { VK_CMD = 55, VK_TAB = 48 }; struct { CGKeyCode virtualKey; bool down; bool mask; } KeySequence[]={ {VK_CMD, true, true}, {VK_TAB, true, true}, {VK_TAB, false, true}, {VK_CMD, false, false} }; for (size_t idx = 0; idx < NumberOf(KeySequence); ++idx) { CGEventRef keystroke = CGEventCreateKeyboardEvent(NULL, KeySequence[idx].virtualKey, KeySequence[idx].down); if (keystroke) { if (KeySequence[idx].mask) { CGEventSetFlags(keystroke, kCGEventFlagMaskCommand); } CGEventPost(kCGSessionEventTap, keystroke); CFRelease(keystroke); pws_os::sleep_ms(delayMS); } else { return false; } } return true; }
void keyretrigger(usbdevice* kb, int scancode){ CGEventRef kp = CGEventCreateKeyboardEvent(kb->event, scancode, 1); CGEventSetIntegerValueField(kp, kCGKeyboardEventAutorepeat, 1); CGEventSetFlags(kp, kb->eflags); CGEventPost(kCGHIDEventTap, kp); CFRelease(kp); }
void XInputSimulatorImplMacOs::keyUp(int key) { CGKeyCode keycode = key; CGEventRef commandUp = CGEventCreateKeyboardEvent(NULL, keycode, false); CGEventPost(kCGAnnotatedSessionEventTap, commandUp); CFRelease(commandUp); }
int main() { int keyCode = 49; //space bar srand ( time(NULL) ); printf("User input coming in 30 seconds\n"); sleep(27); printf("3...\n"); sleep(1); printf("2...\n"); sleep(1); printf("1...\n"); sleep(1); printf("NOW!"); while(1){ CGEventRef ourEvent = CGEventCreate(NULL); CGPoint point = CGEventGetLocation(ourEvent); CGEventRef click1_down = CGEventCreateMouseEvent( NULL, kCGEventLeftMouseDown, point, kCGMouseButtonLeft ); CGEventRef click1_up = CGEventCreateMouseEvent( NULL, kCGEventLeftMouseUp, point, kCGMouseButtonLeft ); CGEventPost(kCGHIDEventTap, click1_down); sleep(0.11); CGEventPost(kCGHIDEventTap, click1_up); CFRelease(click1_up); CFRelease(click1_down); CFRelease(ourEvent); sleep(0.89); if(rand() % 100 <= 2){ //2% chance CGEventRef e = CGEventCreateKeyboardEvent (NULL, (CGKeyCode)keyCode, true); CGEventPost(kCGSessionEventTap, e); CFRelease(e); e = CGEventCreateKeyboardEvent (NULL, (CGKeyCode)keyCode, false); CGEventPost(kCGSessionEventTap, e); CFRelease(e); } sleep(9); } }
Qt::Native::Status sendNativeKeyEvent_Quartz(const QNativeKeyEvent &event) { CGEventRef e = CGEventCreateKeyboardEvent(0, (uint)event.nativeKeyCode, event.press); setModifiersFromQNativeEvent(e, event); CGEventPost(kCGHIDEventTap, e); CFRelease(e); return Qt::Native::Success; }
//-------------------------------------------------------------- void testApp::holdLeftKey(){ if(!leftKeyFlag){ cout << "PRESS LEFT KEY" << endl; keyTime = ofGetElapsedTimeMillis(); CGEventRef a = CGEventCreateKeyboardEvent(NULL, 123, 1); //PRESS LEFT KEY CGEventPost(kCGHIDEventTap, a); leftKeyFlag = true; } }
//-------------------------------------------------------------- void testApp::holdUpKey(){ if(!upKeyFlag){ cout << "PRESS UP KEY" << endl; keyTime = ofGetElapsedTimeMillis(); CGEventRef c = CGEventCreateKeyboardEvent(NULL, 126, 1); //PRESS UP KEY CGEventPost(kCGHIDEventTap, c); upKeyFlag = true; } }
//-------------------------------------------------------------- void testApp::holdRightKey(){ if(!rightKeyFlag){ cout << "PRESS RIGHT KEY" << endl; keyTime = ofGetElapsedTimeMillis(); CGEventRef e = CGEventCreateKeyboardEvent(NULL, 124, 1); //PRESS UP KEY CGEventPost(kCGHIDEventTap, e); rightKeyFlag = true; } }
static inline void SendString(short theCode) { UniChar unicodeString[1]; unicodeString[0] = theCode; CGEventRef e = CGEventCreateKeyboardEvent(NULL, 0, true); CGEventKeyboardSetUnicodeString(e, 1, unicodeString); CGEventPost(kCGHIDEventTap, e); CFRelease(e); }
void SendCharCode(CGCharCode keycode, bool isDown) { CGEventRef event = CGEventCreateKeyboardEvent(NULL, keycode, isDown); if (event) { CGEventPost(kCGHIDEventTap, event); } CFRelease(event); }
void MacOSKeyboardOStream::sendKeyCode(uint16_t key) { #if __APPLE__ if (ofGetElapsedTimeMillis() < elapsed_time_ + kGracePeriod) { return; } elapsed_time_ = ofGetElapsedTimeMillis(); // Get the process number for the front application. ProcessSerialNumber psn = { 0, kNoProcess }; GetFrontProcess( &psn ); CGEventRef key_down = CGEventCreateKeyboardEvent(NULL, key, true); CGEventRef key_up = CGEventCreateKeyboardEvent(NULL, key, false); CGEventPostToPSN(&psn, key_down); CGEventPostToPSN(&psn, key_up); CFRelease(key_down); CFRelease(key_up); #endif }
void mac_shadow_input_keyboard_event(macShadowSubsystem* subsystem, rdpShadowClient* client, UINT16 flags, UINT16 code) { DWORD vkcode; DWORD keycode; BOOL extended; CGEventRef kbdEvent; CGEventSourceRef source; extended = (flags & KBD_FLAGS_EXTENDED) ? TRUE : FALSE; if (extended) code |= KBDEXT; vkcode = GetVirtualKeyCodeFromVirtualScanCode(code, 4); if (extended) vkcode |= KBDEXT; keycode = GetKeycodeFromVirtualKeyCode(vkcode, KEYCODE_TYPE_APPLE); if (keycode < 8) return; keycode -= 8; source = CGEventSourceCreate(kCGEventSourceStateHIDSystemState); if (flags & KBD_FLAGS_DOWN) { kbdEvent = CGEventCreateKeyboardEvent(source, (CGKeyCode) keycode, TRUE); CGEventPost(kCGHIDEventTap, kbdEvent); CFRelease(kbdEvent); } else if (flags & KBD_FLAGS_RELEASE) { kbdEvent = CGEventCreateKeyboardEvent(source, (CGKeyCode) keycode, FALSE); CGEventPost(kCGHIDEventTap, kbdEvent); CFRelease(kbdEvent); } CFRelease(source); }
Qt::Native::Status sendNativeKeyEventToProcess_Quartz(const QNativeKeyEvent &event, int pid) { ProcessSerialNumber psn; GetProcessForPID(pid, &psn); CGEventRef e = CGEventCreateKeyboardEvent(0, (uint)event.nativeKeyCode, event.press); setModifiersFromQNativeEvent(e, event); SetFrontProcess(&psn); CGEventPostToPSN(&psn, e); CFRelease(e); return Qt::Native::Success; }
void GTKeyboardDriver::keyPress(GUITestOpStatus &os, int key, int modifiers) { if (key==GTKeyboardDriver::key["ctrl"]) key=GTKeyboardDriver::key["cmd"]; if (modifiers) { CGEventRef event = CGEventCreateKeyboardEvent(NULL, modifiers, true); GT_CHECK(event != NULL, "Can't create event"); CGEventPost(kCGSessionEventTap, event); CFRelease(event); GTGlobals::sleep(1); } CGEventRef event = CGEventCreateKeyboardEvent(NULL, key, true); GT_CHECK(event != NULL, "Can't create event"); CGEventPost(kCGSessionEventTap, event); CFRelease(event); GTGlobals::sleep(1); }
void os_keypress(usbdevice* kb, int scancode, int down){ // Check for modifier keys and update flags int flags = 0; if(scancode == KEY_CAPSLOCK){ if(down){ if(!(kb->eflags & kCGEventFlagMaskAlphaShift)) kb->eflags |= kCGEventFlagMaskAlphaShift; else kb->eflags &= ~kCGEventFlagMaskAlphaShift; } flags = 1; } else if(scancode == KEY_LEFTSHIFT || scancode == KEY_RIGHTSHIFT){ if(down) kb->eflags |= kCGEventFlagMaskShift; else kb->eflags &= ~kCGEventFlagMaskShift; flags = 1; } else if(scancode == KEY_LEFTCTRL || scancode == KEY_RIGHTCTRL){ if(down) kb->eflags |= kCGEventFlagMaskControl; else kb->eflags &= ~kCGEventFlagMaskControl; flags = 1; } else if(scancode == KEY_LEFTMETA || scancode == KEY_RIGHTMETA){ if(down) kb->eflags |= kCGEventFlagMaskCommand; else kb->eflags &= ~kCGEventFlagMaskCommand; flags = 1; } else if(scancode == KEY_LEFTALT || scancode == KEY_RIGHTALT){ if(down) kb->eflags |= kCGEventFlagMaskAlternate; else kb->eflags &= ~kCGEventFlagMaskAlternate; flags = 1; } CGEventRef kp; if(flags){ kp = CGEventCreate(kb->event); CGEventSetType(kp, kCGEventFlagsChanged); CGEventSetIntegerValueField(kp, kCGKeyboardEventKeycode, scancode); kb->lastkeypress = -1; } else { kp = CGEventCreateKeyboardEvent(kb->event, scancode, down); kb->lastkeypress = (down ? scancode : -1); } kb->keypresstime = 0; CGEventSetFlags(kp, kb->eflags); CGEventPost(kCGHIDEventTap, kp); CFRelease(kp); }
bool wxUIActionSimulator::DoKey(int keycode, int WXUNUSED(modifiers), bool isDown) { CGKeyCode cgcode = wxCharCodeWXToOSX((wxKeyCode)keycode); wxCFRef<CGEventRef> event(CGEventCreateKeyboardEvent(NULL, cgcode, isDown)); if ( !event ) return false; CGEventPost(kCGHIDEventTap, event); return true; }
void MacOSKeyboardOStream::sendKey(char c) { #if __APPLE__ if (ofGetElapsedTimeMillis() < elapsed_time_ + kGracePeriod) { return; } elapsed_time_ = ofGetElapsedTimeMillis(); // Get the process number for the front application. ProcessSerialNumber psn = { 0, kNoProcess }; GetFrontProcess( &psn ); UniChar uni_char = c; CGEventRef key_down = CGEventCreateKeyboardEvent(NULL, 0, true); CGEventRef key_up = CGEventCreateKeyboardEvent(NULL, 0, false); CGEventKeyboardSetUnicodeString(key_down, 1, &uni_char); CGEventKeyboardSetUnicodeString(key_up, 1, &uni_char); CGEventPostToPSN(&psn, key_down); CGEventPostToPSN(&psn, key_up); CFRelease(key_down); CFRelease(key_up); #endif }
/* void char_down (in char key); */ NS_IMETHODIMP SendKeys::Char_down(PRUnichar key) { #if _DEBUG printf("charDown: %c (%d)\n", key, key); #endif UniChar letter = key; CGEventRef event; CGEventSourceRef eventSource = CGEventSourceCreate(kCGEventSourceStateHIDSystemState); event = CGEventCreateKeyboardEvent (eventSource, 1, true); CGEventKeyboardSetUnicodeString(event, 1, &letter); CGEventPost(kCGHIDEventTap, event); CFRelease(event); CFRelease(eventSource); return NS_OK; }
void KwmEmitKeystrokes(std::string Text) { CFStringRef TextRef = CFStringCreateWithCString(NULL, Text.c_str(), kCFStringEncodingMacRoman); CGEventRef EventKeyDown = CGEventCreateKeyboardEvent(NULL, 0, true); CGEventRef EventKeyUp = CGEventCreateKeyboardEvent(NULL, 0, false); UniChar OutputBuffer; for(std::size_t CharIndex = 0; CharIndex < Text.size(); ++CharIndex) { CFStringGetCharacters(TextRef, CFRangeMake(CharIndex, 1), &OutputBuffer); CGEventSetFlags(EventKeyDown, 0); CGEventKeyboardSetUnicodeString(EventKeyDown, 1, &OutputBuffer); CGEventPost(kCGHIDEventTap, EventKeyDown); CGEventSetFlags(EventKeyUp, 0); CGEventKeyboardSetUnicodeString(EventKeyUp, 1, &OutputBuffer); CGEventPost(kCGHIDEventTap, EventKeyUp); } CFRelease(EventKeyUp); CFRelease(EventKeyDown); CFRelease(TextRef); }
bool wxUIActionSimulator::DoKey(int keycode, int WXUNUSED(modifiers), bool isDown) { CGKeyCode cgcode = wxCharCodeWXToOSX((wxKeyCode)keycode); wxCFRef<CGEventRef> event(CGEventCreateKeyboardEvent(NULL, cgcode, isDown)); if ( !event ) return false; CGEventPost(kCGHIDEventTap, event); wxCFEventLoop* loop = dynamic_cast<wxCFEventLoop*>(wxEventLoop::GetActive()); if (loop) loop->SetShouldWaitForEvent(true); return true; }