English 中文(简体)
在Mac OS X中模拟按键事件
原标题:Simulating key press events in Mac OS X

我正在编写一个应用程序,在其中需要模拟在 Mac 上按键事件,给定表示每个键的代码。似乎我需要使用CGEventCreateKeyboardEvent函数来创建事件。问题是这个函数需要一个 Mac keycode,而我拥有的是表示特定键的代码。因此,例如,我收到:

KEY_CODE_SHIFTKEY_CODE_A - 这两个都是在某个地方定义的数值常量。

我需要将这些常量转换为CGKeyCode值。

我的当前尝试使用类似于此 Stack Overflow 问题的代码。问题在于,它只适用于可打印字符。如果其他方法都不行,我不介意硬编码转换,但这意味着我需要一个可能的 CGKeyCode 值表格,但我还没有找到。

有任何想法吗?

最佳回答

这是模拟 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);

一个CGKeyCode只不过是一个无符号整数:

typedef uint16_t CGKeyCode;  //From CGRemoteOperation.h

你面临的实际问题是将一个字符(可能是NSString)转换成键码。幸运的是,《快捷键录制器Shortcut Recorder》项目中的代码可以在SRKeyCodeTransformer.m文件中完成这一转换。它非常适合将字符串转换成键码,然后再转回去。

问题回答

以防有人需要Swift版本:

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

上面的代码模拟了按下并释放CMD-V(也称为粘贴)的操作。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签