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
inputYou must declare the "input" permission in the extension manifest to use the input.ime API. For example:
{
  "name": "My extension",
  ...
  "permissions": [
    "input"
  ],
  ...
}
Availability
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;
    }
  }
);
Types
AssistiveWindowButton
ID of buttons in assistive window.
Enum
"undo"  "addToDictionary" 
 
 
AssistiveWindowProperties
Properties of the assistive window.
Properties
- 
    announceStringstring optional Strings for ChromeVox to announce. 
- 
    type"undo" 
 
- 
    visibleboolean Sets true to show AssistiveWindow, sets false to hide. 
AssistiveWindowType
Type of assistive window.
Value
"undo" 
 
AutoCapitalizeType
The auto-capitalize type of the text field.
Enum
"characters"  "words"  "sentences" 
 
 
 
InputContext
Describes an input Context
Properties
- 
    autoCapitalizeChrome 69+The auto-capitalize type of the text field. 
- 
    autoCompleteboolean Whether the text field wants auto-complete. 
- 
    autoCorrectboolean Whether the text field wants auto-correct. 
- 
    contextIDnumber This is used to specify targets of text field operations. This ID becomes invalid as soon as onBlur is called. 
- 
    shouldDoLearningboolean Chrome 68+Whether text entered into the text field should be used to improve typing suggestions for the user. 
- 
    spellCheckboolean Whether the text field wants spell-check. 
- 
    typeType of value this text field edits, (Text, Number, URL, etc) 
InputContextType
Type of value this text field edits, (Text, Number, URL, etc)
Enum
"text"  "search"  "tel"  "url"  "email"  "number"  "password"  "null" 
 
 
 
 
 
 
 
 
KeyboardEvent
See http://www.w3.org/TR/DOM-Level-3-Events/#events-KeyboardEvent
Properties
- 
    altKeyboolean optional Whether or not the ALT key is pressed. 
- 
    altgrKeyboolean optional Chrome 79+Whether or not the ALTGR key is pressed. 
- 
    capsLockboolean optional Whether or not the CAPS_LOCK is enabled. 
- 
    codestring Value of the physical key being pressed. The value is not affected by current keyboard layout or modifier state. 
- 
    ctrlKeyboolean optional Whether or not the CTRL key is pressed. 
- 
    extensionIdstring optional The extension ID of the sender of this keyevent. 
- 
    keystring Value of the key being pressed 
- 
    keyCodenumber optional The deprecated HTML keyCode, which is system- and implementation-dependent numerical code signifying the unmodified identifier associated with the key pressed. 
- 
    requestIdstring optional (Deprecated) The ID of the request. Use the requestIdparam from theonKeyEventevent instead.
- 
    shiftKeyboolean optional Whether or not the SHIFT key is pressed. 
- 
    typeOne of keyup or keydown. 
KeyboardEventType
Enum
"keyup"  "keydown" 
 
 
MenuItem
A menu item used by an input method to interact with the user from the language menu.
Properties
- 
    checkedboolean optional Indicates this item should be drawn with a check. 
- 
    enabledboolean optional Indicates this item is enabled. 
- 
    idstring String that will be passed to callbacks referencing this MenuItem. 
- 
    labelstring optional Text displayed in the menu for this item. 
- 
    styleMenuItemStyle optional The type of menu item. 
- 
    visibleboolean optional Indicates this item is visible. 
MenuItemStyle
The type of menu item. Radio buttons between separators are considered grouped.
Enum
"check"  "radio"  "separator" 
 
 
 
MenuParameters
Properties
- 
    engineIDstring ID of the engine to use. 
- 
    itemsMenuItem[] MenuItems to add or update. They will be added in the order they exist in the array. 
MouseButton
Which mouse buttons was clicked.
Enum
"left"  "middle"  "right" 
 
 
 
ScreenType
The screen type under which the IME is activated.
Enum
"normal"  "login"  "lock"  "secondary-login" 
 
 
 
 
UnderlineStyle
The type of the underline to modify this segment.
Enum
"underline"  "doubleUnderline"  "noUnderline" 
 
 
 
WindowPosition
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"  "composition" 
 
 
Methods
clearComposition()
chrome.input.ime.clearComposition(
parameters: object,
): Promise<boolean>
Clear the current composition. If this extension does not own the active IME, this fails.
Parameters
- 
    parametersobject - 
    contextIDnumber ID of the context where the composition will be cleared 
 
- 
    
Returns
- 
            Promise<boolean> Chrome 111+
commitText()
chrome.input.ime.commitText(
parameters: object,
): Promise<boolean>
Commits the provided text to the current input.
Parameters
- 
    parametersobject - 
    contextIDnumber ID of the context where the text will be committed 
- 
    textstring The text to commit 
 
- 
    
Returns
- 
            Promise<boolean> Chrome 111+
deleteSurroundingText()
chrome.input.ime.deleteSurroundingText(
parameters: object,
): Promise<void>
Deletes the text around the caret.
Parameters
- 
    parametersobject - 
    contextIDnumber ID of the context where the surrounding text will be deleted. 
- 
    engineIDstring ID of the engine receiving the event. 
- 
    lengthnumber The number of characters to be deleted 
- 
    offsetnumber The offset from the caret position where deletion will start. This value can be negative. 
 
- 
    
Returns
- 
            Promise<void> Chrome 111+
hideInputView()
chrome.input.ime.hideInputView(): void
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()
chrome.input.ime.keyEventHandled(
requestId: string,
response: boolean,
): void
Indicates that the key event received by onKeyEvent is handled. This should only be called if the onKeyEvent listener is asynchronous.
Parameters
- 
    requestIdstring Request id of the event that was handled. This should come from keyEvent.requestId 
- 
    responseboolean True if the keystroke was handled, false if not 
sendKeyEvents()
chrome.input.ime.sendKeyEvents(
parameters: object,
): Promise<void>
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
- 
    parametersobject - 
    contextIDnumber ID of the context where the key events will be sent, or zero to send key events to non-input field. 
- 
    keyDataData on the key event. 
 
- 
    
Returns
- 
            Promise<void> Chrome 111+
setAssistiveWindowButtonHighlighted()
chrome.input.ime.setAssistiveWindowButtonHighlighted(
parameters: object,
): Promise<void>
Highlights/Unhighlights a button in an assistive window.
Parameters
- 
    parametersobject - 
    announceStringstring optional The text for the screenreader to announce. 
- 
    buttonIDThe ID of the button 
- 
    contextIDnumber ID of the context owning the assistive window. 
- 
    highlightedboolean Whether the button should be highlighted. 
- 
    windowType"undo" 
 The window type the button belongs to. 
 
- 
    
Returns
- 
            Promise<void> Chrome 111+
setAssistiveWindowProperties()
chrome.input.ime.setAssistiveWindowProperties(
parameters: object,
): Promise<boolean>
Shows/Hides an assistive window with the given properties.
Parameters
- 
    parametersobject - 
    contextIDnumber ID of the context owning the assistive window. 
- 
    propertiesProperties of the assistive window. 
 
- 
    
Returns
- 
            Promise<boolean> Chrome 111+
setCandidates()
chrome.input.ime.setCandidates(
parameters: object,
): Promise<boolean>
Sets the current candidate list. This fails if this extension doesn't own the active IME
Parameters
- 
    parametersobject - 
    candidatesobject[] List of candidates to show in the candidate window - 
    annotationstring optional Additional text describing the candidate 
- 
    candidatestring The candidate 
- 
    idnumber The candidate's id 
- 
    labelstring optional Short string displayed to next to the candidate, often the shortcut key or index 
- 
    parentIdnumber optional The id to add these candidates under 
- 
    usageobject optional The usage or detail description of word. - 
    bodystring The body string of detail description. 
- 
    titlestring The title string of details description. 
 
- 
    
 
- 
    
- 
    contextIDnumber ID of the context that owns the candidate window. 
 
- 
    
Returns
- 
            Promise<boolean> Chrome 111+
setCandidateWindowProperties()
chrome.input.ime.setCandidateWindowProperties(
parameters: object,
): Promise<boolean>
Sets the properties of the candidate window. This fails if the extension doesn't own the active IME
Parameters
- 
    parametersobject - 
    engineIDstring ID of the engine to set properties on. 
- 
    propertiesobject - 
    auxiliaryTextstring optional Text that is shown at the bottom of the candidate window. 
- 
    auxiliaryTextVisibleboolean optional True to display the auxiliary text, false to hide it. 
- 
    currentCandidateIndexnumber optional Chrome 84+The index of the current chosen candidate out of total candidates. 
- 
    cursorVisibleboolean optional True to show the cursor, false to hide it. 
- 
    pageSizenumber optional The number of candidates to display per page. 
- 
    totalCandidatesnumber optional Chrome 84+The total number of candidates for the candidate window. 
- 
    verticalboolean optional True if the candidate window should be rendered vertical, false to make it horizontal. 
- 
    visibleboolean optional True to show the Candidate window, false to hide it. 
- 
    windowPositionWindowPosition optional Where to display the candidate window. 
 
- 
    
 
- 
    
Returns
- 
            Promise<boolean> Chrome 111+
setComposition()
chrome.input.ime.setComposition(
parameters: object,
): Promise<boolean>
Set the current composition. If this extension does not own the active IME, this fails.
Parameters
- 
    parametersobject - 
    contextIDnumber ID of the context where the composition text will be set 
- 
    cursornumber Position in the text of the cursor. 
- 
    segmentsobject[] optional List of segments and their associated types. - 
    endnumber Index of the character to end this segment after. 
- 
    startnumber Index of the character to start this segment at 
- 
    styleThe type of the underline to modify this segment. 
 
- 
    
- 
    selectionEndnumber optional Position in the text that the selection ends at. 
- 
    selectionStartnumber optional Position in the text that the selection starts at. 
- 
    textstring Text to set 
 
- 
    
Returns
- 
            Promise<boolean> Chrome 111+
setCursorPosition()
chrome.input.ime.setCursorPosition(
parameters: object,
): Promise<boolean>
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
- 
    parametersobject - 
    candidateIDnumber ID of the candidate to select. 
- 
    contextIDnumber ID of the context that owns the candidate window. 
 
- 
    
Returns
- 
            Promise<boolean> Chrome 111+
setMenuItems()
chrome.input.ime.setMenuItems(
parameters: MenuParameters,
): Promise<void>
Adds the provided menu items to the language menu when this IME is active.
Parameters
- 
    parameters
Returns
- 
            Promise<void> Chrome 111+
updateMenuItems()
chrome.input.ime.updateMenuItems(
parameters: MenuParameters,
): Promise<void>
Updates the state of the MenuItems specified
Parameters
- 
    parameters
Returns
- 
            Promise<void> Chrome 111+
Events
onActivate
chrome.input.ime.onActivate.addListener(
callback: function,
)
This event is sent when an IME is activated. It signals that the IME will be receiving onKeyPress events.
Parameters
- 
    callbackfunction The callbackparameter looks like:(engineID: string, screen: ScreenType) => void - 
    engineIDstring 
- 
    screen
 
- 
    
onAssistiveWindowButtonClicked
chrome.input.ime.onAssistiveWindowButtonClicked.addListener(
callback: function,
)
This event is sent when a button in an assistive window is clicked.
Parameters
- 
    callbackfunction The callbackparameter looks like:(details: object) => void - 
    detailsobject - 
    buttonIDThe ID of the button clicked. 
- 
    windowTypeThe type of the assistive window. 
 
- 
    
 
- 
    
onBlur
chrome.input.ime.onBlur.addListener(
callback: 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.
Parameters
- 
    callbackfunction The callbackparameter looks like:(contextID: number) => void - 
    contextIDnumber 
 
- 
    
onCandidateClicked
chrome.input.ime.onCandidateClicked.addListener(
callback: function,
)
This event is sent if this extension owns the active IME.
Parameters
- 
    callbackfunction The callbackparameter looks like:(engineID: string, candidateID: number, button: MouseButton) => void - 
    engineIDstring 
- 
    candidateIDnumber 
- 
    button
 
- 
    
onDeactivated
chrome.input.ime.onDeactivated.addListener(
callback: function,
)
This event is sent when an IME is deactivated. It signals that the IME will no longer be receiving onKeyPress events.
Parameters
- 
    callbackfunction The callbackparameter looks like:(engineID: string) => void - 
    engineIDstring 
 
- 
    
onFocus
chrome.input.ime.onFocus.addListener(
callback: 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.
Parameters
- 
    callbackfunction The callbackparameter looks like:(context: InputContext) => void - 
    context
 
- 
    
onInputContextUpdate
chrome.input.ime.onInputContextUpdate.addListener(
callback: 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.
Parameters
- 
    callbackfunction The callbackparameter looks like:(context: InputContext) => void - 
    context
 
- 
    
onKeyEvent
chrome.input.ime.onKeyEvent.addListener(
callback: 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.
Parameters
- 
    callbackfunction The callbackparameter looks like:(engineID: string, keyData: KeyboardEvent, requestId: string) => boolean | undefined - 
    engineIDstring 
- 
    keyData
- 
    requestIdstring 
 - 
            returnsboolean | undefined 
 
- 
    
onMenuItemActivated
chrome.input.ime.onMenuItemActivated.addListener(
callback: function,
)
Called when the user selects a menu item
Parameters
- 
    callbackfunction The callbackparameter looks like:(engineID: string, name: string) => void - 
    engineIDstring 
- 
    namestring 
 
- 
    
onReset
chrome.input.ime.onReset.addListener(
callback: function,
)
This event is sent when chrome terminates ongoing text input session.
Parameters
- 
    callbackfunction The callbackparameter looks like:(engineID: string) => void - 
    engineIDstring 
 
- 
    
onSurroundingTextChanged
chrome.input.ime.onSurroundingTextChanged.addListener(
callback: 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.
Parameters
- 
    callbackfunction The callbackparameter looks like:(engineID: string, surroundingInfo: object) => void - 
    engineIDstring 
- 
    surroundingInfoobject - 
    anchornumber The beginning position of the selection. This value indicates caret position if there is no selection. 
- 
    focusnumber The ending position of the selection. This value indicates caret position if there is no selection. 
- 
    offsetnumber Chrome 46+The offset position of text. Sincetextonly includes a subset of text around the cursor, offset indicates the absolute position of the first character oftext.
- 
    textstring The text around the cursor. This is only a subset of all text in the input field. 
 
- 
    
 
-