【问题标题】:KeyDown event in applescript objective-c XcodeAppleScript Objective-C Xcode中的KeyDown事件
【发布时间】:2019-04-05 17:16:39
【问题描述】:

我希望能够访问文本字段中的 KeyDown 事件。在下面的代码中,我编写了一个名为 TextfieldKeyDown 的普通方法,并将其链接到委托列表中的文本字段,当按下 enter 时,方法代码将启动。例如,当我按下字母 D 或字母 F 时,如何执行该方法?

on TextfieldKeyDown_(sender)
    log "Enter Pressed"
end TextfieldKeyDown_

提前致谢。

【问题讨论】:

    标签: xcode applescript textfield keydown


    【解决方案1】:

    您的处理程序只是一个常规操作方法,通常在 textField 中的编辑完成时调用。对于额外的控制,你可以添加一个 NSControlTextEditingDelegate 方法来观察 textField 的变化。

    界面编辑器中,将您的 textField 代理连接到您要使用的类/文件(例如 AppDelegate),然后向其中添加 controlTextDidChange: 方法:

    on controlTextDidChange_(aNotification) -- example to show character at insertion point
      set theTextField to aNotification's object() -- the control being changed
      set {theText, theEditor} to {stringValue(), currentEditor()} of theTextField
      set insertPoint to (location of first item of (theEditor's selectedRanges() as list)) as integer
      if (theText as text is not "") and (insertPoint is not 0) then
        set theCharacter to text insertPoint of (theText as text) -- the added character
      else
        set theCharacter to "(none)"
      end if
      log "character at insertion point: " & theCharacter — show it
    end
    

    【讨论】:

    • 不幸的是不起作用。这是错误:2019-04-05 23:02:44.448360+0200 KeyDown App[2842:130500] -[NSTextField object]: unrecognized selector sent to instance 0x100708520
    • 我直接从一个工作项目中复制了我的示例方法 - NSTextField object 错误看起来你做了一些更改?
    • 即使删除方法中的所有代码并仅保留示例 -> (log "Pressed") 也不起作用。
    • textField 的delegate 出口是否连接到包含处理程序的委托对象?如果你使用的是AppDelegate.applescript,你也可以在applicationWillFinishLaunching之类的地方使用NSTextField的委托方法,例如set myTextField's delegate to me(或者任何textField的名字)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多