模拟系统范围热键的按键 [英] Simulate keypress for system wide hotkeys

查看:178
本文介绍了模拟系统范围热键的按键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在OSX中模拟按键.这是我的方法:

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);
}

对于任何应用程序中的每个热键或简单击键,它都可以正常工作,但不适用于系统范围内的快捷键,例如,用于启动Spotlight的 option + space cmd + shift + 4 创建屏幕快照,或 ctrl +`打开iTerm2窗口.

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?

推荐答案

来自CGEventCreateKeyboardEvent的文档:

From the documentation for CGEventCreateKeyboardEvent:

必须输入生成字符所需的所有击键,包括修饰键.例如,要生成"Z",必须按下SHIFT键,然后按下"z"键,然后释放SHIFT和"z"键:

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.

请注意,默认情况下opt-space不会执行任何操作; cmd-space是Spotlight搜索热键,cmd-opt-space是Spotlight窗口热键.

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.

因此,此代码将弹出Spotlight搜索:

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);  
}

这篇关于模拟系统范围热键的按键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆