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 */ 15import prompt from '@ohos.prompt'; 16import inputmethodengine from '@ohos.inputMethodEngine'; 17import display from '@ohos.display'; 18import windowManager from '@ohos.window'; 19import commoneventmanager from '@ohos.commonEventManager'; 20 21 22let inputKeyboardDelegate = inputmethodengine.getKeyboardDelegate(); 23let inputMethodAbility = inputmethodengine.getInputMethodAbility(); 24const TAG = "keyboardController"; 25 26export class KeyboardController { 27 mContext; 28 WINDOW_TYPE_INPUT_METHOD_FLOAT = 2105; 29 windowName = 'inputApp'; 30 31 constructor(context) { 32 this.mContext = context; 33 } 34 35 public onCreate(): void { 36 let that = this; 37 inputMethodAbility.on("inputStop", () => { 38 this.mContext.destroy((err, data) => { 39 console.info(TAG + '====>inputMethodEngine destorey err:' + JSON.stringify(err)); 40 console.info(TAG + '====>inputMethodEngine destorey data:' + JSON.stringify(data)); 41 }); 42 }); 43 44 function subscriberCallback(err, data) { 45 console.info(TAG + '====>receive event err: ' + JSON.stringify(err)); 46 console.info(TAG + '====>receive event data ' + JSON.stringify(data)); 47 switch (data.code) { 48 case 10: 49 console.info(TAG + '====>SUB_Misc_InputMethod_Manage_Physical_Buttons_0010 event:' + data.event); 50 that.SUB_Misc_InputMethod_Manage_Physical_Buttons_0010(); 51 break; 52 case 20: 53 console.info(TAG + '====>SUB_Misc_InputMethod_Manage_Physical_Buttons_0020 event:' + data.event); 54 that.SUB_Misc_InputMethod_Manage_Physical_Buttons_0020(); 55 break; 56 case 30: 57 console.info(TAG + '====>SUB_Misc_InputMethod_Manage_Physical_Buttons_0030 event:' + data.event); 58 that.SUB_Misc_InputMethod_Manage_Physical_Buttons_0030(); 59 break; 60 case 40: 61 console.info(TAG + '====>SUB_Misc_InputMethod_Manage_Physical_Buttons_0040 event:' + data.event); 62 that.SUB_Misc_InputMethod_Manage_Physical_Buttons_0040(); 63 break; 64 case 50: 65 console.info(TAG + '====>SUB_Misc_InputMethod_Manage_Physical_Buttons_0050 event:' + data.event); 66 that.SUB_Misc_InputMethod_Manage_Physical_Buttons_0050(); 67 break; 68 case 60: 69 console.info(TAG + '====>SUB_Misc_InputMethod_Manage_Physical_Buttons_0060 event:' + data.event); 70 that.SUB_Misc_InputMethod_Manage_Physical_Buttons_0060(); 71 break; 72 case 70: 73 console.info(TAG + '====>SUB_Misc_InputMethod_Manage_Physical_Buttons_0070 event:' + data.event); 74 that.SUB_Misc_InputMethod_Manage_Physical_Buttons_0070(); 75 break; 76 } 77 } 78 79 var commonEventSubscribeInfo = { 80 events: ["inputMethodEngineKeyTest"] 81 }; 82 83 var subscriber; 84 commoneventmanager.createSubscriber(commonEventSubscribeInfo).then(function (data) { 85 subscriber = data; 86 commoneventmanager.subscribe(subscriber, subscriberCallback); 87 console.info(TAG + '====>scene subscribe finish===='); 88 }); 89 } 90 91 private publishCallback(err): void { 92 if (err) { 93 console.error(TAG + '====>publish failed: ' + JSON.stringify(err)); 94 } else { 95 console.log(TAG + '====>publish'); 96 } 97 } 98 99 public onDestroy(): void { 100 console.log('imsakitjsapp onDestroy'); 101 globalThis.textInputClient.getTextIndexAtCursor().then((index) => { 102 console.log('imsakitjsapp getTextIndexAtCursor: index = ' + index); 103 prompt.showToast({ message: 'getTextIndexAtCursor success' + index, duration: 200, bottom: 500 }); 104 var win = windowManager.findWindow(this.windowName); 105 win.destroyWindow(); 106 this.mContext.terminateSelf(); 107 return true; 108 }).catch((err) => { 109 prompt.showToast({ message: 'getTextIndexAtCursor failed', duration: 200, bottom: 500 }); 110 }); 111 } 112 113 private SUB_Misc_InputMethod_Manage_Physical_Buttons_0010(): void { 114 let commonEventPublishData = { 115 data: "FAILED" 116 }; 117 console.info(TAG + '====>receive SUB_Misc_InputMethod_Manage_Physical_Buttons_0010 success'); 118 inputKeyboardDelegate.on('keyEvent', (KeyEvents) => { 119 inputKeyboardDelegate.off("keyEvent"); 120 console.info(TAG + "====>inputKeyboardDelegate.on('keyEvent')"); 121 try { 122 console.info(TAG + "====>SUB_Misc_InputMethod_Manage_Physical_Buttons_0010 keyEvent: " + KeyEvents.action); 123 console.info(TAG + "====>SUB_Misc_InputMethod_Manage_Physical_Buttons_0010 keyEvent: " + KeyEvents.key.code); 124 if (KeyEvents.key.code === 2045) { 125 commonEventPublishData = { 126 data: "SUCCESS" 127 }; 128 } 129 } catch (err) { 130 console.error("====>SUB_Misc_InputMethod_Manage_Physical_Buttons_0010 keyDown throw_error: " + err); 131 } 132 return true; 133 }); 134 let t = setTimeout(() => { 135 clearTimeout(t); 136 commoneventmanager.publish("SUB_Misc_InputMethod_Manage_Physical_Buttons_0010", commonEventPublishData, 137 this.publishCallback); 138 }, 800); 139 } 140 141 private SUB_Misc_InputMethod_Manage_Physical_Buttons_0020(): void { 142 let commonEventPublishData = { 143 data: "FAILED" 144 }; 145 console.info(TAG + '====>receive SUB_Misc_InputMethod_Manage_Physical_Buttons_0020 success'); 146 inputKeyboardDelegate.on('keyEvent', (KeyEvents) => { 147 inputKeyboardDelegate.off("keyEvent"); 148 console.info(TAG + "====>inputKeyboardDelegate.on('keyEvent')"); 149 try { 150 console.info(TAG + "====>SUB_Misc_InputMethod_Manage_Physical_Buttons_0020 keyEvent: " + KeyEvents.action); 151 console.info(TAG + "====>SUB_Misc_InputMethod_Manage_Physical_Buttons_0020 keyEvent: " + KeyEvents.key.code); 152 if (KeyEvents.key.code === 2017 && KeyEvents.action === 1) { 153 commonEventPublishData = { 154 data: "SUCCESS" 155 }; 156 } else { 157 commonEventPublishData = { 158 data: "FAILED" 159 }; 160 } 161 } catch (err) { 162 console.error("====>SUB_Misc_InputMethod_Manage_Physical_Buttons_0020 keyDown throw_error: " + err); 163 } 164 return true; 165 }); 166 let t = setTimeout(() => { 167 clearTimeout(t); 168 commoneventmanager.publish("SUB_Misc_InputMethod_Manage_Physical_Buttons_0020", commonEventPublishData, 169 this.publishCallback); 170 }, 800); 171 } 172 173 private SUB_Misc_InputMethod_Manage_Physical_Buttons_0030(): void { 174 let commonEventPublishData = { 175 data: "FAILED" 176 }; 177 let arr = new Array(); 178 console.info(TAG + '====>receive SUB_Misc_InputMethod_Manage_Physical_Buttons_0030 success'); 179 inputKeyboardDelegate.on('keyEvent', (KeyEvents) => { 180 console.info(TAG + "====>inputKeyboardDelegate.on('keyEvent')"); 181 try { 182 console.info(TAG + "====>SUB_Misc_InputMethod_Manage_Physical_Buttons_0030 keyEvent: " + KeyEvents.action); 183 console.info(TAG + "====>SUB_Misc_InputMethod_Manage_Physical_Buttons_0030 keyEvent: " + KeyEvents.key.code); 184 arr.push(KeyEvents.action); 185 arr.push(KeyEvents.key.code); 186 } catch (err) { 187 console.error("====>SUB_Misc_InputMethod_Manage_Physical_Buttons_0030 keyDown throw_error: " + err); 188 } 189 return true; 190 }); 191 let t = setTimeout(() => { 192 clearTimeout(t); 193 inputKeyboardDelegate.off("keyEvent"); 194 console.info(TAG + "====>SUB_Misc_InputMethod_Manage_Physical_Buttons_0030 arr: " + JSON.stringify(arr)); 195 console.info(TAG + "====>SUB_Misc_InputMethod_Manage_Physical_Buttons_0030 arr: " + arr.join()); 196 if (arr.join() === '1,2072,1,2017,2,2017,2,2072') { 197 commonEventPublishData = { 198 data: "SUCCESS" 199 }; 200 commoneventmanager.publish("SUB_Misc_InputMethod_Manage_Physical_Buttons_0030", commonEventPublishData, 201 this.publishCallback); 202 } else { 203 commoneventmanager.publish("SUB_Misc_InputMethod_Manage_Physical_Buttons_0030", commonEventPublishData, 204 this.publishCallback); 205 } 206 }, 800); 207 } 208 209 private SUB_Misc_InputMethod_Manage_Physical_Buttons_0040(): void { 210 let commonEventPublishData = { 211 data: "FAILED" 212 }; 213 let arr = new Array(); 214 console.info(TAG + '====>receive SUB_Misc_InputMethod_Manage_Physical_Buttons_0040 success'); 215 inputKeyboardDelegate.on('keyEvent', (KeyEvents) => { 216 console.info(TAG + "====>inputKeyboardDelegate.on('keyEvent')"); 217 try { 218 console.info(TAG + "====>SUB_Misc_InputMethod_Manage_Physical_Buttons_0040 keyEvent: " + KeyEvents.action); 219 console.info(TAG + "====>SUB_Misc_InputMethod_Manage_Physical_Buttons_0040 keyEvent: " + KeyEvents.key.code); 220 arr.push(KeyEvents.action); 221 arr.push(KeyEvents.key.code); 222 } catch (err) { 223 console.error("====>SUB_Misc_InputMethod_Manage_Physical_Buttons_0040 keyDown throw_error: " + err); 224 } 225 return true; 226 }); 227 let t = setTimeout(() => { 228 clearTimeout(t); 229 inputKeyboardDelegate.off("keyEvent"); 230 console.info(TAG + "====>SUB_Misc_InputMethod_Manage_Physical_Buttons_0040 arr: " + JSON.stringify(arr)); 231 console.info(TAG + "====>SUB_Misc_InputMethod_Manage_Physical_Buttons_0040 arr: " + arr.join()); 232 if (arr.join() === '1,2017,1,2047,2,2047,2,2017') { 233 commonEventPublishData = { 234 data: "SUCCESS" 235 }; 236 commoneventmanager.publish("SUB_Misc_InputMethod_Manage_Physical_Buttons_0040", commonEventPublishData, 237 this.publishCallback); 238 } else { 239 commoneventmanager.publish("SUB_Misc_InputMethod_Manage_Physical_Buttons_0040", commonEventPublishData, 240 this.publishCallback); 241 } 242 }, 800); 243 } 244 245 private SUB_Misc_InputMethod_Manage_Physical_Buttons_0050(): void { 246 let commonEventPublishData = { 247 data: "FAILED" 248 }; 249 let arr = new Array(); 250 console.info(TAG + '====>receive SUB_Misc_InputMethod_Manage_Physical_Buttons_0050 success'); 251 inputKeyboardDelegate.on('keyEvent', (KeyEvents) => { 252 console.info(TAG + "====>inputKeyboardDelegate.on('keyEvent')"); 253 try { 254 console.info(TAG + "====>SUB_Misc_InputMethod_Manage_Physical_Buttons_0050 keyEvent: " + KeyEvents.action); 255 console.info(TAG + "====>SUB_Misc_InputMethod_Manage_Physical_Buttons_0050 keyEvent: " + KeyEvents.key.code); 256 arr.push(KeyEvents.action); 257 arr.push(KeyEvents.key.code); 258 } catch (err) { 259 console.error("====>SUB_Misc_InputMethod_Manage_Physical_Buttons_0050 keyDown throw_error: " + err); 260 } 261 return true; 262 }); 263 let t = setTimeout(() => { 264 clearTimeout(t); 265 inputKeyboardDelegate.off("keyEvent"); 266 console.info(TAG + "====>SUB_Misc_InputMethod_Manage_Physical_Buttons_0050 arr: " + JSON.stringify(arr)); 267 console.info(TAG + "====>SUB_Misc_InputMethod_Manage_Physical_Buttons_0050 arr: " + arr.join()); 268 if (arr.join() === '1,2047,1,2017,2,2017,2,2047') { 269 commonEventPublishData = { 270 data: "SUCCESS" 271 }; 272 commoneventmanager.publish("SUB_Misc_InputMethod_Manage_Physical_Buttons_0050", commonEventPublishData, 273 this.publishCallback); 274 } else { 275 commoneventmanager.publish("SUB_Misc_InputMethod_Manage_Physical_Buttons_0050", commonEventPublishData, 276 this.publishCallback); 277 } 278 }, 800); 279 } 280 281 private SUB_Misc_InputMethod_Manage_Physical_Buttons_0060(): void { 282 let commonEventPublishData = { 283 data: "FAILED" 284 }; 285 let arr = new Array(); 286 console.info(TAG + '====>receive SUB_Misc_InputMethod_Manage_Physical_Buttons_0060 success'); 287 inputKeyboardDelegate.on('keyEvent', (KeyEvents) => { 288 console.info(TAG + "====>inputKeyboardDelegate.on('keyEvent')"); 289 try { 290 console.info(TAG + "====>SUB_Misc_InputMethod_Manage_Physical_Buttons_0060 keyEvent: " + KeyEvents.action); 291 console.info(TAG + "====>SUB_Misc_InputMethod_Manage_Physical_Buttons_0060 keyEvent: " + KeyEvents.key.code); 292 arr.push(KeyEvents.action); 293 arr.push(KeyEvents.key.code); 294 } catch (err) { 295 console.error("====>SUB_Misc_InputMethod_Manage_Physical_Buttons_0060 keyDown throw_error: " + err); 296 } 297 return true; 298 }); 299 let t = setTimeout(() => { 300 clearTimeout(t); 301 inputKeyboardDelegate.off("keyEvent"); 302 console.info(TAG + "====>SUB_Misc_InputMethod_Manage_Physical_Buttons_0060 arr: " + JSON.stringify(arr)); 303 console.info(TAG + "====>SUB_Misc_InputMethod_Manage_Physical_Buttons_0060 arr: " + arr.join()); 304 if (arr.join() === '1,2047,1,2001,2,2001,2,2047') { 305 commonEventPublishData = { 306 data: "SUCCESS" 307 }; 308 commoneventmanager.publish("SUB_Misc_InputMethod_Manage_Physical_Buttons_0060", commonEventPublishData, 309 this.publishCallback); 310 } else { 311 commoneventmanager.publish("SUB_Misc_InputMethod_Manage_Physical_Buttons_0060", commonEventPublishData, 312 this.publishCallback); 313 } 314 }, 800); 315 } 316 317 private SUB_Misc_InputMethod_Manage_Physical_Buttons_0070(): void { 318 let commonEventPublishData = { 319 data: "FAILED" 320 }; 321 let arr = new Array(); 322 console.info(TAG + '====>receive r success'); 323 inputKeyboardDelegate.on('keyEvent', (KeyEvents) => { 324 console.info(TAG + "====>inputKeyboardDelegate.on('keyEvent')"); 325 try { 326 console.info(TAG + "====>SUB_Misc_InputMethod_Manage_Physical_Buttons_0070 keyEvent: " + KeyEvents.action); 327 console.info(TAG + "====>SUB_Misc_InputMethod_Manage_Physical_Buttons_0070 keyEvent: " + KeyEvents.key.code); 328 arr.push(KeyEvents.action); 329 arr.push(KeyEvents.key.code); 330 } catch (err) { 331 console.error("====>SUB_Misc_InputMethod_Manage_Physical_Buttons_0070 keyDown throw_error: " + err); 332 } 333 return true; 334 }); 335 let t = setTimeout(() => { 336 clearTimeout(t); 337 inputKeyboardDelegate.off("keyEvent"); 338 console.info(TAG + "====>SUB_Misc_InputMethod_Manage_Physical_Buttons_0070 arr: " + JSON.stringify(arr)); 339 console.info(TAG + "====>SUB_Misc_InputMethod_Manage_Physical_Buttons_0070 arr: " + arr.join()); 340 if (arr.join() === '1,2047,1,2062,2,2062,2,2047') { 341 commonEventPublishData = { 342 data: "SUCCESS" 343 }; 344 commoneventmanager.publish("SUB_Misc_InputMethod_Manage_Physical_Buttons_0070", commonEventPublishData, 345 this.publishCallback); 346 } else { 347 commoneventmanager.publish("SUB_Misc_InputMethod_Manage_Physical_Buttons_0070", commonEventPublishData, 348 this.publishCallback); 349 } 350 }, 800); 351 } 352}