1# Switching Between Input Methods 2 3You can use the APIs of the input method framework service to easily switch between input methods and input method subtypes. 4 5> **NOTE** 6> 7> 1. The following APIs can be called only in the current input method application. 8> 9> 2. For details about how to implement an input method application, see [Implementing an Input Method Application](./inputmethod-application-guide.md). 10 11## Switching Between Input Method Subtypes 12 131. 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. 14 15 ```ts 16 import { InputMethodSubtype, inputMethod } from '@kit.IMEKit'; 17 18 export class KeyboardController { 19 async switchCurrentInputMethodSubtype() { 20 let subTypes = await inputMethod.getSetting().listCurrentInputMethodSubtype(); // Obtain all subtypes of the current input method. 21 let currentSubType = inputMethod.getCurrentInputMethodSubtype(); // Obtain the current subtype of the current input method. 22 for(let i=0;i<subTypes.length;i++) { 23 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. 24 await inputMethod.switchCurrentInputMethodSubtype(subTypes[i]); 25 } 26 } 27 } 28 } 29 ``` 30 312. Register a listener in the input method application for subtype changes, so as to load a subtype-specific soft keyboard UI. 32 33 ```ts 34 import { InputMethodSubtype, inputMethodEngine, inputMethod } from '@kit.IMEKit'; 35 36 export class KeyboardController { 37 async switchCurrentInputMethodSubtype() { 38 let panel: inputMethodEngine.Panel; 39 let inputMethodAbility: inputMethodEngine.InputMethodAbility = inputMethodEngine.getInputMethodAbility(); 40 // Register a listener in the input method application for subtype changes. 41 inputMethodAbility.on('setSubtype', (inputMethodSubtype: InputMethodSubtype) => { 42 if(inputMethodSubtype.id == 'InputMethodExtAbility') { 43 panel.setUiContent('pages/Index'); // Assume that the panel has been created in the onCreate process in the input method application. 44 } 45 if(inputMethodSubtype.id == 'InputMethodExtAbility1') { 46 panel.setUiContent('pages/Index1'); // Assume that the panel has been created in the onCreate process in the input method application. 47 } 48 }); 49 } 50 } 51 ``` 52 53## Switching Between Input Methods 54 55In 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. 56 57```ts 58import { inputMethod } from '@kit.IMEKit'; 59 60export class KeyboardController { 61 async switchInputMethod(){ 62 let inputMethods = await inputMethod.getSetting().getInputMethods(true); // Obtain the list of enabled input methods. 63 let currentInputMethod = inputMethod.getCurrentInputMethod(); // Obtain the current input method. 64 for(let i=0;i<inputMethods.length;i++) { 65 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. 66 await inputMethod.switchInputMethod(inputMethods[i]); 67 } 68 } 69 } 70} 71``` 72 73## Switching Between Input Methods and Subtypes 74 75In 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. 76 77```ts 78import { inputMethod } from '@kit.IMEKit'; 79 80export class KeyboardController { 81 async switchInputMethodAndSubtype() { 82 let inputMethods = await inputMethod.getSetting().getInputMethods(true); // Obtain the list of enabled input methods. 83 let currentInputMethod = inputMethod.getCurrentInputMethod(); // Obtain the current input method. 84 for (let i = 0;i < inputMethods.length; i++) { 85 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. 86 let subTypes = await inputMethod.getSetting().listInputMethodSubtype(inputMethods[i]); // Obtain the subtypes of the target input method. 87 await inputMethod.switchCurrentInputMethodAndSubtype(inputMethods[i], subTypes[0]); // This example switches to the first obtained subtype. 88 } 89 } 90 } 91} 92``` 93