InputMethodKit超入門

36

description

Cocoa勉強会 #39

Transcript of InputMethodKit超入門

Page 1: InputMethodKit超入門

InputMethodKit超⼊門

Cocoa勉強会�#39�/�2010.02.11�(祝)�/�⽊村渡

Powered by Rabbit 0.6.4 and COZMIXNG

Page 2: InputMethodKit超入門

Input�Method

効率の良い⼊⼒✓

さまざまなアプリケーションとコミュニケート

InputMethodKitは、後者を⽀援するフレームワーク

Page 3: InputMethodKit超入門

InputMethodKit

かな漢字変換などの⼊⼒プログラムをつくるためのフレームワーク

アプリとして⼊⼒プログラムを作成✓

Cocoa✓

Leopardで導⼊された✓

Page 4: InputMethodKit超入門

さまざまな⽇本語⼊⼒プログラム

ことえり✓

かわせみ✓

AquaSKK✓

Google⽇本語⼊⼒✓

        などなど

Page 5: InputMethodKit超入門

InputMethodKitの構成

+----------+ +------------------------+|=user app=| | =input method app= || Client <------->(Server)<-> Controller |+----------+ +------------------------+

Page 6: InputMethodKit超入門

IMK�Classes

IMKServerユーザ操作と⼊⼒操作を仲介

IMKInputController⼊⼒プログラムの処理本体

IMKCandidates選択候補のユーザインターフェイスを制御

Page 7: InputMethodKit超入門

IMK�Protocols

IMKServerInput⼊⼒プログラムのインターフェイス

IMKTextInput⼊⼒プログラムとやりとりするアプリのインターフェイス

IMKStateSettingIMKMouseHandling

Page 8: InputMethodKit超入門

3つの実装手段

IMKServerInputプロトコルキーバインディング✓

⼊⼒⽂字+修飾キーを解釈✓

TSMのイベントを利用✓

Page 9: InputMethodKit超入門

キーバインディング

inputText:client:⼊⼒された⽂字を受け取る

didCommandBySelector:client:キーバインディングで定義されたコマンドを受け取る

"apple"が⼊⼒された

"moveForward:"コマンドが指⽰された

Page 10: InputMethodKit超入門

キーバインディング

キー(の組み合わせ)をObjective-Cメッセージに割り当て

StandardKeyBinding.dictを参照

<key>^f</key><string>moveForward:</string>

Page 11: InputMethodKit超入門

⼊⼒⽂字+修飾キーを解釈

inputText:key:modifiers:client:⼊⼒された⽂字と修飾キーを受け取る

"apple"が⼊⼒された

"f"とCtrlキーが⼊⼒された

Page 12: InputMethodKit超入門

TSMのイベントを利用

handleEvent:client:よくわからん。

たぶん従来のコンポーネントからの移⾏しやすいんじゃないかな。

Page 13: InputMethodKit超入門

3つの実装手段

IMKServerInputプロトコルキーバインディング✓

⼊⼒⽂字+修飾キーを解釈✓

TSMのイベントを利用✓

上のふたつのどちらか

Page 14: InputMethodKit超入門

つくってみよう

Page 15: InputMethodKit超入門

偉大なるお手本

AppleのサンプルNumberInput

Page 16: InputMethodKit超入門

NumberInput作成の手順

⼊⼒メニューに表⽰1.�

ユーザ操作から⼊⼒⽂字を決定2.�

複数の変換モード3.�

変換候補の表⽰4.�

環境設定のUI5.�

Page 17: InputMethodKit超入門

今回のサンプル

単純のため次のように

英数字のみ扱う✓

英単語を候補から選択できる✓

(できれば)⼊⼒中のテキストから候補を抽出する

Page 18: InputMethodKit超入門

実装手順

最⼩限。⼊⼒プログラムとして選択できる

1.�

試験的な変換⼊⼒2.�

目的の変換処理、変換候補の選択3.�

Page 19: InputMethodKit超入門

最⼩限の実装

Info.plistに記述✓

main()でIMKServerを⽣成✓

IMKServerControllerのサブクラス✓

Page 20: InputMethodKit超入門

Info.plist

LSBackgroundOnlyInputMethodConnectionNameInputMethodServerControllerClasstsInputMethodIconFileKeytsInputMethodCharacterRepertoireKey

Page 21: InputMethodKit超入門

Info.plist

LSBackgroundOnly1に。アプリケーションをバックグラウンドにします。

Page 22: InputMethodKit超入門

Info.plist

InputMethodConnectionNameアプリケーションと⼊⼒プログラムの通信に利用する名前を指定します。この名前はほかの⼊⼒プログラムと重複しないように注意してください。

Page 23: InputMethodKit超入門

Info.plist

InputMethodServerControllerClass⼊⼒サービスを提供するクラスを指定

このクラスはIMKServerControllerのサブクラス

Page 24: InputMethodKit超入門

Info.plist

tsInputMethodIconFileKey⼊⼒メニューに表⽰するアイコン(ことえりなら[あ])の画像ファイルを指定

Page 25: InputMethodKit超入門

Info.plist

tsInputMethodCharacterRepertoireKey⼊⼒するキャラクタセットをISO1592で指定(複数可)

Page 26: InputMethodKit超入門

プログラム

main.m✓

IMKServerControllerのサブクラス✓

Page 27: InputMethodKit超入門

main.m

ここでIMKServerのインスタンスを⽣成し、コネクションの名前などを指定します。

Page 28: InputMethodKit超入門

IMKInputController

inputText:client:この⼊⼒プログラムで⼊⼒テキストを処理するならYESを、そうでなければNOを返します。

とりあえず常にNOにしておきます。

Page 29: InputMethodKit超入門

動作確認�0

⼊⼒メニューに表⽰できる

/Library/Input Methodsに✓

ログオフ〜再ログオン✓

開発時はユーザースイッチを利用するのがよいかも

Page 30: InputMethodKit超入門

試験的な変換⼊⼒を実装

-(BOOL)inputText:(NSString*)string client:(id)sender{ if ([string hasPrefix:@"o"]) { [sender insertText:@"Objective-C" replacementRange:NSMakeRange(0, 0)]; return YES; } else { return NO;

Page 31: InputMethodKit超入門

動作確認�1

"o"を⼊⼒すると"Objective-C"が他の⽂字はそのまま

abc..lmnObjective-Cpq..xyz

Page 32: InputMethodKit超入門

変換候補の表示

cancdidates:候補の配列を返す

candidateSelected:候補が選択された

candidateSelectionChanged:選択候補が変更された

Page 33: InputMethodKit超入門

動作確認�2

⼊⼒中にスペースキーで変換候補を表⽰

⼊⼒中の⽂字が表⽰されない...✓

Page 34: InputMethodKit超入門

インライン⼊⼒

これは宿題っ

setMarkedText:selectionRange:replacementRange:

Page 35: InputMethodKit超入門

Page 36: InputMethodKit超入門

質問などあればどうぞ

Powered by Rabbit 0.6.4 and COZMIXNG