Simulate keypress for system wide hotkeys

后端 未结 4 381
旧巷少年郎
旧巷少年郎 2020-12-02 19:31

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

-(void)execute {
    CGEventSourceRef sourceRef =
    CGEventSourceCreate(kCGEventSourceStateHIDSy         


        
4条回答
  •  自闭症患者
    2020-12-02 20:09

    For the record, while @abarnert's answer is great and that's the way you should do it according to documentation, my original code also works. I found out that I had a different problem not relevant to this question.

    So, if you need to apply modifier keys to the keypress, you can simply add them like this CGEventSetFlags(keyPress, modifierFlags); without pressing an depressing every modifier key separately. That approach works, and I haven't found any drawbacks yet and readability of the code is way better.

提交回复
热议问题