Welcome What's new in Chrome extensions Getting started API Reference Samples
Welcome What's new in Chrome extensions Getting started API Reference Samples

chrome.input.ime

  • Description

    Use the chrome.input.ime API to implement a custom IME for Chrome OS. This allows your extension to handle keystrokes, set the composition, and manage the candidate window.

  • Permissions
    input

Manifest #

You must declare the "input" permission in the extension manifest to use the input.ime API. For example:

{
"name": "My extension",
...
"permissions": [
"input"
],
...
}

Examples #

The following code creates an IME that converts typed letters to upper case.

var context_id = -1;

chrome.input.ime.onFocus.addListener(function(context) {
context_id = context.contextID;
});

chrome.input.ime.onKeyEvent.addListener(
function(engineID, keyData) {
if (keyData.type == "keydown" && keyData.key.match(/^[a-z]$/)) {
chrome.input.ime.commitText({"contextID": context_id,
"text": keyData.key.toUpperCase()});
return true;
} else {
return false;
}
}
);

For an example of using this API, see the basic input.ime sample. For other examples and for help in viewing the source code, see Samples.

Summary

Types

AssistiveWindowProperties

Since Chrome 85.

Properties of the assistive window.

Properties

  • announceString
    string optional

    Strings for ChromeVox to announce.

  • type
    string "undo"
  • visible
    boolean

    Sets true to show AssistiveWindow, sets false to hide.

InputContext

Describes an input Context

Properties

  • autoCapitalize

    Since Chrome 69.

    The auto-capitalize type of the text field.

  • autoComplete
    boolean

    Since Chrome 40.

    Whether the text field wants auto-complete.

  • autoCorrect
    boolean

    Since Chrome 40.

    Whether the text field wants auto-correct.

  • contextID
    number

    This is used to specify targets of text field operations. This ID becomes invalid as soon as onBlur is called.

  • shouldDoLearning
    boolean

    Since Chrome 68.

    Whether text entered into the text field should be used to improve typing suggestions for the user.

  • spellCheck
    boolean

    Since Chrome 40.

    Whether the text field wants spell-check.

  • Type of value this text field edits, (Text, Number, URL, etc)

KeyboardEvent

See http://www.w3.org/TR/DOM-Level-3-Events/#events-KeyboardEvent

Properties

  • altKey
    boolean optional

    Whether or not the ALT key is pressed.

  • altgrKey
    boolean optional

    Since Chrome 79.

    Whether or not the ALTGR key is pressed.

  • capsLock
    boolean optional

    Whether or not the CAPS_LOCK is enabled.

  • code
    string

    Value of the physical key being pressed. The value is not affected by current keyboard layout or modifier state.

  • ctrlKey
    boolean optional

    Whether or not the CTRL key is pressed.

  • extensionId
    string optional

    The extension ID of the sender of this keyevent.

  • key
    string

    Value of the key being pressed

  • keyCode
    number optional

    The deprecated HTML keyCode, which is system- and implementation-dependent numerical code signifying the unmodified identifier associated with the key pressed.

  • requestId
    string optional

    (Deprecated) The ID of the request. Use the requestId param from the onKeyEvent event instead.

  • shiftKey
    boolean optional

    Whether or not the SHIFT key is pressed.

  • One of keyup or keydown.

MenuItem

A menu item used by an input method to interact with the user from the language menu.

Properties

  • checked
    boolean optional

    Indicates this item should be drawn with a check.

  • enabled
    boolean optional

    Indicates this item is enabled.

  • id
    string

    String that will be passed to callbacks referencing this MenuItem.

  • label
    string optional

    Text displayed in the menu for this item.

  • style
    MenuItemStyle optional

    The type of menu item.

  • visible
    boolean optional

    Indicates this item is visible.

MenuParameters

Since Chrome 88.

Properties

  • engineID
    string

    ID of the engine to use.

  • items

    MenuItems to add or update. They will be added in the order they exist in the array.

AssistiveWindowButton

Since Chrome 85.

ID of buttons in assistive window.

Enum

"undo", or "addToDictionary"

AssistiveWindowType

Since Chrome 85.

Type of assistive window.

Enum

"undo"

AutoCapitalizeType

Since Chrome 69.

The auto-capitalize type of the text field.

Enum

"characters", "words", or "sentences"

InputContextType

Since Chrome 44.

Type of value this text field edits, (Text, Number, URL, etc)

Enum

"text", "search", "tel", "url", "email", "number", "password", or "null"

KeyboardEventType

Since Chrome 44.

Enum

"keyup", or "keydown"

MenuItemStyle

Since Chrome 44.

The type of menu item. Radio buttons between separators are considered grouped.

Enum

"check", "radio", or "separator"

MouseButton

Since Chrome 44.

Which mouse buttons was clicked.

Enum

"left", "middle", or "right"

ScreenType

Since Chrome 44.

The screen type under which the IME is activated.

Enum

"normal", "login", "lock", or "secondary-login"

UnderlineStyle

Since Chrome 44.

The type of the underline to modify this segment.

Enum

"underline", "doubleUnderline", or "noUnderline"

WindowPosition

Since Chrome 44.

Where to display the candidate window. If set to 'cursor', the window follows the cursor. If set to 'composition', the window is locked to the beginning of the composition.

Enum

"cursor", or "composition"

Methods

clearComposition

input.ime.clearComposition(parameters: object, callback: function)

Clear the current composition. If this extension does not own the active IME, this fails.

Parameters

  • parameters
    object
    • contextID
      number

      ID of the context where the composition will be cleared

  • callback
    function

    Called when the operation completes with a boolean indicating if the text was accepted or not. On failure, runtime.lastError is set.

    The callback parameter should be a function that looks like this:

    (success: boolean) => {...}
    • success
      boolean

commitText

input.ime.commitText(parameters: object, callback: function)

Commits the provided text to the current input.

Parameters

  • parameters
    object
    • contextID
      number

      ID of the context where the text will be committed

    • text
      string

      The text to commit

  • callback
    function

    Called when the operation completes with a boolean indicating if the text was accepted or not. On failure, runtime.lastError is set.

    The callback parameter should be a function that looks like this:

    (success: boolean) => {...}
    • success
      boolean

deleteSurroundingText

input.ime.deleteSurroundingText(parameters: object, callback: function)

Deletes the text around the caret.

Parameters

  • parameters
    object
    • contextID
      number

      ID of the context where the surrounding text will be deleted.

    • engineID
      string

      ID of the engine receiving the event.

    • length
      number

      The number of characters to be deleted

    • offset
      number

      The offset from the caret position where deletion will start. This value can be negative.

  • callback
    function

    Called when the operation completes.

    The callback parameter should be a function that looks like this:

    () => {...}

hideInputView

input.ime.hideInputView()

Hides the input view window, which is popped up automatically by system. If the input view window is already hidden, this function will do nothing.

keyEventHandled

input.ime.keyEventHandled(requestId: string, response: boolean)

Indicates that the key event received by onKeyEvent is handled. This should only be called if the onKeyEvent listener is asynchronous.

Parameters

  • requestId
    string

    Request id of the event that was handled. This should come from keyEvent.requestId

  • response
    boolean

    True if the keystroke was handled, false if not

sendKeyEvents

input.ime.sendKeyEvents(parameters: object, callback: function)

Sends the key events. This function is expected to be used by virtual keyboards. When key(s) on a virtual keyboard is pressed by a user, this function is used to propagate that event to the system.

Parameters

  • parameters
    object
    • contextID
      number

      ID of the context where the key events will be sent, or zero to send key events to non-input field.

    • keyData

      Data on the key event.

  • callback
    function

    Called when the operation completes.

    The callback parameter should be a function that looks like this:

    () => {...}

setAssistiveWindowButtonHighlighted

input.ime.setAssistiveWindowButtonHighlighted(parameters: object, callback: function)

Since Chrome 86.

Highlights/Unhighlights a button in an assistive window.

Parameters

  • parameters
    object
    • announceString
      string optional

      The text for the screenreader to announce.

    • The ID of the button

    • contextID
      number

      ID of the context owning the assistive window.

    • highlighted
      boolean

      Whether the button should be highlighted.

    • The window type the button belongs to.

  • callback
    function

    Called when the operation completes. On failure, runtime.lastError is set.

    The callback parameter should be a function that looks like this:

    () => {...}

setAssistiveWindowProperties

input.ime.setAssistiveWindowProperties(parameters: object, callback: function)

Since Chrome 85.

Shows/Hides an assistive window with the given properties.

Parameters

  • parameters
    object
    • contextID
      number

      ID of the context owning the assistive window.

    • Properties of the assistive window.

  • callback
    function

    Called when the operation completes.

    The callback parameter should be a function that looks like this:

    (success: boolean) => {...}
    • success
      boolean

setCandidateWindowProperties

input.ime.setCandidateWindowProperties(parameters: object, callback: function)

Sets the properties of the candidate window. This fails if the extension doesn't own the active IME

Parameters

  • parameters
    object
    • engineID
      string

      ID of the engine to set properties on.

    • properties
      object
      • auxiliaryText
        string optional

        Text that is shown at the bottom of the candidate window.

      • auxiliaryTextVisible
        boolean optional

        True to display the auxiliary text, false to hide it.

      • currentCandidateIndex
        number optional

        Since Chrome 84.

        The index of the current chosen candidate out of total candidates.

      • cursorVisible
        boolean optional

        True to show the cursor, false to hide it.

      • pageSize
        number optional

        The number of candidates to display per page.

      • totalCandidates
        number optional

        Since Chrome 84.

        The total number of candidates for the candidate window.

      • vertical
        boolean optional

        True if the candidate window should be rendered vertical, false to make it horizontal.

      • visible
        boolean optional

        True to show the Candidate window, false to hide it.

      • windowPosition

        Where to display the candidate window.

  • callback
    function

    Called when the operation completes.

    The callback parameter should be a function that looks like this:

    (success: boolean) => {...}
    • success
      boolean

setCandidates

input.ime.setCandidates(parameters: object, callback: function)

Sets the current candidate list. This fails if this extension doesn't own the active IME

Parameters

  • parameters
    object
    • candidates
      object[]

      List of candidates to show in the candidate window

      • annotation
        string optional

        Additional text describing the candidate

      • candidate
        string

        The candidate

      • id
        number

        The candidate's id

      • label
        string optional

        Short string displayed to next to the candidate, often the shortcut key or index

      • parentId
        number optional

        The id to add these candidates under

      • usage
        object optional

        The usage or detail description of word.

        • body
          string

          The body string of detail description.

        • title
          string

          The title string of details description.

    • contextID
      number

      ID of the context that owns the candidate window.

  • callback
    function

    Called when the operation completes.

    The callback parameter should be a function that looks like this:

    (success: boolean) => {...}
    • success
      boolean

setComposition

input.ime.setComposition(parameters: object, callback: function)

Set the current composition. If this extension does not own the active IME, this fails.

Parameters

  • parameters
    object
    • contextID
      number

      ID of the context where the composition text will be set

    • cursor
      number

      Position in the text of the cursor.

    • segments
      object[] optional

      List of segments and their associated types.

      • end
        number

        Index of the character to end this segment after.

      • start
        number

        Index of the character to start this segment at

      • The type of the underline to modify this segment.

    • selectionEnd
      number optional

      Position in the text that the selection ends at.

    • selectionStart
      number optional

      Position in the text that the selection starts at.

    • text
      string

      Text to set

  • callback
    function

    Called when the operation completes with a boolean indicating if the text was accepted or not. On failure, runtime.lastError is set.

    The callback parameter should be a function that looks like this:

    (success: boolean) => {...}
    • success
      boolean

setCursorPosition

input.ime.setCursorPosition(parameters: object, callback: function)

Set the position of the cursor in the candidate window. This is a no-op if this extension does not own the active IME.

Parameters

  • parameters
    object
    • candidateID
      number

      ID of the candidate to select.

    • contextID
      number

      ID of the context that owns the candidate window.

  • callback
    function

    Called when the operation completes

    The callback parameter should be a function that looks like this:

    (success: boolean) => {...}
    • success
      boolean

setMenuItems

input.ime.setMenuItems(parameters: MenuParameters, callback: function)

Adds the provided menu items to the language menu when this IME is active.

Parameters

  • parameters
  • callback
    function

    The callback parameter should be a function that looks like this:

    () => {...}

updateMenuItems

input.ime.updateMenuItems(parameters: MenuParameters, callback: function)

Updates the state of the MenuItems specified

Parameters

  • parameters
  • callback
    function

    Called when the operation completes

    The callback parameter should be a function that looks like this:

    () => {...}

Events

onActivate

input.ime.onActivate.addListener(listener: function)

This event is sent when an IME is activated. It signals that the IME will be receiving onKeyPress events.

Event

  • listener
    function

    The listener parameter should be a function that looks like this:

    (engineID: string, screen: ScreenType) => {...}
    • engineID
      string

      ID of the engine receiving the event

    • The screen type under which the IME is activated.

onAssistiveWindowButtonClicked

input.ime.onAssistiveWindowButtonClicked.addListener(listener: function)

Since Chrome 85.

This event is sent when a button in an assistive window is clicked.

Event

  • listener
    function

    The listener parameter should be a function that looks like this:

    (details: object) => {...}

onBlur

input.ime.onBlur.addListener(listener: function)

This event is sent when focus leaves a text box. It is sent to all extensions that are listening to this event, and enabled by the user.

Event

  • listener
    function

    The listener parameter should be a function that looks like this:

    (contextID: number) => {...}
    • contextID
      number

      The ID of the text field that has lost focus. The ID is invalid after this call

onCandidateClicked

input.ime.onCandidateClicked.addListener(listener: function)

This event is sent if this extension owns the active IME.

Event

  • listener
    function

    The listener parameter should be a function that looks like this:

    (engineID: string, candidateID: number, button: MouseButton) => {...}
    • engineID
      string

      ID of the engine receiving the event

    • candidateID
      number

      ID of the candidate that was clicked.

    • Which mouse buttons was clicked.

onDeactivated

input.ime.onDeactivated.addListener(listener: function)

This event is sent when an IME is deactivated. It signals that the IME will no longer be receiving onKeyPress events.

Event

  • listener
    function

    The listener parameter should be a function that looks like this:

    (engineID: string) => {...}
    • engineID
      string

      ID of the engine receiving the event

onFocus

input.ime.onFocus.addListener(listener: function)

This event is sent when focus enters a text box. It is sent to all extensions that are listening to this event, and enabled by the user.

Event

  • listener
    function

    The listener parameter should be a function that looks like this:

    (context: InputContext) => {...}
    • Describes the text field that has acquired focus.

onInputContextUpdate

input.ime.onInputContextUpdate.addListener(listener: function)

This event is sent when the properties of the current InputContext change, such as the the type. It is sent to all extensions that are listening to this event, and enabled by the user.

Event

  • listener
    function

    The listener parameter should be a function that looks like this:

    (context: InputContext) => {...}
    • An InputContext object describing the text field that has changed.

onKeyEvent

input.ime.onKeyEvent.addListener(listener: function)

Fired when a key event is sent from the operating system. The event will be sent to the extension if this extension owns the active IME. The listener function should return true if the event was handled false if it was not. If the event will be evaluated asynchronously, this function must return undefined and the IME must later call keyEventHandled() with the result.

Event

  • listener
    function

    The listener parameter should be a function that looks like this:

    (engineID: string, keyData: KeyboardEvent, requestId: string) => {...}
    • engineID
      string

      ID of the engine receiving the event

    • Data on the key event

    • requestId
      string

      Since Chrome 79.

      ID of the request. If the event listener returns undefined, then keyEventHandled must be called later with this requestId.

onMenuItemActivated

input.ime.onMenuItemActivated.addListener(listener: function)

Called when the user selects a menu item

Event

  • listener
    function

    The listener parameter should be a function that looks like this:

    (engineID: string, name: string) => {...}
    • engineID
      string

      ID of the engine receiving the event

    • name
      string

      Name of the MenuItem which was activated

onReset

input.ime.onReset.addListener(listener: function)

This event is sent when chrome terminates ongoing text input session.

Event

  • listener
    function

    The listener parameter should be a function that looks like this:

    (engineID: string) => {...}
    • engineID
      string

      ID of the engine receiving the event

onSurroundingTextChanged

input.ime.onSurroundingTextChanged.addListener(listener: function)

Called when the editable string around caret is changed or when the caret position is moved. The text length is limited to 100 characters for each back and forth direction.

Event

  • listener
    function

    The listener parameter should be a function that looks like this:

    (engineID: string, surroundingInfo: object) => {...}
    • engineID
      string

      ID of the engine receiving the event

    • surroundingInfo
      object

      The surrounding information.

      • anchor
        number

        The beginning position of the selection. This value indicates caret position if there is no selection.

      • focus
        number

        The ending position of the selection. This value indicates caret position if there is no selection.

      • offset
        number

        Since Chrome 46.

        The offset position of text. Since text only includes a subset of text around the cursor, offset indicates the absolute position of the first character of text.

      • text
        string

        The text around the cursor. This is only a subset of all text in the input field.

We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.