harmony 鸿蒙@ohos.inputMethodEngine (输入法服务)

2022-08-09 浏览 (1372)

@ohos.inputMethodEngine (输入法服务)

本模块面向输入法应用(包括系统输入法应用、三方输入法应用),为输入法应用提供能力,包括:创建软键盘窗口、插入/删除字符、选中文本、监听物理键盘按键事件等。

说明:

本模块首批接口从API version 8开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。

导入模块

import inputMethodEngine from '@ohos.inputMethodEngine';

常量

功能键常量值、编辑框常量值及光标常量值。

系统能力: SystemCapability.MiscServices.InputMethodFramework

名称类型说明
ENTER_KEY_TYPE_UNSPECIFIEDnumber0无功能键。
ENTER_KEY_TYPE_GOnumber2“前往”功能键。
ENTER_KEY_TYPE_SEARCHnumber3“搜索”功能键。
ENTER_KEY_TYPE_SENDnumber4“发送”功能键。
ENTER_KEY_TYPE_NEXTnumber5“下一个”功能键。
ENTER_KEY_TYPE_DONEnumber6“回车”功能键。
ENTER_KEY_TYPE_PREVIOUSnumber7“前一个”功能键。
PATTERN_NULLnumber-1无特殊性编辑框。
PATTERN_TEXTnumber0文本编辑框。
PATTERN_NUMBERnumber2数字编辑框。
PATTERN_PHONEnumber3电话号码编辑框。
PATTERN_DATETIMEnumber4日期编辑框。
PATTERN_EMAILnumber5邮件编辑框。
PATTERN_URInumber6超链接编辑框。
PATTERN_PASSWORDnumber7密码编辑框。
OPTION_ASCIInumber20允许输入ASCII值。
OPTION_NONEnumber0不指定编辑框输入属性。
OPTION_AUTO_CAP_CHARACTERSnumber2允许输入字符。
OPTION_AUTO_CAP_SENTENCESnumber8允许输入句子。
OPTION_AUTO_WORDSnumber4允许输入单词。
OPTION_MULTI_LINEnumber1允许输入多行。
OPTION_NO_FULLSCREENnumber10半屏样式。
FLAG_SELECTINGnumber2编辑框处于选择状态。
FLAG_SINGLE_LINEnumber1编辑框为单行。
DISPLAY_MODE_PARTnumber0编辑框显示为半屏。
DISPLAY_MODE_FULLnumber1编辑框显示为全屏。
CURSOR_UP9+number1光标上移。
CURSOR_DOWN9+number2光标下移。
CURSOR_LEFT9+number3光标左移。
CURSOR_RIGHT9+number4光标右移。
WINDOW_TYPE_INPUT_METHOD_FLOAT9+number2105输入法应用窗口风格标识。

inputMethodEngine.getInputMethodAbility9+

getInputMethodAbility(): InputMethodAbility

获取输入法应用客户端实例InputMethodAbility,仅支持输入法应用调用。
输入法应用获取该实例后,可订阅软键盘显示/隐藏请求事件、创建/销毁输入法面板等。

系统能力: SystemCapability.MiscServices.InputMethodFramework

返回值:

类型说明
InputMethodAbility输入法应用客户端。

示例:

let InputMethodAbility = inputMethodEngine.getInputMethodAbility();

inputMethodEngine.getKeyboardDelegate9+

getKeyboardDelegate(): KeyboardDelegate

获取客户端编辑事件监听代理实例KeyboardDelegate
输入法应用获取该实例后,可订阅物理键盘按键事件、选中文本变化事件等。

系统能力: SystemCapability.MiscServices.InputMethodFramework

返回值:

类型说明
KeyboardDelegate客户端编辑事件监听代理。

示例:

let KeyboardDelegate = inputMethodEngine.getKeyboardDelegate();

inputMethodEngine.getInputMethodEngine(deprecated)

getInputMethodEngine(): InputMethodEngine

获取输入法应用客户端实例InputMethodEngine
输入法应用获取该实例后,可订阅软键盘显示/隐藏请求事件等。

说明:

从API version 8开始支持,API version 9开始废弃,建议使用getInputMethodAbility()替代。

系统能力: SystemCapability.MiscServices.InputMethodFramework

返回值:

类型说明
InputMethodEngine输入法应用客户端。

示例:

let InputMethodEngine = inputMethodEngine.getInputMethodEngine();

inputMethodEngine.createKeyboardDelegate(deprecated)

createKeyboardDelegate(): KeyboardDelegate

获取客户端编辑事件监听代理实例KeyboardDelegate。输入法应用获取该实例后,可订阅物理键盘按键事件、选中文本变化事件等。

说明:

从API version 8开始支持,API version 9开始废弃,建议使用getKeyboardDelegate()替代。

系统能力: SystemCapability.MiscServices.InputMethodFramework

返回值:

类型说明
KeyboardDelegate客户端编辑事件监听代理。

示例:

let keyboardDelegate = inputMethodEngine.createKeyboardDelegate();

InputMethodEngine

下列API均需使用getInputMethodEngine获取到InputMethodEngine实例后,通过实例调用。

on('inputStart')

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

订阅输入法绑定成功事件。使用callback异步回调。

系统能力: SystemCapability.MiscServices.InputMethodFramework

参数:

参数名类型必填说明
typestring设置监听类型,固定取值为'inputStart'。
callback(kbController: KeyboardController, textInputClient: TextInputClient) => void回调函数,返回订阅输入法的KeyboardController和TextInputClient实例。

示例:

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

取消订阅输入法绑定成功事件。

系统能力: SystemCapability.MiscServices.InputMethodFramework

参数:

参数名类型必填说明
typestring设置监听类型,固定取值为'inputStart'。
callback(kbController: KeyboardController, textInputClient: TextInputClient) => void取消订阅的回调函数。参数不填写时,取消订阅type对应的所有回调事件。

示例:

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

订阅输入法软键盘显示或隐藏事件。使用callback异步回调。

系统能力: SystemCapability.MiscServices.InputMethodFramework

参数:

参数名类型必填说明
typestring设置监听类型。
-'keyboardShow'表示显示输入法软键盘。
-'keyboardHide'表示隐藏输入法软键盘。
callback() => void回调函数。

示例:

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

取消订阅输入法软键盘显示或隐藏事件。使用callback异步回调。

系统能力: SystemCapability.MiscServices.InputMethodFramework

参数:

参数名类型必填说明
typestring要取消监听的输入法软键盘类型。
-'keyboardShow'表示显示输入法软键盘。
-'keyboardHide'表示隐藏输入法软键盘。
callback() => void取消订阅的回调函数。参数不填写时,取消订阅type对应的所有回调事件。

示例:

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

InputMethodAbility

下列API均需使用getInputMethodAbility获取到InputMethodAbility实例后,通过实例调用。

on('inputStart')9+

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

订阅输入法绑定成功事件。使用callback异步回调。

系统能力: SystemCapability.MiscServices.InputMethodFramework

参数:

参数名类型必填说明
typestring设置监听类型,固定取值为'inputStart'。
callback(kbController: KeyboardController, inputClient: InputClient) => void回调函数,返回输入法操作相关实例。

示例:

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

取消订阅输入法绑定成功事件。使用callback异步回调。

系统能力: SystemCapability.MiscServices.InputMethodFramework

参数:

参数名类型必填说明
typestring设置监听类型,固定取值为'inputStart'。
callback(kbController: KeyboardController, inputClient: InputClient) => void取消订阅的回调函数。参数不填写时,取消订阅type对应的所有回调事件。

示例:

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

on('inputStop')9+

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

订阅停止输入法应用事件。使用callback异步回调。

系统能力: SystemCapability.MiscServices.InputMethodFramework

参数:

参数名类型必填说明
typestring设置监听类型,固定取值为'inputStop'。
callback() => void回调函数。

示例:

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

off('inputStop')9+

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

取消订阅停止输入法应用事件。使用callback异步回调。

系统能力: SystemCapability.MiscServices.InputMethodFramework

参数:

参数名类型必填说明
typestring设置监听类型,固定取值为'inputStop'。
callback() => void取消订阅的回调函数。参数不填写时,取消订阅type对应的所有回调事件。

示例:

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

on('setCallingWindow')9+

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

订阅设置调用窗口事件。使用callback异步回调。

系统能力: SystemCapability.MiscServices.InputMethodFramework

参数:

参数名类型必填说明
typestring设置监听类型,固定取值为'setCallingWindow'。
callback(wid: number) => void回调函数,返回调用方窗口的Id。

示例:

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

off('setCallingWindow')9+

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

取消订阅设置调用窗口事件。使用callback异步回调。

系统能力: SystemCapability.MiscServices.InputMethodFramework

参数:

参数名类型必填说明
typestring设置监听类型,固定取值为'setCallingWindow'。
callback(wid:number) => void取消订阅的回调函数。参数不填写时,取消订阅type对应的所有回调事件。

示例:

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

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

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

订阅输入法软键盘显示或隐藏事件。使用callback异步回调。

系统能力: SystemCapability.MiscServices.InputMethodFramework

参数:

参数名类型必填说明
typestring设置监听类型。
- 'keyboardShow'表示显示输入法软键盘。
- 'keyboardHide'表示隐藏输入法软键盘。
callback() => void回调函数。

示例:

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

取消订阅输入法事件。使用callback异步回调。

系统能力: SystemCapability.MiscServices.InputMethodFramework

参数:

参数名类型必填说明
typestring设置监听类型。
- 'keyboardShow'表示显示键盘。
- 'keyboardHide'表示隐藏键盘。
callback() => void回调函数。

示例:

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

订阅设置输入法子类型事件。使用callback异步回调。

系统能力: SystemCapability.MiscServices.InputMethodFramework

参数:

参数名类型必填说明
typestring设置监听类型,固定取值为'setSubtype'。
callback(inputMethodSubtype: InputMethodSubtype) => void回调函数,返回设置的输入法子类型。

示例:

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

off('setSubtype')9+

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

取消订阅输入法软键盘显示或隐藏事件。使用callback异步回调。

系统能力: SystemCapability.MiscServices.InputMethodFramework

参数:

参数名类型必填说明
typestring设置监听类型,固定取值为'setSubtype'。
callback(inputMethodSubtype: InputMethodSubtype) => void取消订阅的回调函数。参数不填写时,取消订阅type对应的所有回调事件。

示例:

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

createPanel10+

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

创建输入法面板,仅支持输入法应用调用。使用callback异步回调。
单个输入法应用仅允许创建一个软键盘类型状态栏类型的面板。

系统能力: SystemCapability.MiscServices.InputMethodFramework

参数:

参数名类型必填说明
ctxBaseContext当前输入法应用上下文信息。
infoPanelInfo输入法应用信息。
callbackAsyncCallback<Panel>回调函数。当输入法面板创建成功,返回当前创建的输入法面板对象。

错误码:

错误码ID错误信息
12800004not an input method extension.

示例:

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>

创建输入法面板,仅支持输入法应用调用。使用promise异步回调。
单个输入法应用仅允许创建一个软键盘类型状态栏类型的面板。

系统能力: SystemCapability.MiscServices.InputMethodFramework

参数:

参数名类型必填说明
ctxBaseContext当前输入法应用上下文信息。
infoPanelInfo输入法面板信息。

返回值:

类型说明
Promise<Panel>回调函数。当输入法面板创建成功,返回当前创建的输入法面板对象。

错误码:

错误码ID错误信息
12800004not an input method extension.

示例:

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;

销毁输入法面板。使用callback异步回调。

系统能力: SystemCapability.MiscServices.InputMethodFramework

参数:

参数名类型必填说明
panelPanel要销毁的面板对象。
callbackAsyncCallback<void>回调函数。当输入法面板销毁成功,err为undefined,否则为错误对象。

示例:

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>;

销毁输入法面板。使用promise异步回调。

系统能力: SystemCapability.MiscServices.InputMethodFramework

参数:

参数名类型必填说明
panelPanel要销毁的面板对象。

返回值:

类型说明
Promise<void>无返回结果的Promise对象。

示例:

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

下列API均需使用getKeyboardDelegate获取到KeyboardDelegate实例后,通过实例调用。

on('keyDown'|'keyUp')

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

订阅硬键盘(即物理键盘)上物理按键的按下或抬起事件。使用callback异步回调。

系统能力: SystemCapability.MiscServices.InputMethodFramework

参数:

参数名类型必填说明
typestring设置监听类型。
- 'keyDown'表示键盘按下。
- 'keyUp'表示键盘抬起。
callback(event: KeyEvent) => boolean回调函数,返回按键信息。

示例:

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

取消订阅硬键盘(即物理键盘)上物理按键的按下或抬起事件。使用callback异步回调。

系统能力: SystemCapability.MiscServices.InputMethodFramework

参数:

参数名类型必填说明
typestring设置监听类型。
- 'keyDown'表示键盘按下。
- 'keyUp'表示键盘抬起。
callback(event: KeyEvent) => boolean取消订阅的回调函数。参数不填写时,取消订阅type对应的所有回调事件。

示例:

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

订阅硬键盘(即物理键盘)事件。使用callback异步回调。

系统能力: SystemCapability.MiscServices.InputMethodFramework

参数:

参数名类型必填说明
typestring设置监听类型,固定取值为'keyEvent'。
callbackfunction回调函数,入参为按键事件信息,返回值类型为布尔类型。
- 入参按键事件信息的数据类型为InputKeyEvent
- 若按键事件被事件订阅者消费,则callback应返回true,否则返回false。

示例:

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

取消订阅硬键盘(即物理键盘)事件。使用callback异步回调。

系统能力: SystemCapability.MiscServices.InputMethodFramework

参数:

参数名类型必填说明
typestring设置监听类型,固定取值为'keyEvent'。
callbackfunction取消订阅的回调函数。参数不填写时,取消订阅type对应的所有回调事件。

示例:

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

订阅光标变化事件。使用callback异步回调。

系统能力: SystemCapability.MiscServices.InputMethodFramework

参数:

参数名类型必填说明
typestring光标变化事件,固定取值为'cursorContextChange'。
callback(x: number, y: number, height: number) => void回调函数,返回光标信息。
-x为光标上端的的x坐标值,y为光标上端的y坐标值,height为光标的高度值。

示例:

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

取消订阅光标变化事件。使用callback异步回调。

系统能力: SystemCapability.MiscServices.InputMethodFramework

参数:

参数名类型必填说明
typestring光标变化事件,固定取值为'cursorContextChange'
callback(x: number, y:number, height:number) => void取消订阅的回调函数。参数不填写时,取消订阅type对应的所有回调事件。

示例:

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

订阅文本选择范围变化事件。使用callback异步回调。

系统能力: SystemCapability.MiscServices.InputMethodFramework

参数:

参数名类型必填说明
typestring文本选择变化事件,固定取值为'selectionChange'。
callback(oldBegin: number, oldEnd: number, newBegin: number, newEnd: number) => void回调函数,返回文本选择信息。
- oldBegin为变化前被选中文本的起始下标,oldEnd为变化前被选中文本的终止下标。
- newBegin为变化后被选中文本的起始下标,newEnd为变化后被选中文本的终止下标。

示例:

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

取消订阅文本选择范围变化事件。使用callback异步回调。

系统能力: SystemCapability.MiscServices.InputMethodFramework

参数:

参数名类型必填说明
typestring文本选择变化事件,固定取值为'selectionChange'。
callback(oldBegin: number, oldEnd: number, newBegin: number, newEnd: number) => void取消订阅的回调函数。参数不填写时,取消订阅type对应的所有回调事件。

示例:

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

订阅文本内容变化事件。使用callback异步回调。

系统能力: SystemCapability.MiscServices.InputMethodFramework

参数:

参数名类型必填说明
typestring文本变化事件,固定取值为'textChange'。
callback(text: string) => void回调函数,返回订阅的文本内容。

示例:

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

off('textChange')

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

取消订阅文本内容变化事件。使用callback异步回调。

系统能力: SystemCapability.MiscServices.InputMethodFramework

参数:

参数名类型必填说明
typestring文本变化事件,固定取值为'textChange'。
callback(text: string) => void取消订阅的回调函数。参数不填写时,取消订阅type对应的所有回调事件。

示例:

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

on('editorAttributeChanged')10+

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

订阅编辑框属性变化事件。使用callback异步回调。

系统能力: SystemCapability.MiscServices.InputMethodFramework

参数:

参数名类型必填说明
typestring文本变化事件,固定取值为'editorAttributeChanged'。
callback(attr: EditorAttribute) => void回调函数,返回变化的编辑框属性。

示例:

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

取消订阅编辑框属性变化事件。使用callback异步回调。

系统能力: SystemCapability.MiscServices.InputMethodFramework

参数:

参数名类型必填说明
typestring文本变化事件,固定取值为'editorAttributeChanged'。
callback(attr: EditorAttribute) => void所要取消订阅的回调处理函数。参数不填写时,取消订阅type对应的所有回调事件。

示例:

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

Panel10+

下列API均需使用createPanel获取到Panel实例后,通过实例调用。

setUiContent10+

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

为当前的输入法面板加载具体页面内容,使用callback异步回调。

系统能力: SystemCapability.MiscServices.InputMethodFramework

参数:

参数名类型必填说明
pathstring具体页面的路径。
callbackAsyncCallback<void>回调函数。当面板页面内容加载成功,err为undefined,否则err为错误对象。

示例:

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>

为当前的输入法面板加载具体页面内容,使用Promise异步回调。

系统能力: SystemCapability.MiscServices.InputMethodFramework

参数:

参数名类型必填说明
pathstring具体页面的路径。

返回值:

类型说明
Promise<void>无返回结果的Promise对象。

示例:

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

为当前的输入法面板加载与LocalStorage相关联的具体页面内容,使用callback异步回调。

系统能力: SystemCapability.MiscServices.InputMethodFramework

参数:

参数名类型必填说明
pathstringLocalStorage相关联的具体页面的路径。
storageLocalStorage存储单元,为应用程序范围内的可变和不可变状态属性提供存储。
callbackAsyncCallback<void>回调函数。当面板页面内容加载成功,err为undefined,否则err为错误对象。

示例:

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>

为当前面板加载与LocalStorage相关联的具体页面内容,使用Promise异步回调。

系统能力: SystemCapability.MiscServices.InputMethodFramework

参数:

参数名类型必填说明
pathstring设置加载页面的路径。
storageLocalStorage存储单元,为应用程序范围内的可变状态属性和非可变状态属性提供存储。

返回值:

类型说明
Promise<void>无返回结果的Promise对象。

示例:

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

改变当前输入法面板的大小,使用callback异步回调。

说明

面板宽度不超出屏幕宽度,面板高度不高于屏幕高度的0.6倍。

系统能力: SystemCapability.MiscServices.InputMethodFramework

参数:

参数名类型必填说明
widthnumber目标面板的宽度,单位为px。
heightnumber目标面板的高度,单位为px。
callbackAsyncCallback<void>回调函数。当面板大小改变成功,err为undefined,否则err为错误对象。

示例:

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>;

改变当前输入法面板的大小,使用Promise异步回调。

说明

面板宽度不超出屏幕宽度,面板高度不高于屏幕高度的0.6倍。

系统能力: SystemCapability.MiscServices.InputMethodFramework

参数:

参数名类型必填说明
widthnumber目标面板的宽度,单位为px。
heightnumber目标面板的高度,单位为px。

返回值:

类型说明
Promise<void>无返回结果的Promise对象。

示例:

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

移动面板位置,使用callback异步回调。面板状态为固定态时,不产生实际移动效果。

系统能力: SystemCapability.MiscServices.InputMethodFramework

参数:

参数名类型必填说明
xnumberx轴方向移动的值,值大于0表示右移,单位为px。
ynumbery轴方向移动的值,值大于0表示下移,单位为px。
callbackAsyncCallback<void>回调函数。当面板位置移动成功,err为undefined,否则err为错误对象。

示例:

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>

移动面板位置,使用promise异步回调。面板状态为固定态时,不产生实际移动效果。

系统能力: SystemCapability.MiscServices.InputMethodFramework

参数:

参数名类型必填说明
xnumberx轴方向移动的值,值大于0表示右移,单位为px。
ynumbery轴方向移动的值,值大于0表示下移,单位为px。

返回值:

类型说明
Promise<void>无返回结果的Promise对象。

示例:

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

显示当前输入法面板,使用callback异步回调。

系统能力: SystemCapability.MiscServices.InputMethodFramework

参数:

参数名类型必填说明
callbackAsyncCallback<void>回调函数。当面板显示成功,err为undefined,否则err为错误对象。

示例:

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>

显示当前输入法面板,使用promise异步回调。

系统能力: SystemCapability.MiscServices.InputMethodFramework

返回值:

类型说明
Promise<void>无返回结果的Promise对象。

示例:

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

隐藏当前输入法面板,使用callback异步回调。

系统能力: SystemCapability.MiscServices.InputMethodFramework

参数:

参数名类型必填说明
callbackAsyncCallback<void>回调函数。当面板隐藏成功,err为undefined,否则err为错误对象。

示例:

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>

隐藏当前输入法面板,使用promise异步回调。

系统能力: SystemCapability.MiscServices.InputMethodFramework

返回值:

类型说明
Promise<void>无返回结果的Promise对象。

示例:

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

监听当前面板显示状态,使用callback异步回调。

系统能力: SystemCapability.MiscServices.InputMethodFramework

参数:

参数名类型必填说明
typestring监听当前面板的状态类型,固定取值为'show'。
callback() => void回调函数。

示例:

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

on('hide')10+

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

监听当前面板隐藏状态,使用callback异步回调。

系统能力: SystemCapability.MiscServices.InputMethodFramework

参数:

参数名类型必填说明
typestring监听当前面板的状态类型,固定取值为'hide'。
callback() => void回调函数。

示例:

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

off('show')10+

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

取消监听当前输入法面板的隐藏状态,使用callback异步回调。

系统能力: SystemCapability.MiscServices.InputMethodFramework

参数:

参数名类型必填说明
typestring取消监听当前面板的状态类型,固定取值为'show'。
callback() => void取消订阅的回调函数。参数不填写时,取消订阅type对应的所有回调事件。

示例:

panel.off('show');

off('hide')10+

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

取消监听当前面板隐藏状态,使用callback异步回调。

系统能力: SystemCapability.MiscServices.InputMethodFramework

参数:

参数名类型必填说明
typestring要取消监听的当前面板状态类型,固定取值为'hide'。
callback() => void取消订阅的回调函数。参数不填写时,取消订阅type对应的所有回调事件。

示例:

panel.off('hide');

changeFlag10+

changeFlag(flag: PanelFlag): void

将输入法应用的面板状态改变为固定态或者悬浮态,仅对SOFT_KEYBOARD生效。

系统能力: SystemCapability.MiscServices.InputMethodFramework

参数:

参数名类型必填说明
flagPanelFlag目标面板状态类型。

示例:

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

KeyboardController

下列API均需使用on('inputStart')获取到KeyboardController实例后,通过实例调用。

hide9+

hide(callback: AsyncCallback<void>): void

隐藏输入法。使用callback异步回调。

系统能力: SystemCapability.MiscServices.InputMethodFramework

参数:

参数名类型必填说明
callbackAsyncCallback<void>回调函数。当输入法隐藏成功,err为undefined,否则为错误对象。

错误码:

以下错误码的详细介绍请参见输入法框架错误码

错误码ID错误信息
12800003input method client error.

示例:

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>

隐藏输入法。使用promise异步回调。

系统能力: SystemCapability.MiscServices.InputMethodFramework

返回值:

类型说明
Promise<void>无返回结果的Promise对象。

错误码:

以下错误码的详细介绍请参见输入法框架错误码

错误码ID错误信息
12800003input method client error.

示例:

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

隐藏输入法。使用callback异步回调。

说明:

从API version 8开始支持,API version 9开始废弃,建议使用hide替代。

系统能力: SystemCapability.MiscServices.InputMethodFramework

参数:

参数名类型必填说明
callbackAsyncCallback<void>回调函数。当输入法隐藏成功,err为undefined,否则为错误对象。

示例:

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>

隐藏输入法。使用promise异步回调。

说明:

从API version 8开始支持,API version 9开始废弃,建议使用hide替代。

系统能力: SystemCapability.MiscServices.InputMethodFramework

返回值:

类型说明
Promise<void>无返回结果的Promise对象。

示例:

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

exitCurrentInputType11+

exitCurrentInputType(callback: AsyncCallback<void>): void

退出当前输入类型,仅支持系统配置的默认输入法应用调用。使用callback异步回调。

系统能力: SystemCapability.MiscServices.InputMethodFramework

参数:

参数名类型必填说明
callbackAsyncCallback<void>回调函数。当退出当前输入类型成功,err为undefined,否则为错误对象。

错误码:

以下错误码的详细介绍请参见输入法框架错误码

错误码ID错误信息
12800008input method manager service error.
12800010not default input method configured by system.

示例:

keyboardController.exitCurrentInputType((err: BusinessError) => {
  if (err) {
    console.error(`Failed to exitCurrentInputType: ${JSON.stringify(err)}`);
    return;
  }
  console.log('Succeeded in exiting current input type.');
});

exitCurrentInputType11+

exitCurrentInputType(): Promise<void>

退出当前输入类型,仅支持系统配置的默认输入法应用调用。使用promise异步回调。

系统能力: SystemCapability.MiscServices.InputMethodFramework

返回值:

类型说明
Promise<void>无返回结果的Promise对象。

错误码:

以下错误码的详细介绍请参见输入法框架错误码

错误码ID错误信息
12800008input method manager service error.
12800010not default input method configured by system.

示例:

keyboardController.exitCurrentInputType().then(() => {
  console.log('Succeeded in exiting current input type.');
}).catch((err: BusinessError) => {
  console.log(`Failed to exit current input type: ${JSON.stringify(err)}`);
});

ExtendAction10+

编辑框中文本的扩展编辑操作类型,如剪切、复制等。

系统能力: SystemCapability.MiscServices.InputMethodFramework

名称说明
SELECT_ALL0全选。
CUT3剪切。
COPY4复制。
PASTE5粘贴。

Direction10+

光标的移动方向。

系统能力: SystemCapability.MiscServices.InputMethodFramework

名称说明
CURSOR_UP1向上。
CURSOR_DOWN2向下。
CURSOR_LEFT3向左。
CURSOR_RIGHT4向右。

Range10+

选中的文本范围。

系统能力: SystemCapability.MiscServices.InputMethodFramework

名称类型可读可写说明
startnumber选中文本的首字符在编辑框的索引值。
endnumber选中文本的末字符在编辑框的索引值。

Movement10+

选中文本时,光标移动的方向

系统能力: SystemCapability.MiscServices.InputMethodFramework

名称类型可读可写说明
directionDirection选中文本时,光标的移动方向。

InputClient9+

下列API均需使用on('inputStart')获取到InputClient实例后,通过实例调用。

sendKeyFunction9+

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

发送功能键。使用callback异步回调。

系统能力: SystemCapability.MiscServices.InputMethodFramework

参数:

参数名类型必填说明
actionnumber功能键键值。
- 当值为0时,表示无效按键。
- 当值为1时,表示确认键(即回车键)。
callbackAsyncCallback<boolean>回调函数。当功能键发送成功,err为undefined,data为true;否则为错误对象。

错误码:

以下错误码的详细介绍请参见输入法框架错误码

错误码ID错误信息
12800003input method client error.

示例:

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>

发送功能键。使用promise异步回调。

系统能力: SystemCapability.MiscServices.InputMethodFramework

参数:

参数名类型必填说明
actionnumber功能键键值。
当值为0时,表示无效按键;
当值为1时,表示确认键(即回车键)。

返回值:

类型说明
Promise<boolean>Promise对象。返回true表示功能键发送成功;返回false表示功能键发送失败。

错误码:

以下错误码的详细介绍请参见输入法框架错误码

错误码ID错误信息
12800003input method client error.

示例:

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

获取光标前固定长度的文本。使用callback异步回调。

系统能力: SystemCapability.MiscServices.InputMethodFramework

参数:

参数名类型必填说明
lengthnumber文本长度。
callbackAsyncCallback<string>回调函数。当光标前固定长度的文本获取成功,err为undefined,data为获取到的文本;否则为错误对象。

错误码:

以下错误码的详细介绍请参见输入法框架错误码

错误码ID错误信息
12800003input method client error.
12800006Input method controller error.

示例:

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>

获取光标前固定长度的文本。使用promise异步回调。

系统能力: SystemCapability.MiscServices.InputMethodFramework

参数:

参数名类型必填说明
lengthnumber文本长度。

返回值:

类型说明
Promise<string>Promise对象,返回光标前固定长度的文本。

错误码:

以下错误码的详细介绍请参见输入法框架错误码

错误码ID错误信息
12800003input method client error.
12800006Input method controller error.

示例:

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

获取光标前固定长度的文本。

系统能力: SystemCapability.MiscServices.InputMethodFramework

参数:

参数名类型必填说明
lengthnumber文本长度。

返回值:

类型说明
string返回光标前固定长度的文本。

错误码:

以下错误码的详细介绍请参见输入法框架错误码

错误码ID错误信息
12800003input method client error.
12800006input method controller error.

示例:

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

获取光标后固定长度的文本。使用callback异步回调。

系统能力: SystemCapability.MiscServices.InputMethodFramework

参数:

参数名类型必填说明
lengthnumber文本长度。
callbackAsyncCallback<string>回调函数。当光标后固定长度的文本获取成功,err为undefined,data为获取到的文本;否则为错误对象。

错误码:

以下错误码的详细介绍请参见输入法框架错误码

错误码ID错误信息
12800003input method client error.
12800006Input method controller error.

示例:

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>

获取光标后固定长度的文本。使用promise异步回调。

系统能力: SystemCapability.MiscServices.InputMethodFramework

参数:

参数名类型必填说明
lengthnumber文本长度。

返回值:

类型说明
Promise<string>Promise对象,返回光标后固定长度的文本。

错误码:

以下错误码的详细介绍请参见输入法框架错误码

错误码ID错误信息
12800003input method client error.
12800006Input method controller error.

示例:

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

获取光标后固定长度的文本。

系统能力: SystemCapability.MiscServices.InputMethodFramework

参数:

参数名类型必填说明
lengthnumber文本长度。

返回值:

类型说明
string返回光标后固定长度的文本。

错误码:

以下错误码的详细介绍请参见输入法框架错误码

错误码ID错误信息
12800003input method client error.
12800006input method controller error.

示例:

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

删除光标前固定长度的文本。使用callback异步回调。

系统能力: SystemCapability.MiscServices.InputMethodFramework

参数:

参数名类型必填说明
lengthnumber文本长度。
callbackAsyncCallback<boolean>回调函数。当光标前固定长度的文本删除成功,err为undefined,data为true;否则为错误对象。

错误码:

以下错误码的详细介绍请参见输入法框架错误码

错误码ID错误信息
12800002Input method engine error.
12800003input method client error.

示例:

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>

删除光标前固定长度的文本。使用promise异步回调。

系统能力: SystemCapability.MiscServices.InputMethodFramework

参数:

参数名类型必填说明
lengthnumber文本长度。

返回值:

类型说明
Promise<boolean>Promise对象。返回true表示删除光标前固定长度的文本成功;返回false表示删除光标前固定长度的文本失败。

错误码:

以下错误码的详细介绍请参见输入法框架错误码

错误码ID错误信息
12800002Input method engine error.
12800003input method client error.

示例:

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

删除光标前固定长度的文本。

系统能力: SystemCapability.MiscServices.InputMethodFramework

参数:

参数名类型必填说明
lengthnumber文本长度。

错误码:

以下错误码的详细介绍请参见输入法框架错误码

错误码ID错误信息
12800002input method engine error.
12800003input method client error.

示例:

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

删除光标后固定长度的文本。使用callback异步回调。

系统能力: SystemCapability.MiscServices.InputMethodFramework

参数:

参数名类型必填说明
lengthnumber文本长度。
callbackAsyncCallback<boolean>回调函数。当光标后固定长度的文本删除成功,err为undefined,data为true;否则为错误对象。

错误码:

以下错误码的详细介绍请参见输入法框架错误码

错误码ID错误信息
12800002Input method engine error.
12800003input method client error.

示例:

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>

删除光标后固定长度的文本。使用promise异步回调。

系统能力: SystemCapability.MiscServices.InputMethodFramework

参数:

参数名类型必填说明
lengthnumber文本长度。

返回值:

类型说明
Promise<boolean>Promise对象。返回true表示删除光标后固定长度的文本成功;返回false表示删除光标后固定长度的文本失败。

错误码:

以下错误码的详细介绍请参见输入法框架错误码

错误码ID错误信息
12800002Input method engine error.
12800003input method client error.

示例:

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

删除光标后固定长度的文本。

系统能力: SystemCapability.MiscServices.InputMethodFramework

参数:

参数名类型必填说明
lengthnumber文本长度。

错误码:

以下错误码的详细介绍请参见输入法框架错误码

错误码ID错误信息
12800002input method engine error.
12800003input method client error.

示例:

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

插入文本。使用callback异步回调。

系统能力: SystemCapability.MiscServices.InputMethodFramework

参数:

参数名类型必填说明
textstring文本内容。
callbackAsyncCallback<boolean>回调函数。当文本插入成功,err为undefined,data为true;否则为错误对象。

错误码:

以下错误码的详细介绍请参见输入法框架错误码

错误码ID错误信息
12800002Input method engine error.
12800003input method client error.

示例:

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>

插入文本。使用promise异步回调。

系统能力: SystemCapability.MiscServices.InputMethodFramework

参数:

参数名类型必填说明
textstring文本。

返回值:

类型说明
Promise<boolean>Promise对象。返回true表示插入文本成功;返回false表示插入文本失败。

错误码:

以下错误码的详细介绍请参见输入法框架错误码

错误码ID错误信息
12800002Input method engine error.
12800003input method client error.

示例:

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

插入文本。

系统能力: SystemCapability.MiscServices.InputMethodFramework

参数:

参数名类型必填说明
textstring文本内容。

错误码:

以下错误码的详细介绍请参见输入法框架错误码

错误码ID错误信息
12800002input method engine error.
12800003input method client error.

示例:

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

获取编辑框属性值。使用callback异步回调。

系统能力: SystemCapability.MiscServices.InputMethodFramework

参数:

参数名类型必填说明
callbackAsyncCallback<EditorAttribute>回调函数。当编辑框属性值获取成功,err为undefined,data为编辑框属性值;否则为错误对象。

错误码:

以下错误码的详细介绍请参见输入法框架错误码

错误码ID错误信息
12800003input method client error.

示例:

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>

获取编辑框属性值。使用promise异步回调。

系统能力: SystemCapability.MiscServices.InputMethodFramework

返回值:

类型说明
Promise<EditorAttribute>Promise对象,返回编辑框属性值。

错误码:

以下错误码的详细介绍请参见输入法框架错误码

错误码ID错误信息
12800003input method client error.

示例:

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

获取编辑框属性值。

系统能力: SystemCapability.MiscServices.InputMethodFramework

返回值:

类型说明
EditorAttribute编辑框属性对象

错误码:

以下错误码的详细介绍请参见输入法框架错误码

错误码ID错误信息
12800003input method client error.

示例:

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

移动光标。使用callback异步回调。

系统能力: SystemCapability.MiscServices.InputMethodFramework

参数:

参数名类型必填说明
directionnumber光标移动方向。
- 当值为1时,表示向上。
- 当值为2时,表示向下。
- 当值为3时,表示向左。
- 当值为4时,表示向右。
callbackAsyncCallback<void>回调函数。当光标移动成功,err为undefined,否则为错误对象。

错误码:

以下错误码的详细介绍请参见输入法框架错误码

错误码ID错误信息
12800003input method client error.

示例:

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>

移动光标。使用promise异步回调。

系统能力: SystemCapability.MiscServices.InputMethodFramework

参数:

参数名类型必填说明
directionnumber光标移动方向。
- 当值为1时,表示向上。
- 当值为2时,表示向下。
- 当值为3时,表示向左。
- 当值为4时,表示向右。

返回值:

类型说明
Promise<void>无返回结果的Promise对象。

错误码:

以下错误码的详细介绍请参见输入法框架错误码

错误码ID错误信息
12800003input method client error.

示例:

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

移动光标。

系统能力: SystemCapability.MiscServices.InputMethodFramework

参数:

参数名类型必填说明
directionnumber光标移动方向。
- 当值为1时,表示向上。
- 当值为2时,表示向下。
- 当值为3时,表示向左。
- 当值为4时,表示向右。

错误码:

以下错误码的详细介绍请参见输入法框架错误码

错误码ID错误信息
12800003input method client error.

示例:

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

根据索引范围选中文本。使用callback异步回调。

系统能力: SystemCapability.MiscServices.InputMethodFramework

参数:

参数名类型必填说明
rangeRange选中文本的范围。
callbackAsyncCallback<void>回调函数。当成功发送选中事件后,err为undefined,否则为错误对象。

错误码:

以下错误码的详细介绍请参见输入法框架错误码

错误码ID错误信息
401parameter error.
12800003input method client error.

示例:

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>

根据索引范围选中文本。使用promise异步回调。

系统能力: SystemCapability.MiscServices.InputMethodFramework

参数:

参数名类型必填说明
rangeRange选中文本的范围。

返回值:

类型说明
Promise<void>无返回结果的Promise对象。

错误码:

以下错误码的详细介绍请参见输入法框架错误码

错误码ID错误信息
401parameter error.
12800003input method client error.

示例:

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

根据索引范围选中文本。

系统能力: SystemCapability.MiscServices.InputMethodFramework

参数:

参数名类型必填说明
rangeRange选中文本的范围。

错误码:

以下错误码的详细介绍请参见输入法框架错误码

错误码ID错误信息
401parameter error.
12800003input method client error.

示例:

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

根据光标移动方向选中文本。使用callback异步回调。

系统能力: SystemCapability.MiscServices.InputMethodFramework

参数:

参数名类型必填说明
movementMovement选中时光标移动的方向。
callbackAsyncCallback<void>回调函数。当成功发送选中事件后,err为undefined,否则为错误对象。

错误码:

以下错误码的详细介绍请参见输入法框架错误码

错误码ID错误信息
401parameter error.
12800003input method client error.

示例:

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>

根据索引范围选中文本。使用promise异步回调。

系统能力: SystemCapability.MiscServices.InputMethodFramework

参数:

参数名类型必填说明
movementMovement选中时光标移动的方向。

返回值:

类型说明
Promise<void>无返回结果的Promise对象。

错误码:

以下错误码的详细介绍请参见输入法框架错误码

错误码ID错误信息
401parameter error.
12800003input method client error.

示例:

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

根据光标移动方向选中文本。

系统能力: SystemCapability.MiscServices.InputMethodFramework

参数:

参数名类型必填说明
movementMovement选中时光标移动的方向。

错误码:

以下错误码的详细介绍请参见输入法框架错误码

错误码ID错误信息
401parameter error.
12800003input method client error.

示例:

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

获取光标所在处的文本索引。使用callback异步回调。

系统能力: SystemCapability.MiscServices.InputMethodFramework

参数:

参数名类型必填说明
callbackAsyncCallback<number>回调函数。当文本索引获取成功,err为undefined,否则为错误对象。

错误码:

以下错误码的详细介绍请参见输入法框架错误码

错误码ID错误信息
12800003input method client error.
12800006Input method controller error.

示例:

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>

获取光标所在处的文本索引。使用promise异步回调。

系统能力: SystemCapability.MiscServices.InputMethodFramework

返回值:

类型说明
Promise<number>Promise对象,返回光标所在处的文本索引。

错误码:

以下错误码的详细介绍请参见输入法框架错误码

错误码ID错误信息
12800003input method client error.
12800006Input method controller error.

示例:

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

获取光标所在处的文本索引。

系统能力: SystemCapability.MiscServices.InputMethodFramework

返回值:

类型说明
number返回光标所在处的文本索引。

错误码:

以下错误码的详细介绍请参见输入法框架错误码

错误码ID错误信息
12800003input method client error.
12800006Input method controller error.

示例:

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

发送扩展编辑操作。使用callback异步回调。

说明

输入法应用调用该接口向编辑框发送扩展编辑操作,编辑框监听相应事件on('handleExtendAction'),从而进一步做出处理。

系统能力: SystemCapability.MiscServices.InputMethodFramework

参数:

参数名类型必填说明
actionExtendAction要发送的扩展操作。
callbackAsyncCallback<void>回调函数。发送成功,err为undefined,否则为错误对象。

错误码:

以下错误码的详细介绍请参见输入法框架错误码

错误码ID错误信息
12800003input method client error.
12800006Input method controller error.

示例:

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>

发送扩展编辑操作。使用promise异步回调。

说明

输入法应用调用该接口向编辑框发送扩展编辑操作,编辑框监听相应事件on('handleExtendAction'),从而进一步做出处理。

系统能力: SystemCapability.MiscServices.InputMethodFramework

参数:

参数名类型必填说明
actionExtendAction要发送的扩展操作。

返回值:

类型说明
Promise<void>无返回结果的Promise对象。

错误码:

以下错误码的详细介绍请参见输入法框架错误码

错误码ID错误信息
12800003input method client error.
12800006Input method controller error.

示例:

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

编辑框属性值。

系统能力: SystemCapability.MiscServices.InputMethodFramework

名称类型可读可写说明
enterKeyTypenumber编辑框的功能属性。
inputPatternnumber编辑框的文本属性。

KeyEvent

按键属性值。

系统能力: SystemCapability.MiscServices.InputMethodFramework

名称类型可读可写说明
keyCodenumber按键的键值。键码值说明参考KeyCode
keyActionnumber按键事件类型。
- 当值为2时,表示按下事件;
- 当值为3时,表示抬起事件。

PanelFlag10+

输入法面板状态类型枚举。

系统能力: SystemCapability.MiscServices.InputMethodFramework

名称说明
FLG_FIXED0固定态面板类型。
FLG_FLOATING1悬浮态面板类型。

PanelType10+

输入法面板类型枚举。

系统能力: SystemCapability.MiscServices.InputMethodFramework

名称说明
SOFT_KEYBOARD0软键盘类型。
STATUS_BAR1状态栏类型。

PanelInfo10+

系统能力: SystemCapability.MiscServices.InputMethodFramework

输入法面板属性。

名称类型可读可写说明
typenumber面板的类型。
flagnumber面板的状态类型。

TextInputClient(deprecated)

说明:

从API version 8开始支持,API version 9开始废弃,建议使用InputClient替代。

下列API示例中都需使用on('inputStart')回调获取到TextInputClient实例,再通过此实例调用对应方法。

getForward(deprecated)

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

获取光标前固定长度的文本。使用callback异步回调。

说明:

从API version 8开始支持,API version 9开始废弃,建议使用getForward替代。

系统能力: SystemCapability.MiscServices.InputMethodFramework

参数:

参数名类型必填说明
lengthnumber文本长度。
callbackAsyncCallback<string>回调函数。当光标前固定长度的文本获取成功,err为undefined,data为获取到的文本;否则为错误对象。

示例:

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>

获取光标前固定长度的文本。使用promise异步回调。

说明:

从API version 8开始支持,API version 9开始废弃,建议使用getForward替代。

系统能力: SystemCapability.MiscServices.InputMethodFramework

参数:

参数名类型必填说明
lengthnumber文本长度。

返回值:

类型说明
Promise<string>Promise对象,返回光标前固定长度的文本。

示例:

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

获取光标后固定长度的文本。使用callback异步回调。

说明:

从API version 8开始支持,API version 9开始废弃,建议使用getBackward替代。

系统能力: SystemCapability.MiscServices.InputMethodFramework

参数:

参数名类型必填说明
lengthnumber文本长度。
callbackAsyncCallback<string>回调函数。当光标后固定长度的文本获取成功,err为undefined,data为获取到的文本;否则为错误对象。

示例:

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>

获取光标后固定长度的文本。使用promise异步回调。

说明:

从API version 8开始支持,API version 9开始废弃,建议使用getBackward替代。

系统能力: SystemCapability.MiscServices.InputMethodFramework

参数:

参数名类型必填说明
lengthnumber文本长度。

返回值:

类型说明
Promise<string>Promise对象,返回光标后固定长度的文本。

示例:

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

删除光标前固定长度的文本。使用callback异步回调。

说明:

从API version 8开始支持,API version 9开始废弃,建议使用deleteForward替代。

系统能力: SystemCapability.MiscServices.InputMethodFramework

参数:

参数名类型必填说明
lengthnumber文本长度。
callbackAsyncCallback<boolean>回调函数。当光标前固定长度的文本删除成功,err为undefined,data为true;否则为错误对象。

示例:

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>

删除光标前固定长度的文本。使用promise异步回调。

说明:

从API version 8开始支持,API version 9开始废弃,建议使用deleteForward替代。

系统能力: SystemCapability.MiscServices.InputMethodFramework

参数:

参数名类型必填说明
lengthnumber文本长度。

返回值:

类型说明
Promise<boolean>Promise对象。返回true表示删除光标前固定长度的文本成功;返回false表示删除光标前固定长度的文本失败。

示例:

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

删除光标后固定长度的文本。使用callback异步回调。

说明:

从API version 8开始支持,API version 9开始废弃,建议使用deleteBackward替代。

系统能力: SystemCapability.MiscServices.InputMethodFramework

参数:

参数名类型必填说明
lengthnumber文本长度。
callbackAsyncCallback<boolean>回调函数。当光标后固定长度的文本删除成功,err为undefined,data为true;否则为错误对象。

示例:

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>

删除光标后固定长度的文本。使用promise异步回调。

说明:

从API version 8开始支持,API version 9开始废弃,建议使用deleteBackward替代。

系统能力: SystemCapability.MiscServices.InputMethodFramework

参数:

参数名类型必填说明
lengthnumber文本长度。

返回值:

类型说明
Promise<boolean>Promise对象。返回true表示删除光标后固定长度的文本成功;返回false表示删除光标后固定长度的文本失败。

示例:

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

发送功能键。使用callback异步回调。

说明:

从API version 8开始支持,API version 9开始废弃,建议使用sendKeyFunction替代。

系统能力: SystemCapability.MiscServices.InputMethodFramework

参数:

参数名类型必填说明
actionnumber功能键键值。
- 当值为0时,表示无效按键;
- 当值为1时,表示确认键(即回车键)。
callbackAsyncCallback<boolean>回调函数。当功能键发送成功,err为undefined,data为true;否则为错误对象。

示例:

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>

发送功能键。使用promise异步回调。

说明:

从API version 8开始支持,API version 9开始废弃,建议使用sendKeyFunction替代。

系统能力: SystemCapability.MiscServices.InputMethodFramework

参数:

参数名类型必填说明
actionnumber功能键键值。
当值为0时,表示无效按键;
当值为1时,表示确认键(即回车键)。

返回值:

类型说明
Promise<boolean>Promise对象。返回true表示发送功能键成功;返回false表示发送功能键失败。

示例:

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

插入文本。使用callback异步回调。

说明:

从API version 8开始支持,API version 9开始废弃,建议使用insertText替代。

系统能力: SystemCapability.MiscServices.InputMethodFramework

参数:

参数名类型必填说明
textstring文本。
callbackAsyncCallback<boolean>回调函数。当文本插入成功,err为undefined,data为true;否则为错误对象。

示例:

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>

插入文本。使用promise异步回调。

说明:

从API version 8开始支持,API version 9开始废弃,建议使用insertText替代。

系统能力: SystemCapability.MiscServices.InputMethodFramework

参数:

参数名类型必填说明
textstring文本。

返回值:

类型说明
Promise<boolean>Promise对象。返回true表示插入文本成功;返回false表示插入文本失败。

示例:

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

获取编辑框属性值。使用callback异步回调。

说明:

从API version 8开始支持,API version 9开始废弃,建议使用getEditorAttribute替代。

系统能力: SystemCapability.MiscServices.InputMethodFramework

参数:

参数名类型必填说明
callbackAsyncCallback<EditorAttribute>回调函数。当编辑框的属性值获取成功,err为undefined,data为编辑框属性值;否则为错误对象。

示例:

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>

获取编辑框属性值。使用promise异步回调。

说明:

从API version 8开始支持,API version 9开始废弃,建议使用getEditorAttribute替代。

系统能力: SystemCapability.MiscServices.InputMethodFramework

返回值:

类型说明
Promise<EditorAttribute>Promise对象,返回编辑框属性值。

示例:

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 鸿蒙接口

harmony 鸿蒙系统公共事件定义(待停用)

harmony 鸿蒙系统公共事件定义

harmony 鸿蒙开发说明

harmony 鸿蒙企业设备管理概述(仅对系统应用开放)

harmony 鸿蒙BundleStatusCallback

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

harmony 鸿蒙@ohos.distributedBundle (分布式包管理)

harmony 鸿蒙@ohos.bundle (Bundle模块)

harmony 鸿蒙@ohos.enterprise.EnterpriseAdminExtensionAbility (企业设备管理扩展能力)

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