Generate keyboard events for the frontmost application

后端 未结 1 1549
醉话见心
醉话见心 2020-12-09 21:40

In Mac OS / Cocoa, may I synthesize keyboard entries - strings - for the frontmost application in a transparent way?

To be more precise, I don\'t wa

相关标签:
1条回答
  • 2020-12-09 22:22

    Sure, you'll want to use CGEventCreateKeyboardEvent to create keyboard events, then post them as such:

        CGEventRef keyup, keydown;
        keydown = CGEventCreateKeyboardEvent (NULL, (CGKeyCode)56, true);
        keyup = CGEventCreateKeyboardEvent (NULL, (CGKeyCode)56, false);
    
        CGEventPost(kCGHIDEventTap, keydown);
        CGEventPost(kCGHIDEventTap, keyup);
        CFRelease(keydown);
        CFRelease(keyup);
    

    It's a bit more complicated than AppleScript but it does the trick. You do have to explicitly post a keydown and then a keyup event. More information at the Quartz Event Services Reference.

    0 讨论(0)
提交回复
热议问题