harmony 鸿蒙@ohos.inputMethodEngine (Input Method Service)

2022-08-09 浏览 (821)

@ohos.inputMethodEngine (Input Method Service)

The inputMethodEngine module is oriented to input method applications (including system and third-party input method applications). With the APIs of this module, input method applications are able to create soft keyboard windows, insert or delete characters, select text, and listen for physical keyboard events.

NOTE

The initial APIs of this module are supported since API version 8. Newly added APIs will be marked with a superscript to indicate their earliest API version.

Modules to Import

import inputMethodEngine from '@ohos.inputMethodEngine';

Constants

Provides the constant values of function keys, edit boxes, and the cursor.

System capability: SystemCapability.MiscServices.InputMethodFramework

NameTypeValueDescription
ENTER_KEY_TYPE_UNSPECIFIEDnumber0No function is specified for the Enter key.
ENTER_KEY_TYPE_GOnumber2The Enter key takes the user to the target.
ENTER_KEY_TYPE_SEARCHnumber3The Enter key takes the user to the results of their searching.
ENTER_KEY_TYPE_SENDnumber4The Enter key sends the text to its target.
ENTER_KEY_TYPE_NEXTnumber5The Enter key takes the user to the next field.
ENTER_KEY_TYPE_DONEnumber6The Enter key takes the user to the next line.
ENTER_KEY_TYPE_PREVIOUSnumber7The Enter key takes the user to the previous field.
PATTERN_NULLnumber-1Any type of edit box.
PATTERN_TEXTnumber0Text edit box.
PATTERN_NUMBERnumber2Number edit box.
PATTERN_PHONEnumber3Phone number edit box.
PATTERN_DATETIMEnumber4Date edit box.
PATTERN_EMAILnumber5Email edit box.
PATTERN_URInumber6URI edit box.
PATTERN_PASSWORDnumber7Password edit box.
OPTION_ASCIInumber20ASCII values are allowed.
OPTION_NONEnumber0No input attribute is specified.
OPTION_AUTO_CAP_CHARACTERSnumber2Characters are allowed.
OPTION_AUTO_CAP_SENTENCESnumber8Sentences are allowed.
OPTION_AUTO_WORDSnumber4Words are allowed.
OPTION_MULTI_LINEnumber1Multiple lines are allowed.
OPTION_NO_FULLSCREENnumber10Half-screen style.
FLAG_SELECTINGnumber2The edit box is being selected.
FLAG_SINGLE_LINEnumber1The edit box allows only single-line input.
DISPLAY_MODE_PARTnumber0The edit box is displayed in half-screen mode.
DISPLAY_MODE_FULLnumber1The edit box is displayed in full screen.
CURSOR_UP9+number1The caret moves upward.
CURSOR_DOWN9+number2The caret moves downward.
CURSOR_LEFT9+number3The caret moves leftward.
CURSOR_RIGHT9+number4The caret moves rightward.
WINDOW_TYPE_INPUT_METHOD_FLOAT9+number2105The input method is displayed in a floating window.

inputMethodEngine.getInputMethodAbility9+

getInputMethodAbility(): InputMethodAbility

Obtains an InputMethodAbility instance for the input method. This API can be called only by an input method.

The input method can use the obtained instance to subscribe to a soft keyboard display/hide request event, create/destroy an input method panel, and the like.

System capability: SystemCapability.MiscServices.InputMethodFramework

Return value

TypeDescription
InputMethodAbilityInputMethodAbility instance.

Example

let InputMethodAbility = inputMethodEngine.getInputMethodAbility();

inputMethodEngine.getKeyboardDelegate9+

getKeyboardDelegate(): KeyboardDelegate

Obtains a KeyboardDelegate instance for the input method.

The input method can use the obtained instance to subscribe to a physical keyboard event, text selection change event, and more.

System capability: SystemCapability.MiscServices.InputMethodFramework

Return value

TypeDescription
KeyboardDelegateKeyboardDelegate instance.

Example

let KeyboardDelegate = inputMethodEngine.getKeyboardDelegate();

inputMethodEngine.getInputMethodEngine(deprecated)

getInputMethodEngine(): InputMethodEngine

Obtains an InputMethodEngine instance for the input method.

The input method can use the obtained instance to subscribe to a soft keyboard display/hide request event.

NOTE

This API is supported since API version 8 and deprecated since API version 9. You are advised to use getInputMethodAbility() instead.

System capability: SystemCapability.MiscServices.InputMethodFramework

Return value

TypeDescription
InputMethodEngineInputMethodAbility instance.

Example

let InputMethodEngine = inputMethodEngine.getInputMethodEngine();

inputMethodEngine.createKeyboardDelegate(deprecated)

createKeyboardDelegate(): KeyboardDelegate

Obtains a KeyboardDelegate instance for the input method.

The input method can use the obtained instance to subscribe to a physical keyboard event, text selection change event, and more.

NOTE

This API is supported since API version 8 and deprecated since API version 9. You are advised to use getKeyboardDelegate() instead.

System capability: SystemCapability.MiscServices.InputMethodFramework

Return value

TypeDescription
KeyboardDelegateKeyboardDelegate instance.

Example

let keyboardDelegate = inputMethodEngine.createKeyboardDelegate();

InputMethodEngine

In the following API examples, you must first use getInputMethodEngine to obtain an InputMethodEngine instance, and then call the APIs using the obtained instance.

on('inputStart')

on(type: 'inputStart', callback: (kbController: KeyboardController, textInputClient: TextInputClient) => void): void

Enables listening for the input method binding event. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.MiscServices.InputMethodFramework

Parameters

NameTypeMandatoryDescription
typestringYesListening type. The value is fixed at 'inputStart'.
callback(kbController: KeyboardController, textInputClient: TextInputClient) => voidYesCallback used to return the KeyboardController and TextInputClient instances.

Example

inputMethodEngine.getInputMethodEngine()
  .on('inputStart', (kbController: inputMethodEngine.KeyboardController, textClient: inputMethodEngine.TextInputClient) => {
    let keyboardController = kbController;
    let textInputClient = textClient;
});

off('inputStart')

off(type: 'inputStart', callback?: (kbController: KeyboardController, textInputClient: TextInputClient) => void): void

Disables listening for the input method binding event.

System capability: SystemCapability.MiscServices.InputMethodFramework

Parameters

NameTypeMandatoryDescription
typestringYesListening type. The value is fixed at 'inputStart'.
callback(kbController: KeyboardController, textInputClient: TextInputClient) => voidNoCallback for which listening is disabled. If this parameter is not specified, listening will be disabled for all callbacks corresponding to the specified type.

Example

inputMethodEngine.getInputMethodEngine()
  .off('inputStart', (kbController: inputMethodEngine.KeyboardController, textClient: inputMethodEngine.TextInputClient) => {
    console.log('delete inputStart notification.');
});

on('keyboardShow'|'keyboardHide')

on(type: 'keyboardShow'|'keyboardHide', callback: () => void): void

Enables listening for a keyboard visibility event. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.MiscServices.InputMethodFramework

Parameters

NameTypeMandatoryDescription
typestringYesListening type.
- The value 'keyboardShow' indicates the keyboard display event.
- The value 'keyboardHide' indicates the keyboard hiding event.
callback() => voidYesCallback used to return the result.

Example

inputMethodEngine.getInputMethodEngine().on('keyboardShow', () => {
  console.log('inputMethodEngine keyboardShow.');
});
inputMethodEngine.getInputMethodEngine().on('keyboardHide', () => {
  console.log('inputMethodEngine keyboardHide.');
});

off('keyboardShow'|'keyboardHide')

off(type: 'keyboardShow'|'keyboardHide', callback?: () => void): void

Disables listening for a keyboard visibility event. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.MiscServices.InputMethodFramework

Parameters

NameTypeMandatoryDescription
typestringYesListening type.
- The value 'keyboardShow' indicates the keyboard display event.
- The value 'keyboardHide' indicates the keyboard hiding event.
callback() => voidNoCallback for which listening is disabled. If this parameter is not specified, listening will be disabled for all callbacks corresponding to the specified type.

Example

inputMethodEngine.getInputMethodEngine().off('keyboardShow');
inputMethodEngine.getInputMethodEngine().off('keyboardHide');

InputMethodAbility

In the following API examples, you must first use getInputMethodAbility to obtain an InputMethodAbility instance, and then call the APIs using the obtained instance.

on('inputStart')9+

on(type: 'inputStart', callback: (kbController: KeyboardController, inputClient: InputClient) => void): void

Enables listening for the input method binding event. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.MiscServices.InputMethodFramework

Parameters

NameTypeMandatoryDescription
typestringYesListening type. The value is fixed at 'inputStart'.
callback(kbController: KeyboardController, inputClient: InputClient) => voidYesCallback used to return the result.

Example

inputMethodEngine.getInputMethodAbility()
  .on('inputStart', (kbController: inputMethodEngine.KeyboardController, client: inputMethodEngine.InputClient) => {
    let keyboardController = kbController;
    let inputClient = client;
});

off('inputStart')9+

off(type: 'inputStart', callback?: (kbController: KeyboardController, inputClient: InputClient) => void): void

Disables listening for the input method binding event. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.MiscServices.InputMethodFramework

Parameters

NameTypeMandatoryDescription
typestringYesListening type. The value is fixed at 'inputStart'.
callback(kbController: KeyboardController, inputClient: InputClient) => voidNoCallback for which listening is disabled. If this parameter is not specified, listening will be disabled for all callbacks corresponding to the specified type.

Example

inputMethodEngine.getInputMethodAbility().off('inputStart');

on('inputStop')9+

on(type: 'inputStop', callback: () => void): void

Enables listening for the input method unbinding event. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.MiscServices.InputMethodFramework

Parameters

NameTypeMandatoryDescription
typestringYesListening type. The value is fixed at 'inputStop'.
callback() => voidYesCallback used to return the result.

Example

inputMethodEngine.getInputMethodAbility().on('inputStop', () => {
  console.log('inputMethodAbility inputStop');
});

off('inputStop')9+

off(type: 'inputStop', callback: () => void): void

Disables listening for the input method stop event. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.MiscServices.InputMethodFramework

Parameters

NameTypeMandatoryDescription
typestringYesListening type. The value is fixed at 'inputStop'.
callback() => voidYesCallback for which listening is disabled. If this parameter is not specified, listening will be disabled for all callbacks corresponding to the specified type.

Example

inputMethodEngine.getInputMethodAbility().off('inputStop', () => {
  console.log('inputMethodAbility delete inputStop notification.');
});

on('setCallingWindow')9+

on(type: 'setCallingWindow', callback: (wid: number) => void): void

Enables listening for the window invocation setting event. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.MiscServices.InputMethodFramework

Parameters

NameTypeMandatoryDescription
typestringYesListening type. The value is fixed at 'setCallingWindow'.
callback(wid: number) => voidYesCallback used to return the window ID of the caller.

Example

inputMethodEngine.getInputMethodAbility().on('setCallingWindow', (wid: number) => {
  console.log('inputMethodAbility setCallingWindow');
});

off('setCallingWindow')9+

off(type: 'setCallingWindow', callback: (wid:number) => void): void

Disables listening for the window invocation setting event. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.MiscServices.InputMethodFramework

Parameters

NameTypeMandatoryDescription
typestringYesListening type. The value is fixed at 'setCallingWindow'.
callback(wid:number) => voidYesCallback for which listening is disabled. If this parameter is not specified, listening will be disabled for all callbacks corresponding to the specified type.

Example

inputMethodEngine.getInputMethodAbility().off('setCallingWindow', (wid: number) => {
  console.log('inputMethodAbility delete setCallingWindow notification.');
});

on('keyboardShow'|'keyboardHide')9+

on(type: 'keyboardShow'|'keyboardHide', callback: () => void): void

Enables listening for a keyboard visibility event. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.MiscServices.InputMethodFramework

Parameters

NameTypeMandatoryDescription
typestringYesListening type.
- The value 'keyboardShow' indicates the keyboard display event.
- The value 'keyboardHide' indicates the keyboard hiding event.
callback() => voidYesCallback used to return the result.

Example

inputMethodEngine.getInputMethodAbility().on('keyboardShow', () => {
  console.log('InputMethodAbility keyboardShow.');
});
inputMethodEngine.getInputMethodAbility().on('keyboardHide', () => {
  console.log('InputMethodAbility keyboardHide.');
});

off('keyboardShow'|'keyboardHide')9+

off(type: 'keyboardShow'|'keyboardHide', callback?: () => void): void

Disables listening for a keyboard visibility event. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.MiscServices.InputMethodFramework

Parameters

NameTypeMandatoryDescription
typestringYesListening type.
- The value 'keyboardShow' indicates the keyboard display event.
- The value 'keyboardHide' indicates the keyboard hiding event.
callback() => voidNoCallback used to return the result.

Example

inputMethodEngine.getInputMethodAbility().off('keyboardShow', () => {
  console.log('InputMethodAbility delete keyboardShow notification.');
});
inputMethodEngine.getInputMethodAbility().off('keyboardHide', () => {
  console.log('InputMethodAbility delete keyboardHide notification.');
});

on('setSubtype')9+

on(type: 'setSubtype', callback: (inputMethodSubtype: InputMethodSubtype) => void): void

Enables listening for the input method subtype setting event. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.MiscServices.InputMethodFramework

Parameters

NameTypeMandatoryDescription
typestringYesListening type. The value is fixed at 'setSubtype'.
callback(inputMethodSubtype: InputMethodSubtype) => voidYesCallback used to return the input method subtype.

Example

inputMethodEngine.getInputMethodAbility().on('setSubtype', (inputMethodSubtype: InputMethodSubtype) => {
  console.log('InputMethodAbility setSubtype.');
});

off('setSubtype')9+

off(type: 'setSubtype', callback?: (inputMethodSubtype: InputMethodSubtype) => void): void

Disables listening for a keyboard visibility event. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.MiscServices.InputMethodFramework

Parameters

NameTypeMandatoryDescription
typestringYesListening type. The value is fixed at 'setSubtype'.
callback(inputMethodSubtype: InputMethodSubtype) => voidNoCallback for which listening is disabled. If this parameter is not specified, listening will be disabled for all callbacks corresponding to the specified type.

Example

inputMethodEngine.getInputMethodAbility().off('setSubtype', () => {
  console.log('InputMethodAbility delete setSubtype notification.');
});

createPanel10+

createPanel(ctx: BaseContext, info: PanelInfo, callback: AsyncCallback<Panel>): void

Creates an input method panel. This API uses an asynchronous callback to return the result.

This API can be called only by an input method. Only one SOFT_KEYBOARD panel and one STATUS_BAR panel can be created for a single input method.

System capability: SystemCapability.MiscServices.InputMethodFramework

Parameters

NameTypeMandatoryDescription
ctxBaseContextYesCurrent context of the input method.
infoPanelInfoYesInformation about the input method panel.
callbackAsyncCallback<Panel>YesCallback used to return the result. If the operation is successful, the created input method panel is returned.

Error codes

IDError Message
12800004not an input method extension.

Example

let panelInfo: inputMethodEngine.PanelInfo = {
  type: inputMethodEngine.PanelType.SOFT_KEYBOARD,
  flag: inputMethodEngine.PanelFlag.FLG_FIXED
}
try {
  inputMethodEngine.getInputMethodAbility()
    .createPanel(this.context, panelInfo, (err: BusinessError, panel: inputMethodEngine.Panel) => {
      if (err) {
        console.error(`Failed to createPanel: ${JSON.stringify(err)}`);
        return;
      }
      console.log('Succeed in creating panel.');
    })
} catch (err) {
  console.error(`Failed to createPanel: ${JSON.stringify(err)}`);
}

createPanel10+

createPanel(ctx: BaseContext, info: PanelInfo): Promise<Panel>

Creates an input method panel. This API uses a promise to return the result.

This API can be called only by an input method. Only one SOFT_KEYBOARD panel and one STATUS_BAR panel can be created for a single input method.

System capability: SystemCapability.MiscServices.InputMethodFramework

Parameters

NameTypeMandatoryDescription
ctxBaseContextYesCurrent context of the input method.
infoPanelInfoYesInformation about the input method panel.

Return value

TypeDescription
Promise<Panel>Callback used to return the result. If the operation is successful, the created input method panel is returned.

Error codes

IDError Message
12800004not an input method extension.

Example

let panelInfo: inputMethodEngine.PanelInfo = {
  type: inputMethodEngine.PanelType.SOFT_KEYBOARD,
  flag: inputMethodEngine.PanelFlag.FLG_FIXED
}
inputMethodEngine.getInputMethodAbility().createPanel(this.context, panelInfo)
  .then((panel: inputMethodEngine.Panel) => {
    console.log('Succeed in creating panel.');
  }).catch((err: BusinessError) => {
    console.error(`Failed to create panel: ${JSON.stringify(err)}`);
  })

destroyPanel10+

destroyPanel(panel: Panel, callback: AsyncCallback<void>): void;

Destroys the specified input method panel. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.MiscServices.InputMethodFramework

Parameters

NameTypeMandatoryDescription
panelPanelYesInput method panel to destroy.
callbackAsyncCallback<void>YesCallback used to return the result. If the operation is successful, err is undefined. Otherwise, err is an error object.

Example

let panelInfo: inputMethodEngine.PanelInfo = {
  type: inputMethodEngine.PanelType.SOFT_KEYBOARD,
  flag: inputMethodEngine.PanelFlag.FLG_FIXED
}
let inputPanel: inputMethodEngine.Panel|undefined = undefined;
try {
  inputMethodEngine.getInputMethodAbility()
    .createPanel(this.context, panelInfo, (err: BusinessError, panel: inputMethodEngine.Panel) => {
      if (err) {
        console.error(`Failed to create panel: ${JSON.stringify(err)}`);
        return;
      }
      inputPanel = panel;
      console.log('Succeed in creating panel.');
    })
} catch (err) {
  console.error(`Failed to create panel: ${JSON.stringify(err)}`);
}
try {
  if (inputPanel) {
    inputMethodEngine.getInputMethodAbility().destroyPanel(inputPanel, (err: BusinessError) => {
      if (err !== undefined) {
        console.error(`Failed to destroy panel: ${JSON.stringify(err)}`);
        return;
      }
      console.log('Succeed in destroying panel.');
    })
  }
} catch (err) {
  console.error(`Failed to destroy panel: ${JSON.stringify(err)}`);
}

destroyPanel10+

destroyPanel(panel: Panel): Promise<void>;

Destroys the specified input method panel. This API uses a promise to return the result.

System capability: SystemCapability.MiscServices.InputMethodFramework

Parameters

NameTypeMandatoryDescription
panelPanelYesInput method panel to destroy.

Return value

TypeDescription
Promise<void>Promise that returns no value.

Example

let panelInfo: inputMethodEngine.PanelInfo = {
  type: inputMethodEngine.PanelType.SOFT_KEYBOARD,
  flag: inputMethodEngine.PanelFlag.FLG_FIXED
}
let inputPanel: inputMethodEngine.Panel|undefined = undefined;
try {
  inputMethodEngine.getInputMethodAbility()
    .createPanel(this.context, panelInfo, (err: BusinessError, panel: inputMethodEngine.Panel) => {
      if (err) {
        console.error(`Failed to create panel: ${JSON.stringify(err)}`);
        return;
      }
      inputPanel = panel;
      console.log('Succeed in creating panel.');
    })
} catch (err) {
  console.error(`Failed to create panel: ${JSON.stringify(err)}`);
}

try {
  if (inputPanel) {
    inputMethodEngine.getInputMethodAbility().destroyPanel(inputPanel).then(() => {
      console.log('Succeed in destroying panel.');
    }).catch((err: BusinessError) => {
      console.error(`Failed to destroy panel: ${JSON.stringify(err)}`);
    });
  }
} catch (err) {
  console.error(`Failed to destroy panel: ${JSON.stringify(err)}`);
}

KeyboardDelegate

In the following API examples, you must first use getKeyboardDelegate to obtain a KeyboardDelegate instance, and then call the APIs using the obtained instance.

on('keyDown'|'keyUp')

on(type: 'keyDown'|'keyUp', callback: (event: KeyEvent) => boolean): void

Enables listening for a physical keyboard event. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.MiscServices.InputMethodFramework

Parameters

NameTypeMandatoryDescription
typestringYesListening type.
- The value 'keyDown' indicates the keydown event.
- The value 'keyUp' indicates the keyup event.
callback(event: KeyEvent) => booleanYesCallback used to return the key information.

Example

inputMethodEngine.getKeyboardDelegate().on('keyUp', (keyEvent: inputMethodEngine.KeyEvent) => {
  console.log('inputMethodEngine keyCode.(keyUp):' + JSON.stringify(keyEvent.keyCode));
  console.log('inputMethodEngine keyAction.(keyUp):' + JSON.stringify(keyEvent.keyAction));
  return true;
});
inputMethodEngine.getKeyboardDelegate().on('keyDown', (keyEvent: inputMethodEngine.KeyEvent) => {
  console.log('inputMethodEngine keyCode.(keyDown):' + JSON.stringify(keyEvent.keyCode));
  console.log('inputMethodEngine keyAction.(keyDown):' + JSON.stringify(keyEvent.keyAction));
  return true;
});

off('keyDown'|'keyUp')

off(type: 'keyDown'|'keyUp', callback?: (event: KeyEvent) => boolean): void

Disables listening for a physical keyboard event. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.MiscServices.InputMethodFramework

Parameters

NameTypeMandatoryDescription
typestringYesListening type.
- The value 'keyDown' indicates the keydown event.
- The value 'keyUp' indicates the keyup event.
callback(event: KeyEvent) => booleanNoCallback for which listening is disabled. If this parameter is not specified, listening will be disabled for all callbacks corresponding to the specified type.

Example

inputMethodEngine.getKeyboardDelegate().off('keyUp', (keyEvent: inputMethodEngine.KeyEvent) => {
  console.log('delete keyUp notification.');
  return true;
});
inputMethodEngine.getKeyboardDelegate().off('keyDown', (keyEvent: inputMethodEngine.KeyEvent) => {
  console.log('delete keyDown notification.');
  return true;
});

on('keyEvent')10+

on(type: 'keyEvent', callback: (event: InputKeyEvent) => boolean): void

Enables listening for a keyboard event. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.MiscServices.InputMethodFramework

Parameters

NameTypeMandatoryDescription
typestringYesListening type. The value is fixed at 'keyEvent'.
callbackfunctionYesCallback used to return the result.
- The input parameter, of the InputKeyEvent type, indicates the key event information.
- If the event is consumed by the event subscriber, true is returned. Otherwise, false is returned.

Example

inputMethodEngine.getKeyboardDelegate().on('keyEvent', (keyEvent: InputKeyEvent) => {
  console.log('inputMethodEngine keyEvent.action:' + JSON.stringify(keyEvent.action));
  console.log('inputMethodEngine keyEvent.key.code:' + JSON.stringify(keyEvent.key.code));
  console.log('inputMethodEngine keyEvent.ctrlKey:' + JSON.stringify(keyEvent.ctrlKey));
  return true;
});

off('keyEvent')10+

off(type: 'keyEvent', callback?: (event: InputKeyEvent) => boolean): void

Disables listening for a keyboard event. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.MiscServices.InputMethodFramework

Parameters

NameTypeMandatoryDescription
typestringYesListening type. The value is fixed at 'keyEvent'.
callbackfunctionNoCallback for which listening is disabled. If this parameter is not specified, listening will be disabled for all callbacks corresponding to the specified type.

Example

inputMethodEngine.getKeyboardDelegate().off('keyEvent', (keyEvent: InputKeyEvent) => {
  console.log('This is a callback function which will be deregistered.');
  return true;
});
inputMethodEngine.getKeyboardDelegate().off('keyEvent');

on('cursorContextChange')

on(type: 'cursorContextChange', callback: (x: number, y:number, height:number) => void): void

Enables listening for the cursor change event. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.MiscServices.InputMethodFramework

Parameters

NameTypeMandatoryDescription
typestringYesListening type. The value is fixed at 'cursorContextChange'.
callback(x: number, y: number, height: number) => voidYesCallback used to return the cursor movement direction.
- x: x coordinate of the top of the cursor.
- y: y coordinate of the bottom of the cursor.
- height: height of the cursor.

Example

inputMethodEngine.getKeyboardDelegate().on('cursorContextChange', (x: number, y: number, height: number) => {
  console.log('inputMethodEngine cursorContextChange x:' + x);
  console.log('inputMethodEngine cursorContextChange y:' + y);
  console.log('inputMethodEngine cursorContextChange height:' + height);
});

off('cursorContextChange')

off(type: 'cursorContextChange', callback?: (x: number, y: number, height: number) => void): void

Disables listening for cursor context changes. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.MiscServices.InputMethodFramework

Parameters

NameTypeMandatoryDescription
typestringYesListening type. The value is fixed at 'cursorContextChange'.
callback(x: number, y:number, height:number) => voidNoCallback for which listening is disabled. If this parameter is not specified, listening will be disabled for all callbacks corresponding to the specified type.

Example

inputMethodEngine.getKeyboardDelegate().off('cursorContextChange', (x: number, y: number, height: number) => {
  console.log('delete cursorContextChange notification.');
});

on('selectionChange')

on(type: 'selectionChange', callback: (oldBegin: number, oldEnd: number, newBegin: number, newEnd: number) => void): void

Enables listening for the text selection change event. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.MiscServices.InputMethodFramework

Parameters

NameTypeMandatoryDescription
typestringYesListening type. The value is fixed at 'selectionChange'.
callback(oldBegin: number, oldEnd: number, newBegin: number, newEnd: number) => voidYesCallback used to return the text selection information.
- oldBegin: start of the selected text before the change.
- oldEnd: end of the selected text before the change.
- newBegin: start of the selected text after the change.
- newEnd: end of the selected text after the change.

Example

inputMethodEngine.getKeyboardDelegate()
  .on('selectionChange', (oldBegin: number, oldEnd: number, newBegin: number, newEnd: number) => {
    console.log('inputMethodEngine beforeEach selectionChange oldBegin:' + oldBegin);
    console.log('inputMethodEngine beforeEach selectionChange oldEnd:' + oldEnd);
    console.log('inputMethodEngine beforeEach selectionChange newBegin:' + newBegin);
    console.log('inputMethodEngine beforeEach selectionChange newEnd:' + newEnd);
  });

off('selectionChange')

off(type: 'selectionChange', callback?: (oldBegin: number, oldEnd: number, newBegin: number, newEnd: number) => void): void

Disables listening for the text selection change event. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.MiscServices.InputMethodFramework

Parameters

NameTypeMandatoryDescription
typestringYesListening type. The value is fixed at 'selectionChange'.
callback(oldBegin: number, oldEnd: number, newBegin: number, newEnd: number) => voidNoCallback for which listening is disabled. If this parameter is not specified, listening will be disabled for all callbacks corresponding to the specified type.

Example

inputMethodEngine.getKeyboardDelegate()
  .off('selectionChange', (oldBegin: number, oldEnd: number, newBegin: number, newEnd: number)  => {
    console.log('delete selectionChange notification.');
  });

on('textChange')

on(type: 'textChange', callback: (text: string) => void): void

Enables listening for the text change event. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.MiscServices.InputMethodFramework

Parameters

NameTypeMandatoryDescription
typestringYesListening type. The value is fixed at 'textChange'.
callback(text: string) => voidYesCallback used to return the text content.

Example

inputMethodEngine.getKeyboardDelegate().on('textChange', (text: string) => {
  console.log('inputMethodEngine textChange. text:' + text);
});

off('textChange')

off(type: 'textChange', callback?: (text: string) => void): void

Disables listening for the text change event. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.MiscServices.InputMethodFramework

Parameters

NameTypeMandatoryDescription
typestringYesListening type. The value is fixed at 'textChange'.
callback(text: string) => voidNoCallback for which listening is disabled. If this parameter is not specified, listening will be disabled for all callbacks corresponding to the specified type.

Example

inputMethodEngine.getKeyboardDelegate().off('textChange', (text: string) => {
  console.log('delete textChange notification. text:' + text);
});

on('editorAttributeChanged')10+

on(type: 'editorAttributeChanged', callback: (attr: EditorAttribute) => void): void

Enables listening for the edit box attribute change event. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.MiscServices.InputMethodFramework

Parameters

NameTypeMandatoryDescription
typestringYesListening type. The value is fixed at 'editorAttributeChanged'.
callback(attr: EditorAttribute) => voidYesCallback used to return the changed edit box attribute.

Example

inputMethodEngine.getKeyboardDelegate().on('editorAttributeChanged', (attr: inputMethodEngine.EditorAttribute) => {
  console.log(`Succeeded in receiving attribute of editor, inputPattern = ${attr.inputPattern}, enterKeyType = ${attr.enterKeyType}`);
});

off('editorAttributeChanged')10+

off(type: 'editorAttributeChanged', callback?: (attr: EditorAttribute) => void): void

Disables listening for the edit box attribute change event. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.MiscServices.InputMethodFramework

Parameters

NameTypeMandatoryDescription
typestringYesListening type. The value is fixed at 'editorAttributeChanged'.
callback(attr: EditorAttribute) => voidNoCallback for which listening is disabled. If this parameter is not specified, listening will be disabled for all callbacks corresponding to the specified type.

Example

inputMethodEngine.getKeyboardDelegate().off('editorAttributeChanged');

Panel10+

In the following API examples, you must first use createPanel to obtain a Panel instance, and then call the APIs using the obtained instance.

setUiContent10+

setUiContent(path: string, callback: AsyncCallback<void>): void

Loads content from a page to this input method panel. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.MiscServices.InputMethodFramework

Parameters

NameTypeMandatoryDescription
pathstringYesPath of the page from which the content will be loaded.
callbackAsyncCallback<void>YesCallback used to return the result. If the operation is successful, err is undefined. Otherwise, err is an error object.

Example

try {
  panel.setUiContent('pages/page2/page2', (err: BusinessError) => {
    if (err) {
      console.error(`Failed to setUiContent: ${JSON.stringify(err)}`);
      return;
    }
    console.log('Succeeded in setting the content.');
  });
} catch (err) {
  console.error(`Failed to setUiContent: ${JSON.stringify(err)}`);
}

setUiContent10+

setUiContent(path: string): Promise<void>

Loads content from a page to this input method panel. This API uses a promise to return the result.

System capability: SystemCapability.MiscServices.InputMethodFramework

Parameters

NameTypeMandatoryDescription
pathstringYesPath of the page from which the content will be loaded.

Return value

TypeDescription
Promise<void>Promise that returns no value.

Example

try {
  panel.setUiContent('pages/page2/page2').then(() => {
    console.log('Succeeded in setting the content.');
  }).catch((err: BusinessError) => {
    console.error(`Failed to setUiContent: ${JSON.stringify(err)}`);
  });
} catch (err) {
  console.error(`Failed to setUiContent: ${JSON.stringify(err)}`);
}

setUiContent10+

setUiContent(path: string, storage: LocalStorage, callback: AsyncCallback<void>): void

Loads content from a page linked to LocalStorage to this input method panel. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.MiscServices.InputMethodFramework

Parameters

NameTypeMandatoryDescription
pathstringYesPath of the page linked to LocalStorage.
storageLocalStorageYesStorage unit that provides storage for mutable and immutable state variables in the application.
callbackAsyncCallback<void>YesCallback used to return the result. If the operation is successful, err is undefined. Otherwise, err is an error object.

Example

let storage = new LocalStorage();
storage.setOrCreate('storageSimpleProp',121);
try {
  panel.setUiContent('pages/page2/page2', storage, (err: BusinessError) => {
    if (err) {
      console.error(`Failed to setUiContent: ${JSON.stringify(err)}`);
      return;
    }
    console.log('Succeeded in setting the content.');
  });
} catch (err) {
  console.error(`Failed to setUiContent: ${JSON.stringify(err)}`);
}

setUiContent10+

setUiContent(path: string, storage: LocalStorage): Promise<void>

Loads content from a page linked to LocalStorage to this panel. This API uses a promise to return the result.

System capability: SystemCapability.MiscServices.InputMethodFramework

Parameters

NameTypeMandatoryDescription
pathstringYesPath of the page from which the content will be loaded.
storageLocalStorageYesStorage unit that provides storage for mutable and immutable state variables in the application.

Return value

TypeDescription
Promise<void>Promise that returns no value.

Example

let storage = new LocalStorage();
storage.setOrCreate('storageSimpleProp',121);
try {
  panel.setUiContent('pages/page2/page2')then(() => {
    console.log('Succeeded in setting the content.');
  }).catch((err: BusinessError) => {
    console.error(`Failed to setUiContent: ${JSON.stringify(err)}`);
  });
} catch (err) {
  console.error(`Failed to setUiContent: ${JSON.stringify(err)}`);
}

resize10+

resize(width: number, height: number, callback: AsyncCallback<void>): void

Resizes this input method panel. This API uses an asynchronous callback to return the result.

NOTE

The panel width cannot exceed the screen width, and the panel height cannot be 0.6 times higher than the screen height.

System capability: SystemCapability.MiscServices.InputMethodFramework

Parameters

NameTypeMandatoryDescription
widthnumberYesTarget width of the panel, in px.
heightnumberYesTarget height of the panel, in px.
callbackAsyncCallback<void>YesCallback used to return the result. If the operation is successful, err is undefined. Otherwise, err is an error object.

Example

try {
  panel.resize(500, 1000, (err: BusinessError) => {
    if (err) {
      console.error(`Failed to resize panel: ${JSON.stringify(err)}`);
      return;
    }
    console.log('Succeeded in changing the panel size.');
  });
} catch (err) {
  console.error(`Failed to resize panel: ${JSON.stringify(err)}`);
}

resize10+

resize(width: number, height: number): Promise<void>;

Resizes this input method panel. This API uses a promise to return the result.

NOTE

The panel width cannot exceed the screen width, and the panel height cannot be 0.6 times higher than the screen height.

System capability: SystemCapability.MiscServices.InputMethodFramework

Parameters

NameTypeMandatoryDescription
widthnumberYesTarget width of the panel, in px.
heightnumberYesTarget height of the panel, in px.

Return value

TypeDescription
Promise<void>Promise that returns no value.

Example

try {
  panel.resize(500, 1000).then(() => {
    console.log('Succeeded in changing the panel size.');
  }).catch((err: BusinessError) => {
    console.error(`Failed to resize panel: ${JSON.stringify(err)}`);
  });
} catch (err) {
  console.error(`Failed to resize panel: ${JSON.stringify(err)}`);
}

moveTo10+

moveTo(x: number, y: number, callback: AsyncCallback<void>): void

Moves this input method panel to the specified position. This API uses an asynchronous callback to return the result. This API does not work on panels in the FLG_FIXED state.

System capability: SystemCapability.MiscServices.InputMethodFramework

Parameters

NameTypeMandatoryDescription
xnumberYesDistance to move along the x-axis, in px. A positive value indicates moving rightwards.
ynumberYesDistance to move along the y-axis, in px. A positive value indicates moving downwards.
callbackAsyncCallback<void>YesCallback used to return the result. If the operation is successful, err is undefined. Otherwise, err is an error object.

Example

try {
  panel.moveTo(300, 300, (err: BusinessError) =>{
    if (err) {
      console.error(`Failed to move panel: ${JSON.stringify(err)}`);
      return;
    }
    console.log('Succeeded in moving the panel.');
  });
} catch (err) {
    console.error(`Failed to move panel: ${JSON.stringify(err)}`);
}

moveTo10+

moveTo(x: number, y: number): Promise<void>

Moves this input method panel to the specified position. This API uses a promise to return the result. This API does not work on panels in the FLG_FIXED state.

System capability: SystemCapability.MiscServices.InputMethodFramework

Parameters

NameTypeMandatoryDescription
xnumberYesDistance to move along the x-axis, in px. A positive value indicates moving rightwards.
ynumberYesDistance to move along the y-axis, in px. A positive value indicates moving downwards.

Return value

TypeDescription
Promise<void>Promise that returns no value.

Example

try {
  panel.moveTo(300, 300).then(() => {
    console.log('Succeeded in moving the panel.');
  }).catch((err: BusinessError) => {
    console.error(`Failed to move panel: ${JSON.stringify(err)}`);
  });
} catch (err) {
  console.error(`Failed to move panel: ${JSON.stringify(err)}`);
}

show10+

show(callback: AsyncCallback<void>): void

Shows this input method panel. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.MiscServices.InputMethodFramework

Parameters

NameTypeMandatoryDescription
callbackAsyncCallback<void>YesCallback used to return the result. If the operation is successful, err is undefined. Otherwise, err is an error object.

Example

panel.show((err: BusinessError) => {
  if (err) {
    console.error(`Failed to show panel: ${JSON.stringify(err)}`);
    return;
  }
  console.log('Succeeded in showing the panel.');
});

show10+

show(): Promise<void>

Shows this input method panel. This API uses a promise to return the result.

System capability: SystemCapability.MiscServices.InputMethodFramework

Return value

TypeDescription
Promise<void>Promise that returns no value.

Example

panel.show().then(() => {
  console.log('Succeeded in showing the panel.');
}).catch((err: BusinessError) => {
  console.error(`Failed to show panel: ${JSON.stringify(err)}`);
});

hide10+

hide(callback: AsyncCallback<void>): void

Hides this panel. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.MiscServices.InputMethodFramework

Parameters

NameTypeMandatoryDescription
callbackAsyncCallback<void>YesCallback used to return the result. If the operation is successful, err is undefined. Otherwise, err is an error object.

Example

panel.hide((err: BusinessError) => {
  if (err) {
    console.error(`Failed to hide panel: ${JSON.stringify(err)}`);
    return;
  }
  console.log('Succeeded in hiding the panel.');
});

hide10+

hide(): Promise<void>

Hides this panel. This API uses a promise to return the result.

System capability: SystemCapability.MiscServices.InputMethodFramework

Return value

TypeDescription
Promise<void>Promise that returns no value.

Example

panel.hide().then(() => {
  console.log('Succeeded in hiding the panel.');
}).catch((err: BusinessError) => {
  console.error(`Failed to hide panel: ${JSON.stringify(err)}`);
});

on('show')10+

on(type: 'show', callback: () => void): void

Enables listening for the show event of this panel. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.MiscServices.InputMethodFramework

Parameters

NameTypeMandatoryDescription
typestringYesListening type. The value is fixed at 'show'.
callback() => voidYesCallback used to return the result.

Example

panel.on('show', () => {
  console.log('Panel is showing.');
});

on('hide')10+

on(type: 'hide', callback: () => void): void

Enables listening for the hide event of this panel. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.MiscServices.InputMethodFramework

Parameters

NameTypeMandatoryDescription
typestringYesListening type. The value is fixed at 'hide'.
callback() => voidYesCallback used to return the result.

Example

panel.on('hide', () => {
  console.log('Panel is hiding.');
});

off('show')10+

off(type: 'show', callback?: () => void): void

Disables listening for the show event of this panel. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.MiscServices.InputMethodFramework

Parameters

NameTypeMandatoryDescription
typestringYesListening type. The value is fixed at 'show'.
callback() => voidNoCallback for which listening is disabled. If this parameter is not specified, listening will be disabled for all callbacks corresponding to the specified type.

Example

panel.off('show');

off('hide')10+

off(type: 'hide', callback?: () => void): void

Disables listening for the hide event of this panel. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.MiscServices.InputMethodFramework

Parameters

NameTypeMandatoryDescription
typestringYesListening type. The value is fixed at 'hide'.
callback() => voidNoCallback for which listening is disabled. If this parameter is not specified, listening will be disabled for all callbacks corresponding to the specified type.

Example

panel.off('hide');

changeFlag10+

changeFlag(flag: PanelFlag): void

Changes the state type of this input method panel. This API only works for SOFT_KEYBOARD panels.

System capability: SystemCapability.MiscServices.InputMethodFramework

Parameters

NameTypeMandatoryDescription
flagPanelFlagYesTarget state type of the panel.

Example

let panelFlag = inputMethodEngine.PanelFlag.FLG_FIXED;
panel.changeFlag(panelFlag);

KeyboardController

In the following API examples, you must first use on('inputStart') to obtain a KeyboardController instance, and then call the APIs using the obtained instance.

hide9+

hide(callback: AsyncCallback<void>): void

Hides the keyboard. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.MiscServices.InputMethodFramework

Parameters

NameTypeMandatoryDescription
callbackAsyncCallback<void>YesCallback used to return the result. If the operation is successful, err is undefined. Otherwise, err is an error object.

Error codes

For details about the error codes, see Input Method Framework Error Codes.

IDError Message
12800003input method client error.

Example

keyboardController.hide((err: BusinessError) => {
  if (err) {
    console.error(`Failed to hide: ${JSON.stringify(err)}`);
    return;
  }
  console.log('Succeeded in hiding keyboard.');
});

hide9+

hide(): Promise<void>

Hides the keyboard. This API uses a promise to return the result.

System capability: SystemCapability.MiscServices.InputMethodFramework

Return value

TypeDescription
Promise<void>Promise that returns no value.

Error codes

For details about the error codes, see Input Method Framework Error Codes.

IDError Message
12800003input method client error.

Example

keyboardController.hide().then(() => {
  console.log('Succeeded in hiding keyboard.');
}).catch((err: BusinessError) => {
  console.log(`Failed to hide: ${JSON.stringify(err)}`);
});

hideKeyboard(deprecated)

hideKeyboard(callback: AsyncCallback<void>): void

Hides the keyboard. This API uses an asynchronous callback to return the result.

NOTE

This API is supported since API version 8 and deprecated since API version 9. You are advised to use hide instead.

System capability: SystemCapability.MiscServices.InputMethodFramework

Parameters

NameTypeMandatoryDescription
callbackAsyncCallback<void>YesCallback used to return the result. If the operation is successful, err is undefined. Otherwise, err is an error object.

Example

keyboardController.hideKeyboard((err: BusinessError) => {
  if (err) {
    console.error(`Failed to hideKeyboard: ${JSON.stringify(err)}`);
    return;
  }
  console.log('Succeeded in hiding keyboard.');
});

hideKeyboard(deprecated)

hideKeyboard(): Promise<void>

Hides the keyboard. This API uses a promise to return the result.

NOTE

This API is supported since API version 8 and deprecated since API version 9. You are advised to use hide instead.

System capability: SystemCapability.MiscServices.InputMethodFramework

Return value

TypeDescription
Promise<void>Promise that returns no value.

Example

keyboardController.hideKeyboard().then(() => {
  console.log('Succeeded in hiding keyboard.');
}).catch((err: BusinessError) => {
  console.log(`Failed to hideKeyboard: ${JSON.stringify(err)}`);
});

ExtendAction10+

Describes the type of the extended edit action on the text box.

System capability: SystemCapability.MiscServices.InputMethodFramework

NameValueDescription
SELECT_ALL0Select all.
CUT3Cut.
COPY4Copy.
PASTE5Paste.

Direction10+

Enumerates the directions of cursor movement of the input method.

System capability: SystemCapability.MiscServices.InputMethodFramework

NameValueDescription
CURSOR_UP1Upward.
CURSOR_DOWN2Downward.
CURSOR_LEFT3Leftward.
CURSOR_RIGHT4Rightward.

Range10+

Describes the range of the selected text.

System capability: SystemCapability.MiscServices.InputMethodFramework

NameTypeReadableWritableDescription
startnumberYesYesIndex of the first selected character in the text box.
endnumberYesYesIndex of the last selected character in the text box.

Movement10+

Describes the direction in which the cursor moves when the text is selected.

System capability: SystemCapability.MiscServices.InputMethodFramework

NameTypeReadableWritableDescription
directionDirectionYesYesDirection in which the cursor moves when the text is selected.

InputClient9+

In the following API examples, you must first use on('inputStart') to obtain an InputClient instance, and then call the APIs using the obtained instance.

sendKeyFunction9+

sendKeyFunction(action:number, callback: AsyncCallback<boolean>): void

Sends the function key. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.MiscServices.InputMethodFramework

Parameters

NameTypeMandatoryDescription
actionnumberYesAction of the function key.
- 0: invalid key.
- 1: confirm key (Enter key).
callbackAsyncCallback<boolean>YesCallback used to return the result. If the operation is successful, err is undefined and data is true. Otherwise, err is an error object.

Error codes

For details about the error codes, see Input Method Framework Error Codes.

IDError Message
12800003input method client error.

Example

let action = 1;
try {
  inputClient.sendKeyFunction(action, (err: BusinessError, result: boolean) => {
    if (err) {
      console.error(`Failed to sendKeyFunction: ${JSON.stringify(err)}`);
      return;
    }
    if (result) {
      console.log('Succeeded in sending key function.');
    } else {
      console.error('Failed to sendKeyFunction.');
    }
  });
} catch (err) {
  console.error(`Failed to sendKeyFunction: ${JSON.stringify(err)}`);
}

sendKeyFunction9+

sendKeyFunction(action: number): Promise<boolean>

Sends the function key. This API uses a promise to return the result.

System capability: SystemCapability.MiscServices.InputMethodFramework

Parameters

NameTypeMandatoryDescription
actionnumberYesAction of the function key.
0: invalid key.
1: confirm key (Enter key).

Return value

TypeDescription
Promise<boolean>Promise used to return the result. The value true means that the operation is successful, and false means the opposite.

Error codes

For details about the error codes, see Input Method Framework Error Codes.

IDError Message
12800003input method client error.

Example

let action = 1;
try {
  inputClient.sendKeyFunction(action).then((result: boolean) => {
    if (result) {
      console.log('Succeeded in sending key function.');
    } else {
      console.error('Failed to sendKeyFunction.');
    }
  }).catch((err: BusinessError) => {
    console.error(`Failed to sendKeyFunction: ${JSON.stringify(err)}`);
  });
} catch (err) {
  console.error(`Failed to sendKeyFunction: ${JSON.stringify(err)}`);
}

getForward9+

getForward(length:number, callback: AsyncCallback<string>): void

Obtains the specific-length text before the cursor. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.MiscServices.InputMethodFramework

Parameters

NameTypeMandatoryDescription
lengthnumberYesText length.
callbackAsyncCallback<string>YesCallback used to return the result. If the operation is successful, err is undefined and data is the obtained text. Otherwise, err is an error object.

Error codes

For details about the error codes, see Input Method Framework Error Codes.

IDError Message
12800003input method client error.
12800006Input method controller error.

Example

let length = 1;
try {
  inputClient.getForward(length, (err: BusinessError, text: string) => {
    if (err) {
      console.error(`Failed to getForward: ${JSON.stringify(err)}`);
      return;
    }
    console.log('Succeeded in getting forward, text: ' + text);
  });
} catch (err) {
  console.error(`Failed to getForward: ${JSON.stringify(err)}`);
}

getForward9+

getForward(length:number): Promise<string>

Obtains the specific-length text before the cursor. This API uses a promise to return the result.

System capability: SystemCapability.MiscServices.InputMethodFramework

Parameters

NameTypeMandatoryDescription
lengthnumberYesText length.

Return value

TypeDescription
Promise<string>Promise used to return the specific-length text before the cursor.

Error codes

For details about the error codes, see Input Method Framework Error Codes.

IDError Message
12800003input method client error.
12800006Input method controller error.

Example

let length = 1;
try {
  inputClient.getForward(length).then((text: string) => {
    console.log('Succeeded in getting forward, text: ' + text);
  }).catch((err: BusinessError) => {
    console.error(`Failed to getForward: ${JSON.stringify(err)}`);
  });
} catch (err) {
  console.error(`Failed to getForward: ${JSON.stringify(err)}`);
}

getForwardSync10+

getForwardSync(length:number): string

Obtains the specific-length text before the cursor.

System capability: SystemCapability.MiscServices.InputMethodFramework

Parameters

NameTypeMandatoryDescription
lengthnumberYesText length.

Return value

TypeDescription
stringSpecific-length text before the cursor.

Error codes

For details about the error codes, see Input Method Framework Error Codes.

IDError Message
12800003input method client error.
12800006input method controller error.

Example

let length = 1;
try {
  let text: string = inputClient.getForwardSync(length);
  console.log(`Succeeded in getting forward, text: ${text}`);
} catch (err) {
  console.error(`Failed to getForwardSync: ${JSON.stringify(err)}`);
}

getBackward9+

getBackward(length:number, callback: AsyncCallback<string>): void

Obtains the specific-length text after the cursor. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.MiscServices.InputMethodFramework

Parameters

NameTypeMandatoryDescription
lengthnumberYesText length.
callbackAsyncCallback<string>YesCallback used to return the result. If the operation is successful, err is undefined and data is the obtained text. Otherwise, err is an error object.

Error codes

For details about the error codes, see Input Method Framework Error Codes.

IDError Message
12800003input method client error.
12800006Input method controller error.

Example

let length = 1;
try {
  inputClient.getBackward(length, (err: BusinessError, text: string) => {
    if (err) {
      console.error(`Failed to getBackward: ${JSON.stringify(err)}`);
      return;
    }
    console.log('Succeeded in getting backward, text: ' + text);
  });
} catch (err) {
  console.error(`Failed to getBackward: ${JSON.stringify(err)}`);
}

getBackward9+

getBackward(length:number): Promise<string>

Obtains the specific-length text after the cursor. This API uses a promise to return the result.

System capability: SystemCapability.MiscServices.InputMethodFramework

Parameters

NameTypeMandatoryDescription
lengthnumberYesText length.

Return value

TypeDescription
Promise<string>Promise used to return the specific-length text after the cursor.

Error codes

For details about the error codes, see Input Method Framework Error Codes.

IDError Message
12800003input method client error.
12800006Input method controller error.

Example

let length = 1;
try {
  inputClient.getBackward(length).then((text: string) => {
    console.log('Succeeded in getting backward, text: ' + text);
  }).catch((err: BusinessError) => {
    console.error(`Failed to getBackward: ${JSON.stringify(err)}`);
  });
} catch (err) {
  console.error(`Failed to getBackward: ${JSON.stringify(err)}`);
}

getBackwardSync10+

getBackwardSync(length:number): string

Obtains the specific-length text after the cursor.

System capability: SystemCapability.MiscServices.InputMethodFramework

Parameters

NameTypeMandatoryDescription
lengthnumberYesText length.

Return value

TypeDescription
stringSpecific-length text after the cursor.

Error codes

For details about the error codes, see Input Method Framework Error Codes.

IDError Message
12800003input method client error.
12800006input method controller error.

Example

let length = 1;
try {
  let text: string = inputClient.getBackwardSync(length);
  console.log(`Succeeded in getting backward, text: ${text}`);
} catch (err) {
  console.error(`Failed to getBackwardSync: ${JSON.stringify(err)}`);
}

deleteForward9+

deleteForward(length:number, callback: AsyncCallback<boolean>): void

Deletes the fixed-length text before the cursor. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.MiscServices.InputMethodFramework

Parameters

NameTypeMandatoryDescription
lengthnumberYesText length.
callbackAsyncCallback<boolean>YesCallback used to return the result. If the operation is successful, err is undefined and data is true. Otherwise, err is an error object.

Error codes

For details about the error codes, see Input Method Framework Error Codes.

IDError Message
12800002Input method engine error.
12800003input method client error.

Example

let length = 1;
try {
  inputClient.deleteForward(length, (err: BusinessError, result: boolean) => {
    if (err) {
      console.error(`Failed to deleteForward: ${JSON.stringify(err)}`);
      return;
    }
    if (result) {
      console.log('Succeeded in deleting forward.');
    } else {
      console.error(`Failed to deleteForward: ${JSON.stringify(err)}`);
    }
  });
} catch (err) {
  console.error(`Failed to deleteForward: ${JSON.stringify(err)}`);
}

deleteForward9+

deleteForward(length:number): Promise<boolean>

Deletes the fixed-length text before the cursor. This API uses a promise to return the result.

System capability: SystemCapability.MiscServices.InputMethodFramework

Parameters

NameTypeMandatoryDescription
lengthnumberYesText length.

Return value

TypeDescription
Promise<boolean>Promise used to return the result. The value true means that the deletion is successful, and false means the opposite.

Error codes

For details about the error codes, see Input Method Framework Error Codes.

IDError Message
12800002Input method engine error.
12800003input method client error.

Example

let length = 1;
try {
  inputClient.deleteForward(length).then((result: boolean) => {
    if (result) {
      console.log('Succeeded in deleting forward.');
    } else {
      console.error('Failed to delete Forward.');
    }
  }).catch((err: BusinessError) => {
    console.error(`Failed to deleteForward: ${JSON.stringify(err)}`);
  });
} catch (err) {
  console.error(`Failed to deleteForward: ${JSON.stringify(err)}`);
}

deleteForwardSync10+

deleteForwardSync(length:number): void

Deletes the fixed-length text before the cursor.

System capability: SystemCapability.MiscServices.InputMethodFramework

Parameters

NameTypeMandatoryDescription
lengthnumberYesText length.

Error codes

For details about the error codes, see Input Method Framework Error Codes.

IDError Message
12800002input method engine error.
12800003input method client error.

Example

let length = 1;
try {
  inputClient.deleteForwardSync(length);
  console.log('Succeeded in deleting forward.');
} catch (err) {
  console.error('deleteForwardSync err: ' + JSON.stringify(err));
}

deleteBackward9+

deleteBackward(length:number, callback: AsyncCallback<boolean>): void

Deletes the fixed-length text after the cursor. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.MiscServices.InputMethodFramework

Parameters

NameTypeMandatoryDescription
lengthnumberYesText length.
callbackAsyncCallback<boolean>YesCallback used to return the result. If the operation is successful, err is undefined and data is true. Otherwise, err is an error object.

Error codes

For details about the error codes, see Input Method Framework Error Codes.

IDError Message
12800002Input method engine error.
12800003input method client error.

Example

let length = 1;
try {
  inputClient.deleteBackward(length, (err: BusinessError, result: boolean) => {
    if (err) {
      console.error(`Failed to deleteBackward: ${JSON.stringify(err)}`);
      return;
    }
    if (result) {
      console.log('Succeeded in deleting backward.');
    } else {
      console.error(`Failed to deleteBackward: ${JSON.stringify(err)}`);
    }
  });
} catch (err) {
  console.error('deleteBackward err: ' + JSON.stringify(err));
}

deleteBackward9+

deleteBackward(length:number): Promise<boolean>

Deletes the fixed-length text after the cursor. This API uses a promise to return the result.

System capability: SystemCapability.MiscServices.InputMethodFramework

Parameters

NameTypeMandatoryDescription
lengthnumberYesText length.

Return value

TypeDescription
Promise<boolean>Promise used to return the result. The value true means that the deletion is successful, and false means the opposite.

Error codes

For details about the error codes, see Input Method Framework Error Codes.

IDError Message
12800002Input method engine error.
12800003input method client error.

Example

let length = 1;
inputClient.deleteBackward(length).then((result: boolean) => {
  if (result) {
    console.log('Succeeded in deleting backward.');
  } else {
    console.error('Failed to deleteBackward.');
  }
}).catch((err: BusinessError) => {
  console.error(`Failed to deleteBackward: ${JSON.stringify(err)}`);
});

deleteBackwardSync10+

deleteBackwardSync(length:number): void

Deletes the fixed-length text after the cursor.

System capability: SystemCapability.MiscServices.InputMethodFramework

Parameters

NameTypeMandatoryDescription
lengthnumberYesText length.

Error codes

For details about the error codes, see Input Method Framework Error Codes.

IDError Message
12800002input method engine error.
12800003input method client error.

Example

let length = 1;
try {
  inputClient.deleteBackwardSync(length);
  console.log('Succeeded in deleting backward.');
} catch (err) {
  console.error('deleteBackwardSync err: ' + JSON.stringify(err));
}

insertText9+

insertText(text:string, callback: AsyncCallback<boolean>): void

Inserts text. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.MiscServices.InputMethodFramework

Parameters

NameTypeMandatoryDescription
textstringYesText to insert.
callbackAsyncCallback<boolean>YesCallback used to return the result. If the operation is successful, err is undefined and data is true. Otherwise, err is an error object.

Error codes

For details about the error codes, see Input Method Framework Error Codes.

IDError Message
12800002Input method engine error.
12800003input method client error.

Example

inputClient.insertText('test', (err: BusinessError, result: boolean) => {
  if (err) {
    console.error(`Failed to insertText: ${JSON.stringify(err)}`);
    return;
  }
  if (result) {
    console.log('Succeeded in inserting text.');
  } else {
    console.error('Failed to insertText.');
  }
});

insertText9+

insertText(text:string): Promise<boolean>

Inserts text. This API uses a promise to return the result.

System capability: SystemCapability.MiscServices.InputMethodFramework

Parameters

NameTypeMandatoryDescription
textstringYesText to insert.

Return value

TypeDescription
Promise<boolean>Promise used to return the result. The value true means that the insertion is successful, and false means the opposite.

Error codes

For details about the error codes, see Input Method Framework Error Codes.

IDError Message
12800002Input method engine error.
12800003input method client error.

Example

try {
  inputClient.insertText('test').then((result: boolean) => {
    if (result) {
      console.log('Succeeded in inserting text.');
    } else {
      console.error('Failed to insertText.');
    }
  }).catch((err: BusinessError) => {
    console.error(`Failed to insertText: ${JSON.stringify(err)}`);
  });
} catch (err) {
  console.error(`Failed to insertText: ${JSON.stringify(err)}`);
}

insertTextSync10+

insertTextSync(text: string): void

Inserts text.

System capability: SystemCapability.MiscServices.InputMethodFramework

Parameters

NameTypeMandatoryDescription
textstringYesText to insert.

Error codes

For details about the error codes, see Input Method Framework Error Codes.

IDError Message
12800002input method engine error.
12800003input method client error.

Example

try {
  inputClient.insertTextSync('test');
  console.log('Succeeded in inserting text.');
} catch (err) {
  console.error(`Failed to insertTextSync: ${JSON.stringify(err)}`);
}

getEditorAttribute9+

getEditorAttribute(callback: AsyncCallback<EditorAttribute>): void

Obtains the attribute of the edit box. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.MiscServices.InputMethodFramework

Parameters

NameTypeMandatoryDescription
callbackAsyncCallback<EditorAttribute>YesCallback used to return the result. If the operation is successful, err is undefined and data is the attribute of the edit box. Otherwise, err is an error object.

Error codes

For details about the error codes, see Input Method Framework Error Codes.

IDError Message
12800003input method client error.

Example

inputClient.getEditorAttribute((err: BusinessError, editorAttribute: inputMethodEngine.EditorAttribute) => {
  if (err) {
    console.error(`Failed to getEditorAttribute: ${JSON.stringify(err)}`);
    return;
  }
  console.log('editorAttribute.inputPattern: ' + JSON.stringify(editorAttribute.inputPattern));
  console.log('editorAttribute.enterKeyType: ' + JSON.stringify(editorAttribute.enterKeyType));
});

getEditorAttribute9+

getEditorAttribute(): Promise<EditorAttribute>

Obtains the attribute of the edit box. This API uses a promise to return the result.

System capability: SystemCapability.MiscServices.InputMethodFramework

Return value

TypeDescription
Promise<EditorAttribute>Promise used to return the attribute of the edit box.

Error codes

For details about the error codes, see Input Method Framework Error Codes.

IDError Message
12800003input method client error.

Example

inputClient.getEditorAttribute().then((editorAttribute: inputMethodEngine.EditorAttribute) => {
  console.log('editorAttribute.inputPattern: ' + JSON.stringify(editorAttribute.inputPattern));
  console.log('editorAttribute.enterKeyType: ' + JSON.stringify(editorAttribute.enterKeyType));
}).catch((err: BusinessError) => {
  console.error(`Failed to getEditorAttribute: ${JSON.stringify(err)}`);
});

getEditorAttributeSync10+

getEditorAttributeSync(): EditorAttribute

Obtains the attribute of the edit box.

System capability: SystemCapability.MiscServices.InputMethodFramework

Return value

TypeDescription
EditorAttributeAttribute of the edit box.

Error codes

For details about the error codes, see Input Method Framework Error Codes.

IDError Message
12800003input method client error.

Example

try {
  let editorAttribute: inputMethodEngine.EditorAttribute = inputClient.getEditorAttributeSync();
  console.log(`Succeeded in getEditorAttributeSync, editorAttribute = ${JSON.stringify(editorAttribute)}`);
} catch (err) {
  console.error(`Failed to getEditorAttributeSync: ${JSON.stringify(err)}`);
}

moveCursor9+

moveCursor(direction: number, callback: AsyncCallback<void>): void

Moves the cursor. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.MiscServices.InputMethodFramework

Parameters

NameTypeMandatoryDescription
directionnumberYesDirection in which the cursor moves.
- 1: upward.
- 2: downward.
- 3: leftward.
- 4: rightward.
callbackAsyncCallback<void>YesCallback used to return the result. If the operation is successful, err is undefined. Otherwise, err is an error object.

Error codes

For details about the error codes, see Input Method Framework Error Codes.

IDError Message
12800003input method client error.

Example

try {
  inputClient.moveCursor(inputMethodEngine.Direction.CURSOR_UP, (err: BusinessError) => {
    if (err) {
      console.error(`Failed to moveCursor: ${JSON.stringify(err)}`);
      return;
    }
    console.log('Succeeded in moving cursor.');
  });
} catch (err) {
  console.error(`Failed to moveCursor: ${JSON.stringify(err)}`);
}

moveCursor9+

moveCursor(direction: number): Promise<void>

Moves the cursor. This API uses a promise to return the result.

System capability: SystemCapability.MiscServices.InputMethodFramework

Parameters

NameTypeMandatoryDescription
directionnumberYesDirection in which the cursor moves.
- 1: upward.
- 2: downward.
- 3: leftward.
- 4: rightward.

Return value

TypeDescription
Promise<void>Promise that returns no value.

Error codes

For details about the error codes, see Input Method Framework Error Codes.

IDError Message
12800003input method client error.

Example

try {
  inputClient.moveCursor(inputMethodEngine.Direction.CURSOR_UP).then(() => {
    console.log('Succeeded in moving cursor.');
  }).catch((err: BusinessError) => {
    console.error(`Failed to moveCursor: ${JSON.stringify(err)}`);
  });
} catch (err) {
  console.error(`Failed to moveCursor: ${JSON.stringify(err)}`);
}

moveCursorSync10+

moveCursorSync(direction: number): void

Moves the cursor.

System capability: SystemCapability.MiscServices.InputMethodFramework

Parameters

NameTypeMandatoryDescription
directionnumberYesDirection in which the cursor moves.
- 1: upward.
- 2: downward.
- 3: leftward.
- 4: rightward.

Error codes

For details about the error codes, see Input Method Framework Error Codes.

IDError Message
12800003input method client error.

Example

try {
  inputClient.moveCursorSync(inputMethodEngine.Direction.CURSOR_UP);
  console.log('Succeeded in moving cursor.');
} catch (err) {
  console.error(`Failed to moveCursorSync: ${JSON.stringify(err)}`);
}

selectByRange10+

selectByRange(range: Range, callback: AsyncCallback<void>): void

Selects text based on the specified range. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.MiscServices.InputMethodFramework

Parameters

NameTypeMandatoryDescription
rangeRangeYesRange of the selected text.
callbackAsyncCallback<void>YesCallback used to return the result. If the selection event is sent, err is undefined; otherwise, err is an error object.

Error codes

For details about the error codes, see Input Method Framework Error Codes.

IDError Message
401parameter error.
12800003input method client error.

Example

try {
  let range: inputMethodEngine.Range = { start: 0, end: 1 };
  inputClient.selectByRange(range, (err: BusinessError) => {
    if (err) {
      console.error(`Failed to selectByRange: ${JSON.stringify(err)}`);
      return;
    }
    console.log('Succeeded in selecting by range.');
  });
} catch (err) {
  console.error(`Failed to selectByRange: ${JSON.stringify(err)}`);
}

selectByRange10+

selectByRange(range: Range): Promise<void>

Selects text based on the specified range. This API uses a promise to return the result.

System capability: SystemCapability.MiscServices.InputMethodFramework

Parameters

NameTypeMandatoryDescription
rangeRangeYesRange of the selected text.

Return value

TypeDescription
Promise<void>Promise that returns no value.

Error codes

For details about the error codes, see Input Method Framework Error Codes.

IDError Message
401parameter error.
12800003input method client error.

Example

try {
  let range: inputMethodEngine.Range = { start: 0, end: 1 };
  inputClient.selectByRange(range).then(() => {
    console.log('Succeeded in selecting by range.');
  }).catch((err: BusinessError) => {
    console.error(`Failed to selectByRange: ${JSON.stringify(err)}`);
  });
} catch (err) {
  console.error(`Failed to selectByRange: ${JSON.stringify(err)}`);
}

selectByRangeSync10+

selectByRangeSync(range: Range): void

Selects text based on the specified range.

System capability: SystemCapability.MiscServices.InputMethodFramework

Parameters

NameTypeMandatoryDescription
rangeRangeYesRange of the selected text.

Error codes

For details about the error codes, see Input Method Framework Error Codes.

IDError Message
401parameter error.
12800003input method client error.

Example

try {
  let range: inputMethodEngine.Range = { start: 0, end: 1 };
  inputClient.selectByRangeSync(range);
  console.log('Succeeded in selecting by range.');
} catch (err) {
  console.error(`Failed to selectByRangeSync: ${JSON.stringify(err)}`);
}

selectByMovement10+

selectByMovement(movement: Movement, callback: AsyncCallback<void>): void

Selects text based on the cursor movement direction. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.MiscServices.InputMethodFramework

Parameters

NameTypeMandatoryDescription
movementMovementYesDirection in which the cursor moves when the text is selected.
callbackAsyncCallback<void>YesCallback used to return the result. If the selection event is sent, err is undefined; otherwise, err is an error object.

Error codes

For details about the error codes, see Input Method Framework Error Codes.

IDError Message
401parameter error.
12800003input method client error.

Example

try {
  let movement: inputMethodEngine.Movement = { direction: 1 };
  inputClient.selectByMovement(movement, (err: BusinessError) => {
    if (err) {
      console.error(`Failed to selectByMovement: ${JSON.stringify(err)}`);
      return;
    }
    console.log('Succeeded in selecting by movement.');
  });
} catch (err) {
  console.error(`Failed to selectByMovement: ${JSON.stringify(err)}`);
}

selectByMovement10+

selectByMovement(movement: Movement): Promise<void>

Selects text based on the specified range. This API uses a promise to return the result.

System capability: SystemCapability.MiscServices.InputMethodFramework

Parameters

NameTypeMandatoryDescription
movementMovementYesDirection in which the cursor moves when the text is selected.

Return value

TypeDescription
Promise<void>Promise that returns no value.

Error codes

For details about the error codes, see Input Method Framework Error Codes.

IDError Message
401parameter error.
12800003input method client error.

Example

try {
  let movement: inputMethodEngine.Movement = { direction: 1 };
  inputClient.selectByMovement(movement).then(() => {
    console.log('Succeeded in selecting by movement.');
  }).catch((err: BusinessError) => {
    console.error(`Failed to selectByMovement: ${JSON.stringify(err)}`);
  });
} catch (err) {
  console.error(`Failed to selectByMovement: ${JSON.stringify(err)}`);
}

selectByMovementSync10+

selectByMovementSync(movement: Movement): void

Selects text based on the cursor movement direction.

System capability: SystemCapability.MiscServices.InputMethodFramework

Parameters

NameTypeMandatoryDescription
movementMovementYesDirection in which the cursor moves when the text is selected.

Error codes

For details about the error codes, see Input Method Framework Error Codes.

IDError Message
401parameter error.
12800003input method client error.

Example

try {
  let movement: inputMethodEngine.Movement = { direction: 1 };  
  inputClient.selectByMovementSync(movement);
  console.log('Succeeded in selecting by movement.');
} catch (err) {
  console.error(`Failed to selectByMovement: ${JSON.stringify(err)}`);
}

getTextIndexAtCursor10+

getTextIndexAtCursor(callback: AsyncCallback<number>): void

Obtains the index of the text where the cursor is located. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.MiscServices.InputMethodFramework

Parameters

NameTypeMandatoryDescription
callbackAsyncCallback<number>YesCallback used to return the result. If the text index is obtained, err is undefined; otherwise, err is an error object.

Error codes

For details about the error codes, see Input Method Framework Error Codes.

IDError Message
12800003input method client error.
12800006Input method controller error.

Example

inputClient.getTextIndexAtCursor((err: BusinessError, index: number) => {
  if (err) {
    console.error(`Failed to getTextIndexAtCursor: ${JSON.stringify(err)}`);
    return;
  }
  console.log('Succeeded in getTextIndexAtCursor: ' + index);
});

getTextIndexAtCursor10+

getTextIndexAtCursor(): Promise<number>

Obtains the index of the text where the cursor is located. This API uses a promise to return the result.

System capability: SystemCapability.MiscServices.InputMethodFramework

Return value

TypeDescription
Promise<number>Promise used to return the result.

Error codes

For details about the error codes, see Input Method Framework Error Codes.

IDError Message
12800003input method client error.
12800006Input method controller error.

Example

inputClient.getTextIndexAtCursor().then((index: number) => {
  console.log('Succeeded in getTextIndexAtCursor: ' + index);
}).catch((err: BusinessError) => {
  console.error(`Failed to getTextIndexAtCursor: ${JSON.stringify(err)}`);
});

getTextIndexAtCursorSync10+

getTextIndexAtCursorSync(): number

Obtains the index of the text where the cursor is located.

System capability: SystemCapability.MiscServices.InputMethodFramework

Return value

TypeDescription
numberIndex of the text where the cursor is located.

Error codes

For details about the error codes, see Input Method Framework Error Codes.

IDError Message
12800003input method client error.
12800006Input method controller error.

Example

try{
  let index: number = inputClient.getTextIndexAtCursorSync();
  console.log(`Succeeded in getTextIndexAtCursorSync, index: ${index}`);
} catch (err) {
  console.error(`Failed to getTextIndexAtCursorSync: ${JSON.stringify(err)}`);
}

sendExtendAction10+

sendExtendAction(action: ExtendAction, callback: AsyncCallback<void>): void

Sends an extended edit action. This API uses an asynchronous callback to return the result.

NOTE

The input method calls this API to send an extended edit action to an edit box, which in turn listens for the corresponding event on('handleExtendAction') for further processing.

System capability: SystemCapability.MiscServices.InputMethodFramework

Parameters

NameTypeMandatoryDescription
actionExtendActionYesExtended edit action to send.
callbackAsyncCallback<void>YesCallback used to return the result. If the operation is successful, err is undefined. Otherwise, err is an error object.

Error codes

For details about the error codes, see Input Method Framework Error Codes.

IDError Message
12800003input method client error.
12800006Input method controller error.

Example

try {
  inputClient.sendExtendAction(inputMethodEngine.ExtendAction.COPY, (err: BusinessError) => {
    if (err) {
      console.error(`Failed to sendExtendAction: ${JSON.stringify(err)}`);
      return;
    }
    console.log('Succeeded in sending extend action.');
  });
} catch(err) {
  console.error(`Failed to sendExtendAction: ${JSON.stringify(err)}`);
}

sendExtendAction10+

sendExtendAction(action: ExtendAction): Promise<void>

Sends an extended edit action. This API uses a promise to return the result.

NOTE

The input method calls this API to send an extended edit action to an edit box, which in turn listens for the corresponding event on('handleExtendAction') for further processing.

System capability: SystemCapability.MiscServices.InputMethodFramework

Parameters

NameTypeMandatoryDescription
actionExtendActionYesExtended edit action to send.

Return value

TypeDescription
Promise<void>Promise that returns no value.

Error codes

For details about the error codes, see Input Method Framework Error Codes.

IDError Message
12800003input method client error.
12800006Input method controller error.

Example

try {
  inputClient.sendExtendAction(inputMethodEngine.ExtendAction.COPY).then(() => {
    console.log('Succeeded in sending extend action.');
  }).catch((err: BusinessError) => {
    console.error(`Failed to sendExtendAction: ${JSON.stringify(err)}`);
  });
} catch(err) {
  console.error(`Failed to sendExtendAction: ${JSON.stringify(err)}`);
}

EditorAttribute

Attribute of the edit box.

System capability: SystemCapability.MiscServices.InputMethodFramework

NameTypeReadableWritableDescription
enterKeyTypenumberYesNoFunction attribute of the edit box.
inputPatternnumberYesNoText attribute of the edit box.

KeyEvent

Describes the attribute of a key.

System capability: SystemCapability.MiscServices.InputMethodFramework

NameTypeReadableWritableDescription
keyCodenumberYesNoKey value. For details, see KeyCode.
keyActionnumberYesNoKey event type.
- 2: keydown event.
- 3: keyup event.

PanelFlag10+

Enumerates the state types of the input method panel.

System capability: SystemCapability.MiscServices.InputMethodFramework

NameValueDescription
FLG_FIXED0Fixed state type.
FLG_FLOATING1Floating state type.

PanelType10+

Enumerates the types of the input method panel.

System capability: SystemCapability.MiscServices.InputMethodFramework

NameValueDescription
SOFT_KEYBOARD0Soft keyboard type.
STATUS_BAR1Status bar type.

PanelInfo10+

System capability: SystemCapability.MiscServices.InputMethodFramework

Describes the attributes of the input method panel.

NameTypeReadableWritableDescription
typenumberYesYesType of the panel.
flagnumberYesYesState type of the panel.

TextInputClient(deprecated)

NOTE

This API is supported since API version 8 and deprecated since API version 9. You are advised to use InputClient instead.

In the following API examples, you must first use on('inputStart') to obtain a TextInputClient instance, and then call the APIs using the obtained instance.

getForward(deprecated)

getForward(length:number, callback: AsyncCallback<string>): void

Obtains the specific-length text before the cursor. This API uses an asynchronous callback to return the result.

NOTE

This API is supported since API version 8 and deprecated since API version 9. You are advised to use getForward instead.

System capability: SystemCapability.MiscServices.InputMethodFramework

Parameters

NameTypeMandatoryDescription
lengthnumberYesText length.
callbackAsyncCallback<string>YesCallback used to return the result. If the operation is successful, err is undefined and data is the obtained text. Otherwise, err is an error object.

Example

let length = 1;
textInputClient.getForward(length, (err: BusinessError, text: string) => {
  if (err) {
    console.error(`Failed to getForward: ${JSON.stringify(err)}`);
    return;
  }
  console.log('Succeeded in getting forward, text: ' + text);
});

getForward(deprecated)

getForward(length:number): Promise<string>

Obtains the specific-length text before the cursor. This API uses a promise to return the result.

NOTE

This API is supported since API version 8 and deprecated since API version 9. You are advised to use getForward instead.

System capability: SystemCapability.MiscServices.InputMethodFramework

Parameters

NameTypeMandatoryDescription
lengthnumberYesText length.

Return value

TypeDescription
Promise<string>Promise used to return the specific-length text before the cursor.

Example

let length = 1;
textInputClient.getForward(length).then((text: string) => {
  console.log('Succeeded in getting forward, text: ' + text);
}).catch((err: BusinessError) => {
  console.error(`Failed to getForward: ${JSON.stringify(err)}`);
});

getBackward(deprecated)

getBackward(length:number, callback: AsyncCallback<string>): void

Obtains the specific-length text after the cursor. This API uses an asynchronous callback to return the result.

NOTE

This API is supported since API version 8 and deprecated since API version 9. You are advised to use getBackward instead.

System capability: SystemCapability.MiscServices.InputMethodFramework

Parameters

NameTypeMandatoryDescription
lengthnumberYesText length.
callbackAsyncCallback<string>YesCallback used to return the result. If the operation is successful, err is undefined and data is the obtained text. Otherwise, err is an error object.

Example

let length = 1;
textInputClient.getBackward(length, (err: BusinessError, text: string) => {
  if (err) {
    console.error(`Failed to getBackward: ${JSON.stringify(err)}`);
    return;
  }
  console.log('Succeeded in getting borward, text: ' + text);
});

getBackward(deprecated)

getBackward(length:number): Promise<string>

Obtains the specific-length text after the cursor. This API uses a promise to return the result.

NOTE

This API is supported since API version 8 and deprecated since API version 9. You are advised to use getBackward instead.

System capability: SystemCapability.MiscServices.InputMethodFramework

Parameters

NameTypeMandatoryDescription
lengthnumberYesText length.

Return value

TypeDescription
Promise<string>Promise used to return the specific-length text after the cursor.

Example

let length = 1;
textInputClient.getBackward(length).then((text: string) => {
  console.log('Succeeded in getting backward: ' + JSON.stringify(text));
}).catch((err: BusinessError) => {
  console.error(`Failed to getBackward: ${JSON.stringify(err)}`);
});

deleteForward(deprecated)

deleteForward(length:number, callback: AsyncCallback<boolean>): void

Deletes the fixed-length text before the cursor. This API uses an asynchronous callback to return the result.

NOTE

This API is supported since API version 8 and deprecated since API version 9. You are advised to use deleteForward instead.

System capability: SystemCapability.MiscServices.InputMethodFramework

Parameters

NameTypeMandatoryDescription
lengthnumberYesText length.
callbackAsyncCallback<boolean>YesCallback used to return the result. If the operation is successful, err is undefined and data is true. Otherwise, err is an error object.

Example

let length = 1;
textInputClient.deleteForward(length, (err: BusinessError, result: boolean) => {
  if (err) {
    console.error(`Failed to deleteForward: ${JSON.stringify(err)}`);
    return;
  }
  if (result) {
    console.log('Succeeded in deleting forward.');
  } else {
    console.error('Failed to deleteForward.');
  }
});

deleteForward(deprecated)

deleteForward(length:number): Promise<boolean>

Deletes the fixed-length text before the cursor. This API uses a promise to return the result.

NOTE

This API is supported since API version 8 and deprecated since API version 9. You are advised to use deleteForward instead.

System capability: SystemCapability.MiscServices.InputMethodFramework

Parameters

NameTypeMandatoryDescription
lengthnumberYesText length.

Return value

TypeDescription
Promise<boolean>Promise used to return the result. The value true means that the deletion is successful, and false means the opposite.

Example

let length = 1;
textInputClient.deleteForward(length).then((result: boolean) => {
  if (result) {
    console.log('Succeeded in deleting forward.');
  } else {
    console.error('Failed to delete forward.');
  }
}).catch((err: BusinessError) => {
  console.error(`Failed to deleteForward: ${JSON.stringify(err)}`);
});

deleteBackward(deprecated)

deleteBackward(length:number, callback: AsyncCallback<boolean>): void

Deletes the fixed-length text after the cursor. This API uses an asynchronous callback to return the result.

NOTE

This API is supported since API version 8 and deprecated since API version 9. You are advised to use deleteBackward instead.

System capability: SystemCapability.MiscServices.InputMethodFramework

Parameters

NameTypeMandatoryDescription
lengthnumberYesText length.
callbackAsyncCallback<boolean>YesCallback used to return the result. If the operation is successful, err is undefined and data is true. Otherwise, err is an error object.

Example

let length = 1;
textInputClient.deleteBackward(length, (err: BusinessError, result: boolean) => {
  if (err) {
    console.error(`Failed to deleteBackward: ${JSON.stringify(err)}`);
    return;
  }
  if (result) {
    console.log('Succeeded in deleting backward.');
  } else {
    console.error('Failed to deleteBackward.');
  }
});

deleteBackward(deprecated)

deleteBackward(length:number): Promise<boolean>

Deletes the fixed-length text after the cursor. This API uses a promise to return the result.

NOTE

This API is supported since API version 8 and deprecated since API version 9. You are advised to use deleteBackward instead.

System capability: SystemCapability.MiscServices.InputMethodFramework

Parameters

NameTypeMandatoryDescription
lengthnumberYesText length.

Return value

TypeDescription
Promise<boolean>Promise used to return the result. The value true means that the deletion is successful, and false means the opposite.

Example

let length = 1;
textInputClient.deleteBackward(length).then((result: boolean) => {
  if (result) {
    console.log('Succeeded in deleting backward.');
  } else {
    console.error('Failed to deleteBackward.');
  }
}).catch((err: BusinessError) => {
  console.error(`Failed to deleteBackward: ${JSON.stringify(err)}`);
});

sendKeyFunction(deprecated)

sendKeyFunction(action: number, callback: AsyncCallback<boolean>): void

Sends the function key. This API uses an asynchronous callback to return the result.

NOTE

This API is supported since API version 8 and deprecated since API version 9. You are advised to use sendKeyFunction instead.

System capability: SystemCapability.MiscServices.InputMethodFramework

Parameters

NameTypeMandatoryDescription
actionnumberYesAction of the function key.
- 0: invalid key.
- 1: confirm key (Enter key).
callbackAsyncCallback<boolean>YesCallback used to return the result. If the operation is successful, err is undefined and data is true. Otherwise, err is an error object.

Example

let action = 1;
textInputClient.sendKeyFunction(action, (err: BusinessError, result: boolean) => {
  if (err) {
    console.error(`Failed to sendKeyFunction: ${JSON.stringify(err)}`);
    return;
  }
  if (result) {
    console.log('Succeeded in sending key function.');
  } else {
    console.error('Failed to sendKeyFunction.');
  }
});

sendKeyFunction(deprecated)

sendKeyFunction(action: number): Promise<boolean>

Sends the function key. This API uses a promise to return the result.

NOTE

This API is supported since API version 8 and deprecated since API version 9. You are advised to use sendKeyFunction instead.

System capability: SystemCapability.MiscServices.InputMethodFramework

Parameters

NameTypeMandatoryDescription
actionnumberYesAction of the function key.
0: invalid key.
1: confirm key (Enter key).

Return value

TypeDescription
Promise<boolean>Promise used to return the result. The value true means that the setting is successful, and false means the opposite.

Example

let action = 1;
textInputClient.sendKeyFunction(action).then((result: boolean) => {
  if (result) {
    console.log('Succeeded in sending key function.');
  } else {
    console.error('Failed to sendKeyFunction.');
  }
}).catch((err: BusinessError) => {
  console.error(`Failed to sendKeyFunction: ${JSON.stringify(err)}`);
});

insertText(deprecated)

insertText(text:string, callback: AsyncCallback<boolean>): void

Inserts text. This API uses an asynchronous callback to return the result.

NOTE

This API is supported since API version 8 and deprecated since API version 9. You are advised to use insertText instead.

System capability: SystemCapability.MiscServices.InputMethodFramework

Parameters

NameTypeMandatoryDescription
textstringYesText to insert.
callbackAsyncCallback<boolean>YesCallback used to return the result. If the operation is successful, err is undefined and data is true. Otherwise, err is an error object.

Example

textInputClient.insertText('test', (err: BusinessError, result: boolean) => {
  if (err) {
    console.error(`Failed to insertText: ${JSON.stringify(err)}`);
    return;
  }
  if (result) {
    console.log('Succeeded in inserting text.');
  } else {
    console.error('Failed to insertText.');
  }
});

insertText(deprecated)

insertText(text:string): Promise<boolean>

Inserts text. This API uses a promise to return the result.

NOTE

This API is supported since API version 8 and deprecated since API version 9. You are advised to use insertText instead.

System capability: SystemCapability.MiscServices.InputMethodFramework

Parameters

NameTypeMandatoryDescription
textstringYesText to insert.

Return value

TypeDescription
Promise<boolean>Promise used to return the result. The value true means that the insertion is successful, and false means the opposite.

Example

textInputClient.insertText('test').then((result: boolean) => {
  if (result) {
    console.log('Succeeded in inserting text.');
  } else {
    console.error('Failed to insertText.');
  }
}).catch((err: BusinessError) => {
  console.error(`Failed to insertText: ${JSON.stringify(err)}`);
});

getEditorAttribute(deprecated)

getEditorAttribute(callback: AsyncCallback<EditorAttribute>): void

Obtains the attribute of the edit box. This API uses an asynchronous callback to return the result.

NOTE

This API is supported since API version 8 and deprecated since API version 9. You are advised to use getEditorAttribute instead.

System capability: SystemCapability.MiscServices.InputMethodFramework

Parameters

NameTypeMandatoryDescription
callbackAsyncCallback<EditorAttribute>YesCallback used to return the result. If the operation is successful, err is undefined and data is the attribute of the edit box. Otherwise, err is an error object.

Example

textInputClient.getEditorAttribute((err: BusinessError, editorAttribute: inputMethodEngine.EditorAttribute) => {
  if (err) {
    console.error(`Failed to getEditorAttribute: ${JSON.stringify(err)}`);
    return;
  }
  console.log('editorAttribute.inputPattern: ' + JSON.stringify(editorAttribute.inputPattern));
  console.log('editorAttribute.enterKeyType: ' + JSON.stringify(editorAttribute.enterKeyType));
});

getEditorAttribute(deprecated)

getEditorAttribute(): Promise<EditorAttribute>

Obtains the attribute of the edit box. This API uses a promise to return the result.

NOTE

This API is supported since API version 8 and deprecated since API version 9. You are advised to use getEditorAttribute instead.

System capability: SystemCapability.MiscServices.InputMethodFramework

Return value

TypeDescription
Promise<EditorAttribute>Promise used to return the attribute of the edit box.

Example

textInputClient.getEditorAttribute().then((editorAttribute: inputMethodEngine.EditorAttribute) => {
  console.log('editorAttribute.inputPattern: ' + JSON.stringify(editorAttribute.inputPattern));
  console.log('editorAttribute.enterKeyType: ' + JSON.stringify(editorAttribute.enterKeyType));
}).catch((err: BusinessError) => {
  console.error(`Failed to getEditorAttribute: ${JSON.stringify(err)}`);
});

你可能感兴趣的鸿蒙文章

harmony 鸿蒙APIs

harmony 鸿蒙System Common Events (To Be Deprecated Soon)

harmony 鸿蒙System Common Events

harmony 鸿蒙API Reference Document Description

harmony 鸿蒙Enterprise Device Management Overview (for System Applications Only)

harmony 鸿蒙BundleStatusCallback

harmony 鸿蒙@ohos.bundle.innerBundleManager (innerBundleManager)

harmony 鸿蒙@ohos.distributedBundle (Distributed Bundle Management)

harmony 鸿蒙@ohos.bundle (Bundle)

harmony 鸿蒙@ohos.enterprise.EnterpriseAdminExtensionAbility (EnterpriseAdminExtensionAbility)

  • 所属分类: 后端技术
  • 本文标签: 软件 鸿蒙
  • 版权声明: 本文链接 https://seaxiang.com/blog/f0ee64c87f4c46c4b0c42c9337ee4de1