谁能帮我把这个从 Objective-C 移植到 MonoMac 的工作?
CGEventSourceRef eventSource = CGEventSourceCreate(kCGEventSourceStateHIDSystemState);
CGEventRef keyEventDown = CGEventCreateKeyboardEvent(eventSource, 0, true);
NSString * characters = @"ABCD";
UniChar buffer;
for (int i = 0; i < [characters length]; i++)
{
[characters getCharacters:&buffer range:NSMakeRange(i, 1)];
keyEventDown = CGEventCreateKeyboardEvent(eventSource, 1, true);
CGEventKeyboardSetUnicodeString(keyEventDown, 1, &buffer);
CGEventPost(kCGHIDEventTap, keyEventDown);
CFRelease(keyEventDown);
}
CFRelease(eventSource);
我使用以下代码导入了 DLL,但我不知道如何处理 Objective-C 的 UniChar。我不知道我应该在 Objective-C 更改的 C# 中使用哪种类型。这是一个 Obj-C 需要更改的指针,我可以再次在我的 C# 代码中访问它。
using System;
using System.Runtime.InteropServices;
using MonoMac;
namespace SomeNameSpace
{
public unsafe static class Native
{
[DllImport(Constants.CoreGraphicsLibrary)]
public static extern IntPtr CGEventCreateKeyboardEvent(IntPtr source, ushort virtualKey, bool keyDown);
[DllImport(Constants.CoreGraphicsLibrary)]
public static extern void CGEventKeyboardSetUnicodeString(IntPtr @event, ulong stringLength, char* unicodeString);
[DllImport(Constants.CoreGraphicsLibrary)]
public static extern IntPtr CGEventSourceCreate(CGEventSourceStateID stateID);
[DllImport(Constants.CoreGraphicsLibrary)]
public static extern void CGEventPost(CGEventTapLocation tap, IntPtr @event);
[DllImport(Constants.CoreFoundationLibrary)]
public static extern void CFRelease (IntPtr cf);
}
public enum CGEventTapLocation:uint
{
kCGHIDEventTap = 0,
kCGSessionEventTap,
kCGAnnotatedSessionEventTap
}
public enum CGEventSourceStateID:int
{
kCGEventSourceStatePrivate = -1,
kCGEventSourceStateCombinedSessionState = 0,
kCGEventSourceStateHIDSystemState = 1
}
}
public unsafe void SomeMethod()
{
IntPtr eventSource = Native.CGEventSourceCreate(CGEventSourceStateID.kCGEventSourceStateHIDSystemState);
IntPtr keyEventDown = Native.CGEventCreateKeyboardEvent(cgEventSource,0,true);
string characters = @"ABCD";
char buffer;
for (int i = 0; i < characters.Length; i++)
{
buffer = characters[i];
keyEventDown = Native.CGEventCreateKeyboardEvent(eventSource, 1, true);
Native.CGEventKeyboardSetUnicodeString(keyEventDown, 1, &buffer);
Native.CGEventPost(CGEventTapLocation.kCGHIDEventTap, keyEventDown);
Native.CFRelease(keyEventDown);
}
Native.CFRelease(eventSource);
}