objective-c - 使用 CoreGraphics 方法发送自动重复键 (Mac OS X Snow Leopard)

标签 objective-c cocoa macos core-graphics macos-carbon

我已成功发送击键,以便自动执行我使用的用于绘图的特定软件包。这个软件依赖于很多键盘快捷键,所以我写了一些可以调用其中一些键盘快捷键的东西,以简化我的工作流程。正如我所说,结果很好。

我的库是一个作为插件加载到软件包中的 Cocoa 库。这是我用来发送击键的代码片段。

CGEventSourceRef eventSource = CGEventSourceCreate(kCGEventSourceStateHIDSystemState);
CGEventRef eventDown = CGEventCreateKeyboardEvent(eventSource, (CGKeyCode)1, true);
CGEventRef eventUp = CGEventCreateKeyboardEvent(eventSource, (CGKeyCode)1, false);

//setting the process number somewhere else
CGEventPostToPSN(&number, eventDown);
CGEventPostToPSN(&number, eventUp);

对于绘图包中的某些程序,如果您继续按住 Shift 键,则会激活一个特殊工具。我一直无法模拟这个。我以为我可以发送 Shift 键并说我希望它自动重复,但这似乎不起作用。我一直在使用以下代码来设置自动重复:

//This is done before sending the key
CGEventSetIntegerValueField(eventDown, kCGKeyboardEventAutorepeat, 1);

在我的测试中,我无法使任何键自动重复。它只发送一次 key ,仅此而已。

有没有人使用上述方法成功自动重复 key ?我在 Internet 上搜索了答案,但我发现的都是 2008 年以来的一些 Unresolved 问题...非常感谢任何帮助。

谢谢,

手机

最佳答案

OP最终想出来的解决问题的代码(转自其他回答下的评论):

CGEventRef flagsChanged = CGEventCreate(eventSource); 
CGEventSetType(flagsChanged, kCGEventFlagsChanged); 
CGEventSetIntegerValueField(flagsChanged, kCGKeyboardEventKeycode, 56);     
CGEventSetFlags(flagsChanged, 131330); 
CGEventPostToPSN(&number, flagsChanged); 
CFRelease(flagsChanged); CFRelease(eventSource);

131330是表示Shift键的常量;它与 NSShiftKeyMaskkCGEventFlagMaskShift 有关,它们是 131072 (0x00020000)。 131330 - 256 - 2 == 131072

关于objective-c - 使用 CoreGraphics 方法发送自动重复键 (Mac OS X Snow Leopard),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5718587/

相关文章:

ios - 找不到 GMSMarker 的协议(protocol)声明

c++ - 将 C 代码注入(inject) *nix 应用程序 : replacing a function

objective-c - 如何将这个 Objective-C block 存储在 Swift 变量中?

python - osx Nose 测试不加载模块egg

node.js - Electron import x509 cert to local keychain (macOS) - 授权被拒绝,因为无法进行用户交互

ios - 从 App-delegate 重新加载或运行函数

objective-c - 如何以编程方式向 OS X 应用程序添加红绿灯按钮

iphone - 接近传感器禁用我的屏幕

cocoa - 如何将菜单项添加到 NSTextField 的上下文菜单中

macos - 单击 osx 10.6 选择 View