Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
763 views
in Technique[技术] by (71.8m points)

macos - Simulate keypress for system wide hotkeys

I need to simulate keystrokes in OSX. Here's how I do it:

-(void)execute {
    CGEventSourceRef sourceRef =
    CGEventSourceCreate(kCGEventSourceStateHIDSystemState);

    CGEventRef keyPress = CGEventCreateKeyboardEvent (sourceRef, (CGKeyCode)keyCode, true);
    CGEventRef keyUnpress = CGEventCreateKeyboardEvent (sourceRef, (CGKeyCode)keyCode, false);

    CGEventSetFlags(keyPress, modifierFlags);
    CGEventPost(kCGHIDEventTap, keyPress);

    //unpressing the acualkey
    CGEventPost(kCGHIDEventTap, keyUnpress);

    CFRelease(keyPress);
    CFRelease(keyUnpress);
    CFRelease(sourceRef);
}

It works fine for every hotkey or simple keystrokes in any app, but doesn't work for system wide shortcuts, for example option + space to launch Spotlight or cmd + shift + 4 to make a screenshot or ctrl + ` to open iTerm2 window.

I tried to change event's source and the location at which to post event, doesn't help. Any ideas?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

From the documentation for CGEventCreateKeyboardEvent:

All keystrokes needed to generate a character must be entered, including modifier keys. For example, to produce a 'Z', the SHIFT key must be down, the 'z' key must go down, and then the SHIFT and 'z' key must be released:

So, you can't just press and release space with the option modifier to trigger an option-space; you have to press option, press space, release space, release option.

As a side note, opt-space doesn't do anything by default; cmd-space is the Spotlight search hotkey, and cmd-opt-space is the Spotlight window hotkey.

So, this code will pop up the Spotlight search:

- (void)execute {
  CGEventSourceRef src = 
    CGEventSourceCreate(kCGEventSourceStateHIDSystemState);

  CGEventRef cmdd = CGEventCreateKeyboardEvent(src, 0x38, true);
  CGEventRef cmdu = CGEventCreateKeyboardEvent(src, 0x38, false);
  CGEventRef spcd = CGEventCreateKeyboardEvent(src, 0x31, true);
  CGEventRef spcu = CGEventCreateKeyboardEvent(src, 0x31, false);

  CGEventSetFlags(spcd, kCGEventFlagMaskCommand);
  CGEventSetFlags(spcu, kCGEventFlagMaskCommand);

  CGEventTapLocation loc = kCGHIDEventTap; // kCGSessionEventTap also works
  CGEventPost(loc, cmdd);
  CGEventPost(loc, spcd);
  CGEventPost(loc, spcu);
  CGEventPost(loc, cmdu);

  CFRelease(cmdd);
  CFRelease(cmdu);
  CFRelease(spcd);
  CFRelease(spcu);
  CFRelease(src);  
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...