Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8012571
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T19:21:57+00:00 2026-06-04T19:21:57+00:00

I need to simulate keystrokes in OSX. Here’s how I do it: -(void)execute {

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T19:21:58+00:00Added an answer on June 4, 2026 at 7:21 pm

    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);  
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on a Java project and need to have a keypress simulate
I need to simulate a keypress in game window. I try to send key
Here's what I need to do: I need to simulate a server, which sends
I need to simulate a test scenario in which I call the getBytes() method
I need to simulate multiple embedded server devices that are typically used for motor
I need to simulate how my application will look when a user is driving
I need to simulate a memory-hungry process. For example, On a machine with 4.0
I'm writing an app where I need to simulate key press events on a
I'm looking for a jquery plugin to simulate a vertical marquee. I need it
I have a class derived from Dictionary. I need this class to simulate a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.