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