WebAPI/KeboardIME

From MozillaWiki
< WebAPI
Revision as of 16:57, 31 January 2013 by Yuanxulei (talk | contribs) (Related)

Jump to: navigation, search

Virtual Keyboard/IME API

Introduction

Virtual Keyboard/IME API aims to implement the system IME as a Web App.

The API provides the communication channel between the IME App and the other App that receives user's inputs.

It is very different from the IME API from Google that aims to re-use the system's IME in a web page.

Status

API discussion:

  1. WebAPI mailing list post
  2. Extended API mailing list post

See bug 737110 for the implementation.

Features

The Virtual Keyboard/IME API supports the following features:

  • Notifies the VKB app when the focus text field was changing in other

apps

  • Allow user to manual hide the keyboard. Check bug 737110.
  • The VKB app should be responsive to properties and the state of the input field (more than HTML5 input type, including current content, cursor position, x-inputmode bug 796544).
  • Sends trust
  • The VKB app should be able to send trusted key events such as they are considered by the other apps as user' inputs.
  • The VKB app should be able to send a character or a string to the current input cursor position.
  • Keyboard should be able to overwrite the current value of the input field of the input field and set the cursor position.
  • The VKB app should be able to move the cursor position or select a specified range of text.
  • The VKB should be able to switch the focus onto the previous/next input field.
  • The return key label of the VKB can be customized.

Proposed API

partial interface Navigator {
    readonly attribute InputMethodManager mozInputMethodManager;
    readonly attribute InputMethodConnection mozInputMethodConnection;
};
interface InputMethodConnection {
    // User begins or finishes editing a field.
    // If the user change input directly from one input to another, only one event will be dispatched.
    attribute Function oninputmethodstatechange;

    // Whether the user begins editing.
    readonly attribute boolean inputStarted; 

    // User moves the cursor, changes the selection, or alters the composing text length
    attribute Function onselectionchange;

    // Send a keyevent to the input field that is currently attached. It exists in the current interface
    void sendKey(in long keyCode, in long charCode, in long modifiers);

    // Insert the given text at the cursor position. Replace the selected text if any and clear the composing text.
    void commitText(in DOMString text);

    // Set the text value of the current input field. 
    void setText(in DOMString text);

    // Get the text value of the current input field asynchronously.
    void getText(in long start, in long end, in Function callback);

    // Length of the content
    readonly attribute long textLength;

    // Delete text around the cursor
    void deleteSurroundingText(in long beforeLength, in long afterLength);

    // Set the composing text before the current cursor position.
    // To clear the composing text, set the text paramter to the empty string.
    void setComposingText(in DOMString text);

    // The start and stop position of the selection. 
    readonly attribute long selectionStart;
    readonly attribute long selectionEnd;

    // Set the selection range of the the editable text.
    // Note that the start position should be less or equal to the end position.
    // To move the cursor, set the start and end position to the same value.
    void setSelectionRange(in long start, in long end);

    // Clear the focus of the current input field and hide the keyboard.
    void removeFocus();

    // Focus the next text field.
    void advanceFocus();

    // Focus the previous text field.
    void rewindFocus();

    // This read only attribute is true if user can advance the focus to the next
    // text field.
    readonly attribute boolean canAdvanceFocus;

    // This readonly attribute is true if user can rewind the focus back to the
    // previous text field.
    readonly attribute boolean canRewindFocus;

    // The input mode string.
    // https://bugzilla.mozilla.org/show_bug.cgi?id=796544
    // It can be one of the following values:
    // "none"
    // "verbatim"   - no capitalization, no word suggestions
    // "latin"          - word suggestions but no capitalization
    // "latin-prose" - word suggestions and capitalization at the start of sentences
    // "latin-name" - word suggestions and capitalize each word
    readonly attribute DOMString inputMode;

    // The type of the input field, including text, number, password, url, tel and email.
    readonly attribute DOMString inputType;

    // Get the return key type, which determines the title of the key displayed in the keyboard and action 
    // performed when it is  pressed. The value could be "done", "next", "go", "search" and "send".
    // If the value is "next", when pressing the enter key, the focus should be move to the next input field.
    readonly attribute DOMString returnKeyType;

    // Get the customized label for the return key.
    readonly attribute DOMString returnKeyLabel;
};

Examples

var conn = navigator.mozInputMethodConnection;
// Called when the user starts or finishes editing an input field
conn.oninputmethodstatechange= function(event) {
    if (conn.inputStarted) {
        // Text input should start
    } else {
        // Text input should end
    }
};
// Insert a string at the current cursor position
conn.commitText('Hello world');
// Get the selected text
conn.getText(conn.selectionStart, conn.selectionEnd, function callback(text) {
    var selectedText = text;
});
// Move the cursor position
var position = 10;
conn.setSelectionRange(position, position);
// Hide the keyboard
conn.removeFocus();
// Switch the focus onto the next input field if possible
if (conn.canAdvanceFocus) {
    conn.advanceFocus();
}

Related

Android IME API:

http://developer.android.com/guide/topics/text/creating-input-method.html#IMEAPI

iOS Keyboard Management:

http://developer.apple.com/library/ios/#documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/KeyboardManagement/KeyboardManagement.html#//apple_ref/doc/uid/TP40009542-CH5-SW1