有没有人可以帮助我从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。我不知道我应该在C#中使用哪种类型的Objective-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);
}
答案 0 :(得分:1)
unichar
是一个无符号16位类型,用于保存单个UTF-16代码单元。 UTF-16字符由1或2个unichars(通常为1)组成。在C#中,它与char
答案 1 :(得分:1)
我找到了解决方案!
这对我来说只有这个小小的改变:
public static extern void CGEventKeyboardSetUnicodeString(IntPtr @event, uint stringLength, char* unicodeString);
而不是
public static extern void CGEventKeyboardSetUnicodeString(IntPtr @event, ulong stringLength, char* unicodeString);
度过美好的一天;)
答案 2 :(得分:0)
在C中,UniChar
相当于unsigned short
。