如何在Swift中为Mac OS X开发一种简单的输入法?

如何在Swift中为Mac OS X开发一种简单的输入法?,swift,xcode,macos,ime,input-method-kit,Swift,Xcode,Macos,Ime,Input Method Kit,我知道有一个问题。Objective-C中也有,但它没有在Swift中提供示例 有人知道如何用Swift编写简单的IME吗?它可以有一些功能,比如重复字母,但什么也不做,所以我知道它确实有效。您使用哪个Xcode SDK构建并成功运行它?为了更易于阅读,我已将其上载到github repo: 具体地说,我已经将示例项目演示中的每个“步骤”上传为自己的git提交。这使得项目工作流更容易区分。接下来看看他们是如何向示例输入法项目添加功能的。我还尝试将第一部分“NumberInput0”项目从Obj

我知道有一个问题。Objective-C中也有,但它没有在Swift中提供示例

有人知道如何用Swift编写简单的IME吗?它可以有一些功能,比如重复字母,但什么也不做,所以我知道它确实有效。您使用哪个Xcode SDK构建并成功运行它?

为了更易于阅读,我已将其上载到github repo:


具体地说,我已经将示例项目演示中的每个“步骤”上传为自己的git提交。这使得项目工作流更容易区分。接下来看看他们是如何向示例输入法项目添加功能的。

我还尝试将第一部分“NumberInput0”项目从Objective-c转换为Swift

如果Swift项目中的“数字输入0”起作用,则可以快速转换后面的数字输入1、2、3

Swift项目中的“NumberInput 0”可以编译、安装、添加到输入源,可以选择和运行,但当我使用Xcode调试NumberInput.app时,键入键无法访问IMKInputController子类NumberInputController的方法inputText(…)

NumberInput 0仅包含4个文件和几行代码:

  • AppDelegate.swift
  • NumberInputController.swift
  • MainMenu.xib
  • Info.plist
  • NumberInputController列在info.plist中

    我已经成功地用Objective-C和Xcode 7重新创建了“NumberInput 0”项目,调试时只需键入键即可访问NumberInputController函数inputText(…)

    我是Swift新手,有人能帮我在Swift工作中获得“数字输入0”吗

    以下是3个文件的内容:

    AppDelegate.swift

    import Cocoa
    
    import InputMethodKit
    
    let kConnectionName = "NumberInput_1_Connection"
    
    var server:IMKServer = IMKServer.init()
    
    @NSApplicationMain
    
    class AppDelegate: NSObject, NSApplicationDelegate {
    
        func applicationDidFinishLaunching(aNotification: NSNotification) {
    
            let identifier = NSBundle.mainBundle().bundleIdentifier;         
            server = IMKServer.init(name: kConnectionName, bundleIdentifier: identifier)
    
        }
    
        func applicationWillTerminate(aNotification: NSNotification) {
    
        }
    }
    
    import Cocoa
    
    import InputMethodKit
    
    class NumberInputController: IMKInputController {
    
         override func inputText(string:String, client: AnyObject) ->Bool {
            // Debug break point put here
            print(string);
            return false;
        }
    }
    
    NumberInputController.swift

    import Cocoa
    
    import InputMethodKit
    
    let kConnectionName = "NumberInput_1_Connection"
    
    var server:IMKServer = IMKServer.init()
    
    @NSApplicationMain
    
    class AppDelegate: NSObject, NSApplicationDelegate {
    
        func applicationDidFinishLaunching(aNotification: NSNotification) {
    
            let identifier = NSBundle.mainBundle().bundleIdentifier;         
            server = IMKServer.init(name: kConnectionName, bundleIdentifier: identifier)
    
        }
    
        func applicationWillTerminate(aNotification: NSNotification) {
    
        }
    }
    
    import Cocoa
    
    import InputMethodKit
    
    class NumberInputController: IMKInputController {
    
         override func inputText(string:String, client: AnyObject) ->Bool {
            // Debug break point put here
            print(string);
            return false;
        }
    }
    
    Info.plist

    import Cocoa
    
    import InputMethodKit
    
    let kConnectionName = "NumberInput_1_Connection"
    
    var server:IMKServer = IMKServer.init()
    
    @NSApplicationMain
    
    class AppDelegate: NSObject, NSApplicationDelegate {
    
        func applicationDidFinishLaunching(aNotification: NSNotification) {
    
            let identifier = NSBundle.mainBundle().bundleIdentifier;         
            server = IMKServer.init(name: kConnectionName, bundleIdentifier: identifier)
    
        }
    
        func applicationWillTerminate(aNotification: NSNotification) {
    
        }
    }
    
    import Cocoa
    
    import InputMethodKit
    
    class NumberInputController: IMKInputController {
    
         override func inputText(string:String, client: AnyObject) ->Bool {
            // Debug break point put here
            print(string);
            return false;
        }
    }
    
    。。。
    ....
    NSMainNibFile
    主菜单
    非主类
    不适用
    LSBackgroundOnly
    1.
    InputMethodConnectionName
    数字输入1连接
    InputMethodServerControllerClass
    数字输入控制器
    TsinPutMethodConfigleKey
    九.蒂芙
    tInputMethodCharacter曲目键
    拉丁语
    
    Pkamb的项目仍在objective-c中,而不是Swift中。基于输入法工具包框架,我在objective-c中开发了一个功能齐全的输入法,并在GitHub上共享:请注意,这不是一个“答案”,因为它不是一个工作示例,所以这个问题仍然没有答案。