How to develop a simple input method for Mac OS X in Swift?
Asked Answered
B

2

6

I know there is an API reference for the InputMethodKit framework. And there is also sample code in Objective-C, but it doesn't provide an example in Swift.

Does anyone know how to make a simple IME in Swift? 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?

Basle answered 7/1, 2015 at 5:46 Comment(0)
C
2

I've uploaded Apple's NumberInput sample code to a github repo for easier readability:

https://github.com/pkamb/NumberInput_IMKit_Sample

Specifically I have uploaded each "step" in the sample project demonstration as its own 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.

Chryso answered 14/6, 2015 at 1:53 Comment(1)
Pkamb's project still in objective-c, not in Swift.Molecular
M
-2

I also have tried to convert Apple's IMKit sample first part "NumberInput 0" project from Objective-c to Swift

If "NumberInput 0" in Swift project works, then following NumberInput 1,2,3 can be converted quickly.

"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

NumberInput 0 is simply only includes 4 files, few lines of code:

  1. AppDelegate.swift
  2. NumberInputController.swift
  3. MainMenu.xib
  4. Info.plist

NumberInputController is listed in 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.

I am new to Swift, can any one help me to get "NumberInput 0" in Swift working?

Following are contents of 3 files:

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

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.

Molecular answered 29/1, 2016 at 2:58 Comment(2)
based on Input Method Kit Framework, I developed a fully functional Input method in Objective-c, and shared on GitHub:github.com/HeChinese/OpenHeInput-MacOSMolecular
Note that this is not an "answer" because it is not a working example, so this question remains unanswered.Candelariacandelario

© 2022 - 2024 — McMap. All rights reserved.