• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Switching Between Input Methods
2<!--Kit: IME Kit-->
3<!--Subsystem: MiscServices-->
4<!--Owner: @illybyy-->
5<!--Designer: @andeszhang-->
6<!--Tester: @murphy1984-->
7<!--Adviser: @zhang_yixin13-->
8
9You can use the APIs of the input method framework service to easily switch between input methods and input method subtypes.
10
11> **NOTE**
12>
13> 1. The following APIs can be called only in the current input method application.
14>
15> 2. This example assumes that an input method application has been executed. For details about how to implement an input method application, see [Implementing an Input Method Application](./inputmethod-application-guide.md).
16
17## Switching Between Input Method Subtypes
18
191. In the input method application in use, call [switchCurrentInputMethodSubtype](../reference/apis-ime-kit/js-apis-inputmethod.md#inputmethodswitchcurrentinputmethodsubtype9) with the target [InputMethodSubtype](../reference/apis-ime-kit/js-apis-inputmethod-subtype.md#inputmethodsubtype) to switch to another subtype of the current input method.
20
21   ```ts
22   import { InputMethodSubtype, inputMethod } from '@kit.IMEKit';
23
24   export class KeyboardController {
25     async switchCurrentInputMethodSubtype() {
26       let subTypes = await inputMethod.getSetting().listCurrentInputMethodSubtype(); // Obtain all subtypes of the current input method.
27       let currentSubType = inputMethod.getCurrentInputMethodSubtype(); // Obtain the current subtype of the current input method.
28       for(let i=0;i<subTypes.length;i++) {
29         if(subTypes[i].id != currentSubType.id) { // If the current subtype is not the specified one, switch to the specified one. You can enter a fixed subtype as required.
30           await inputMethod.switchCurrentInputMethodSubtype(subTypes[i]);
31         }
32       }
33     }
34   }
35   ```
36
372. Register a listener in the input method application for subtype changes, so as to load a subtype-specific soft keyboard UI.
38
39   ```ts
40   import { InputMethodSubtype, inputMethodEngine, inputMethod } from '@kit.IMEKit';
41
42   export class KeyboardController {
43     async switchCurrentInputMethodSubtype() {
44       let panel: inputMethodEngine.Panel;
45       let inputMethodAbility: inputMethodEngine.InputMethodAbility = inputMethodEngine.getInputMethodAbility();
46       // Register a listener in the input method application for subtype changes.
47       inputMethodAbility.on('setSubtype', (inputMethodSubtype: InputMethodSubtype) => {
48         if(inputMethodSubtype.id == 'InputMethodExtAbility') {
49           panel.setUiContent('pages/Index'); // Assume that the panel has been created in the onCreate process in the input method application.
50         }
51         if(inputMethodSubtype.id == 'InputMethodExtAbility1') {
52           panel.setUiContent('pages/Index1'); // Assume that the panel has been created in the onCreate process in the input method application.
53         }
54       });
55     }
56   }
57
58
59   ```
60
61## Switching Between Input Methods
62
63In the input method application in use, call [switchInputMethod](../reference/apis-ime-kit/js-apis-inputmethod.md#inputmethodswitchinputmethod9) with the target [InputMethodProperty](../reference/apis-ime-kit/js-apis-inputmethod.md#inputmethodproperty8) to switch to another input method.
64
65```ts
66import { inputMethod } from '@kit.IMEKit';
67
68export class KeyboardController {
69  async switchInputMethod(){
70    let inputMethods = await inputMethod.getSetting().getInputMethods(true); // Obtain the list of enabled input methods.
71    let currentInputMethod = inputMethod.getCurrentInputMethod(); // Obtain the current input method.
72    for(let i=0;i<inputMethods.length;i++) {
73      if(inputMethods[i].name != currentInputMethod.name) { // If the current input method is not the specified one, switch to the specified one. You can enter a fixed input method as required.
74        await inputMethod.switchInputMethod(inputMethods[i]);
75      }
76    }
77  }
78}
79```
80
81## Switching Between Input Methods and Subtypes
82
83In the input method application in use, call [switchCurrentInputMethodAndSubtype](../reference/apis-ime-kit/js-apis-inputmethod.md#inputmethodswitchcurrentinputmethodandsubtype9) with the target [InputMethodProperty](../reference/apis-ime-kit/js-apis-inputmethod.md#inputmethodproperty8) and [InputMethodSubtype](../reference/apis-ime-kit/js-apis-inputmethod-subtype.md#inputmethodsubtype) to switch to the subtype of another input method.
84
85```ts
86import { inputMethod } from '@kit.IMEKit';
87
88export class KeyboardController {
89  async switchInputMethodAndSubtype() {
90    let inputMethods = await inputMethod.getSetting().getInputMethods(true); // Obtain the list of enabled input methods.
91    let currentInputMethod = inputMethod.getCurrentInputMethod(); // Obtain the current input method.
92    for (let i = 0;i < inputMethods.length; i++) {
93      if (inputMethods[i].name != currentInputMethod.name) { // If the current input method is not the specified one, switch to the specified one. You can enter a fixed input method as required.
94        let subTypes = await inputMethod.getSetting().listInputMethodSubtype(inputMethods[i]); // Obtain the subtypes of the target input method.
95        await inputMethod.switchCurrentInputMethodAndSubtype(inputMethods[i], subTypes[0]); // This example switches to the first obtained subtype.
96      }
97    }
98  }
99}
100```
101