1

谁能帮我把这个从 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);
}
4

3 回答 3

1

unichar是用于保存单个 UTF-16 代码单元的无符号 16 位类型。一个 UTF-16 字符由 1 个或 2 个 unichars(通常是 1 个)组成。在 C# 中,它就像char

于 2012-04-18T11:20:50.743 回答
1

我找到了解决方案!

只需这个小小的改动,它就对我有用:

public static extern void CGEventKeyboardSetUnicodeString(IntPtr @event, uint stringLength, char* unicodeString);

代替

public static extern void CGEventKeyboardSetUnicodeString(IntPtr @event, ulong stringLength, char* unicodeString);

祝你有美好的一天 ;)

于 2015-12-23T23:44:04.413 回答
0

在 C 中, aUniChar等价于 a unsigned short

于 2012-04-18T11:18:49.963 回答