macos 在 Mac OS X 中模拟按键事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2379867/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Simulating key press events in Mac OS X
提问by Thomi
I'm writing an app where I need to simulate key press events on a Mac, given a code that represents each key. It seems I need to use the CGEventCreateKeyboardEvent
function to create the event. The problem is that this function needs a Mac keycode, and what I have is a code that represents the specific key. So, for example, I receive:
我正在编写一个应用程序,我需要在 Mac 上模拟按键事件,给出一个代表每个键的代码。看来我需要使用该CGEventCreateKeyboardEvent
函数来创建事件。问题是这个功能需要一个Mac键码,而我有的是一个代表特定键的代码。因此,例如,我收到:
KEY_CODE_SHIFT
or KEY_CODE_A
- these are both numeric constants defined somewhere.
KEY_CODE_SHIFT
或者KEY_CODE_A
- 这些都是在某处定义的数字常量。
I need to take these constants and turn them into CGKeyCode
values.
我需要将这些常量转化为CGKeyCode
值。
My current attempt uses code similar to this SO question. The problem is that it only works for printable characters. If all else fails, I'm not above hard coding the conversion, but that would mean that I'd need a table of possible CGKeyCode values, which I have not yet been able to find.
我目前的尝试使用类似于这个 SO question 的代码。问题是它只适用于可打印的字符。如果所有其他方法都失败了,我不会对转换进行硬编码,但这意味着我需要一个可能的 CGKeyCode 值表,但我还没有找到。
Any ideas?
有任何想法吗?
回答by Dave DeLong
Here's code to simulate a Cmd-Saction:
这是模拟Cmd-S动作的代码:
CGKeyCode inputKeyCode = kVK_ANSI_S;
CGEventSourceRef source = CGEventSourceCreate(kCGEventSourceStateCombinedSessionState);
CGEventRef saveCommandDown = CGEventCreateKeyboardEvent(source, inputKeyCode, YES);
CGEventSetFlags(saveCommandDown, kCGEventFlagMaskCommand);
CGEventRef saveCommandUp = CGEventCreateKeyboardEvent(source, inputKeyCode, NO);
CGEventPost(kCGAnnotatedSessionEventTap, saveCommandDown);
CGEventPost(kCGAnnotatedSessionEventTap, saveCommandUp);
CFRelease(saveCommandUp);
CFRelease(saveCommandDown);
CFRelease(source);
A CGKeyCode
is nothing more than an unsigned integer:
ACGKeyCode
只不过是一个无符号整数:
typedef uint16_t CGKeyCode; //From CGRemoteOperation.h
Your real issue will be turning a character (probably an NSString
) into a keycode. Fortunately, the Shortcut Recorderproject has code that will do just that in the SRKeyCodeTransformer.m
file. It's great for transforming a string to a keycode and back again.
您真正的问题是将字符(可能是NSString
)转换为键码。幸运的是,Shortcut Recorder项目的代码可以在SRKeyCodeTransformer.m
文件中执行此操作。它非常适合将字符串转换为键码然后再转换回来。
回答by Tyler Long
Just in case some one needs a Swift version:
以防万一有人需要 Swift 版本:
XCode 7.3 and Swift 2.2:
XCode 7.3 和 Swift 2.2:
let event1 = CGEventCreateKeyboardEvent(nil, 0x09, true); // cmd-v down
CGEventSetFlags(event1, CGEventFlags.MaskCommand);
CGEventPost(CGEventTapLocation.CGHIDEventTap, event1);
let event2 = CGEventCreateKeyboardEvent(nil, 0x09, false); // cmd-v up
CGEventSetFlags(event2, CGEventFlags.MaskCommand);
CGEventPost(CGEventTapLocation.CGHIDEventTap, event2);
Code above simulates CMD-V pressed then released(AKA: paste).
上面的代码模拟 CMD-V 按下然后释放(又名:粘贴)。