如何从cocoa keyDown事件中获取unicode字符串?

时间:2012-08-20 07:36:35

标签: objective-c

我读过cocoa Key Event Handling文档。并获得如下代码片段示例: 问题是当我改变输入法而非英语(如中文)时。代码段不起作用,它仍返回单个字母字符,但不返回unicode中文字符。任何建议或我遗漏了部分文件?

- (void)keyDown:(NSEvent *)theEvent
{
    [self interpretKeyEvents:[NSArray arrayWithObject:theEvent]];
}

// The following action methods are declared in NSResponder.h
- (void)insertTab:(id)sender
{
    if ([[self window] firstResponder] == self)
    {
        [[self window] selectNextKeyView:self];
    }
}


- (void)insertBacktab:(id)sender
{
    if ([[self window] firstResponder] == self)
    {
        [[self window] selectPreviousKeyView:self];
    }
}

- (void)insertText:(id)string
{
    [super insertText:string];  // have superclass insert it
}

2 个答案:

答案 0 :(得分:0)

如果默认实现([NSEvent characters]charactersIgnoringModifiers?)不起作用,您可以尝试通过事件的@property CGEvent访问该值,然后查看是否获得了您想要的结果类似于CGEventKeyboardGetUnicodeString

答案 1 :(得分:0)

如果你正在NSTextView中捕捉文字(我猜你是,因为你有-insertText:),那么你可以使用NSTextViewDelegate来捕获Unicode输入。

例如,实施[NSTextViewDelegate textView:shouldChangeTextInRange:replacementString:]

与按键级别操作的NSEvent不同,此方法在较高级别的Unicode字符下运行。