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 ``` 54 55## Switching Between Input Methods 56 57In 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. 58 59```ts 60import { inputMethod } from '@kit.IMEKit'; 61 62export class KeyboardController { 63 async switchInputMethod(){ 64 let inputMethods = await inputMethod.getSetting().getInputMethods(true); // Obtain the list of enabled input methods. 65 let currentInputMethod = inputMethod.getCurrentInputMethod(); // Obtain the current input method. 66 for(let i=0;i<inputMethods.length;i++) { 67 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. 68 await inputMethod.switchInputMethod(inputMethods[i]); 69 } 70 } 71 } 72} 73``` 74 75## Switching Between Input Methods and Subtypes 76 77In 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. 78 79```ts 80import { inputMethod } from '@kit.IMEKit'; 81 82export class KeyboardController { 83 async switchInputMethodAndSubtype() { 84 let inputMethods = await inputMethod.getSetting().getInputMethods(true); // Obtain the list of enabled input methods. 85 let currentInputMethod = inputMethod.getCurrentInputMethod(); // Obtain the current input method. 86 for (let i = 0;i < inputMethods.length; i++) { 87 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. 88 let subTypes = await inputMethod.getSetting().listInputMethodSubtype(inputMethods[i]); // Obtain the subtypes of the target input method. 89 await inputMethod.switchCurrentInputMethodAndSubtype(inputMethods[i], subTypes[0]); // This example switches to the first obtained subtype. 90 } 91 } 92 } 93} 94``` 95