简体   繁体   English

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

[英]How to develop a simple input method for Mac OS X in Swift?

I know there is an API reference for the InputMethodKit framework .我知道InputMethodKit 框架有一个API 参考 And there is also sample code in Objective-C, but it doesn't provide an example in Swift. Objective-C 中也有示例代码,但没有提供 Swift 中的示例。

Does anyone know how to make a simple IME in Swift?有谁知道如何在 Swift 中制作一个简单的 IME? It can have features like repeating the letter but just not doing nothing, so I can know it actually works.它可以具有重复字母之类的功能,但只是什么都不做,所以我可以知道它确实有效。 With which Xcode SDKs do you build it and run it successfully?您使用哪些 Xcode SDK 构建并成功运行它?

I've uploaded Apple's NumberInput sample code to a github repo for easier readability:我已将Apple 的NumberInput示例代码上传到 github 存储库,以便于阅读:

https://github.com/pkamb/NumberInput_IMKit_Sample https://github.com/pkamb/NumberInput_IMKit_Sample

Specifically I have uploaded each "step" in the sample project demonstration as its own git commit.具体来说,我已将示例项目演示中的每个“步骤”作为自己的 git commit 上传。 This allows for much easier diffing of the project workflow.这允许更轻松地区分项目工作流程。 Follow along to see how they add features to the sample Input Method project.跟随以了解他们如何向示例输入法项目添加功能。

I also have tried to convert Apple's IMKit sample first part "NumberInput 0" project from Objective-c to Swift我还尝试将Apple 的 IMKit 示例第一部分“NumberInput 0”项目从 Objective-c 转换为 Swift

If "NumberInput 0" in Swift project works, then following NumberInput 1,2,3 can be converted quickly.如果Swift项目中的“NumberInput 0”有效,那么可以快速转换以下NumberInput 1,2,3。

"NumberInput 0" in Swift project can be compiled, installed, added to input sources, can be be selected and ran, but the IMKInputController subclass NumberInputController's method inputText(...) can't be reached by typing keys when I using Xcode to debug NumberInput.app Swift 项目中的“NumberInput 0”可以被编译、安装、添加到输入源,可以被选择和运行,但是当我使用 Xcode调试 NumberInput.app

NumberInput 0 is simply only includes 4 files, few lines of code: NumberInput 0 只是简单地只包含 4 个文件,几行代码:

  1. AppDelegate.swift AppDelegate.swift
  2. NumberInputController.swift数字输入控制器.swift
  3. MainMenu.xib主菜单文件
  4. Info.plist信息表

NumberInputController is listed in info.plist. NumberInputController 列在 info.plist 中。

I have successfully recreated "NumberInput 0" project using Objective-C with Xcode 7, all works, NumberInputController function inputText(...) can be reached by typing keys when debugging.我已经成功地使用带有 Xcode 7 的 Objective-C 重新创建了“NumberInput 0”项目,一切正常,在调试时可以通过键入键来访问 NumberInputController 函数 inputText(...)。

I am new to Swift, can any one help me to get "NumberInput 0" in Swift working?我是 Swift 的新手,有人可以帮助我在 Swift 中使用“NumberInput 0”吗?

Following are contents of 3 files:以下是3个文件的内容:

AppDelegate.swift 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) {

    }
}

NumberInputController.swift数字输入控制器.swift

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信息表

 ... <dict> .... <key>NSMainNibFile</key> <string>MainMenu</string> <key>NSPrincipalClass</key> <string>NSApplication</string> <key>LSBackgroundOnly</key> <string>1</string> <key>InputMethodConnectionName</key> <string>NumberInput_1_Connection</string> <key>InputMethodServerControllerClass</key> <string>NumberInputController</string> <key>tsInputMethodIconFileKey</key> <string>nine.tiff</string> <key>tsInputMethodCharacterRepertoireKey</key> <array> <string>Latn</string> </array> </dict> </plist>

Thanks.谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM