1/* 2 * Copyright (c) 2024 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16import display from '@ohos.display'; 17import inputMethodEngine from '@ohos.inputMethodEngine'; 18import InputMethodExtensionContext from '@ohos.InputMethodExtensionContext'; 19 20// 调用输入法框架的getInputMethodAbility方法获取实例,并由此实例调用输入法框架功能接口 21const inputMethodAbility: inputMethodEngine.InputMethodAbility = inputMethodEngine.getInputMethodAbility(); 22 23export class KeyboardController1 { 24 private mContext: InputMethodExtensionContext | undefined = undefined; // 保存InputMethodExtensionAbility中的context属性 25 private panel: inputMethodEngine.Panel | undefined = undefined; 26 private textInputClient: inputMethodEngine.InputClient | undefined = undefined; 27 private keyboardController: inputMethodEngine.KeyboardController | undefined = undefined; 28 29 constructor() { 30 } 31 32 public onCreate(context: InputMethodExtensionContext): void 33 { 34 this.mContext = context; 35 this.initWindow(); // 初始化窗口 36 this.registerListener(); // 注册对输入法框架的事件监听 37 } 38 39 public onDestroy(): void // 应用生命周期销毁 40 { 41 this.unRegisterListener(); // 去注册事件监听 42 if(this.panel) { // 销毁窗口 43 this.panel.hide(); 44 inputMethodAbility.destroyPanel(this.panel); 45 } 46 if(this.mContext) { 47 this.mContext.destroy(); 48 } 49 } 50 51 public insertText(text: string): void { 52 if(this.textInputClient) { 53 this.textInputClient.insertText(text); 54 } 55 } 56 57 public deleteForward(length: number): void { 58 if(this.textInputClient) { 59 this.textInputClient.deleteForward(length); 60 } 61 } 62 63 private initWindow(): void // 初始化窗口 64 { 65 if(this.mContext === undefined) { 66 return; 67 } 68 let dis = display.getDefaultDisplaySync(); 69 let dWidth = dis.width; 70 let dHeight = dis.height; 71 let keyHeightRate = 0.47; 72 let keyHeight = dHeight * keyHeightRate; 73 let nonBarPosition = dHeight - keyHeight; 74 let panelInfo: inputMethodEngine.PanelInfo = { 75 type: inputMethodEngine.PanelType.SOFT_KEYBOARD, 76 flag: inputMethodEngine.PanelFlag.FLG_FIXED 77 }; 78 inputMethodAbility.createPanel(this.mContext, panelInfo).then(async (inputPanel: inputMethodEngine.Panel) => { 79 this.panel = inputPanel; 80 if(this.panel) { 81 await this.panel.resize(dWidth, keyHeight); 82 await this.panel.moveTo(0, nonBarPosition); 83 await this.panel.setUiContent('inputmethodextability2/pages/Index'); 84 } 85 }); 86 } 87 88 private registerListener(): void 89 { 90 this.registerInputListener(); // 注册对输入法框架服务的监听 91 // 注册隐藏键盘事件监听等 92 } 93 94 private registerInputListener(): void { // 注册对输入法框架服务的开启及停止事件监听 95 inputMethodAbility.on('inputStart', (kbController, textInputClient) => { 96 this.textInputClient = textInputClient; // 此为输入法客户端实例,由此调用输入法框架提供给输入法应用的功能接口 97 this.keyboardController = kbController; 98 }) 99 inputMethodAbility.on('inputStop', () => { 100 this.onDestroy(); // 销毁KeyboardController 101 }); 102 } 103 104 private unRegisterListener(): void 105 { 106 inputMethodAbility.off('inputStart'); 107 inputMethodAbility.off('inputStop', () => {}); 108 } 109} 110 111const keyboardController1 = new KeyboardController1(); 112 113export default keyboardController1;