识别用户在编辑NSTextField swift时是否按下了箭头键 [英] Recognize if user has pressed arrow key while editing NSTextField swift

查看:101
本文介绍了识别用户在编辑NSTextField swift时是否按下了箭头键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有很多NSTextField,我想知道,如果用户在编辑其中之一时按下了箭头键之一.函数

I have many NSTextFields and I want to know, if the user has pressed one of the arrow keys while editing one of them. The function

override func keyDown(theEvent: NSEvent) {
    switch theEvent.character {
    case NSRightArrowFunctionKey:
        println(1)
        moveGor(NSRightArrowFunctionKey)
    case NSLeftArrowFunctionKey:
        moveGor(NSLeftArrowFunctionKey)
    case NSUpArrowFunctionKey:
        moveVert(NSUpArrowFunctionKey)
    case NSDownArrowFunctionKey:
        moveVert(NSDownArrowFunctionKey)
    default:
        super.keyDown(theEvent)
    }
}

似乎不起作用.还有其他方法可以快速做到这一点吗?

doesn't seem to work. Is there any other way to do that in swift?

我有NSEvent的扩展名:

extension NSEvent {
    var character: Int {
        let str = charactersIgnoringModifiers!.utf16
        return Int(str[str.startIndex])
    }
}

我在上一个功能中使用的

that I used in previous function

推荐答案

当文本字段具有焦点时,它们实际上没有.而是将文本 view 添加到文本字段顶部的窗口中,并且该文本视图是第一响应者,并处理所有输入和编辑行为.文本视图称为字段编辑器" .文本字段不接收按键事件.文本视图可以.

When text fields have the focus, they actually don't. Instead, a text view is added to the window on top of the text field and that text view is the first responder and handles all of the input and editing behaviors. The text view is known as the "field editor". The text field does not receive key down events; the text view does.

您可以将自定义文本视图替换为文本字段的第一响应者,并让该文本视图专门处理按键事件.但是,利用文本字段是文本视图的委托这一事实可能更容易.取决于您要实现的目标,可以实现-textView:willChangeSelectionFromCharacterRange:toCharacterRange:,但这不仅仅涉及箭头键.

You could substitute a custom text view as the first responder for the text field and have that text view handle the key down events specially. However, it's probably easier to take advantage of the fact that the text field is the delegate for the text view. Depending on exactly what you're trying to achieve, you might implement -textView:willChangeSelectionFromCharacterRange:toCharacterRange:, but that's not exclusively about arrow keys.

一种更有前途的方法可能是-textView:doCommandBySelector:.这也不是真正意义上的箭头键,但是在某些方面还是更好.箭头键和所有其他标准编辑键的操作方式是通过键绑定系统到命令选择器中.命令选择器表示正在执行的语义操作,例如-moveUp:.它们通过修饰符标志进行更改,因此Shift-up-arrow可能会生成-moveUpAndModifySelection:.

A more promising method might be -textView:doCommandBySelector:. That's also not really about the arrow keys, but in some ways it's better. The arrow keys, and all other standard editing keys, operate by being translated through the key bindings system into command selectors. The command selectors represent the semantic operation being performed, like -moveUp:. They are changed by modifier flags, so that Shift-up-arrow might generate -moveUpAndModifySelection:.

无论如何,在-textView:doCommandBySelector:中,您可以基于选择器执行代码,并告诉文本视图不执行其他任何操作(通过返回YES),或者让文本视图另外执行其正常的操作(通过返回NO). (很明显,对于您不需要的任何内容,返回NO.)

Anyway, in -textView:doCommandBySelector:, you can execute code based on the selector and either tell the text view not to do anything else (by returning YES) or let the text view do its normal thing in addition (by returning NO). (Obviously, return NO for anything that you don't care about.)

这篇关于识别用户在编辑NSTextField swift时是否按下了箭头键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆