• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1import prompt from '@ohos.prompt';
2/*
3 * Copyright (c) 2022 Huawei Device Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
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.debug(TAG + '====>receive event err: ' + JSON.stringify(err));
46            console.debug(TAG + '====>receive event data ' + JSON.stringify(data));
47            switch (data.code) {
48                case 10:
49                    console.debug(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.debug(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.debug(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.debug(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.debug(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.debug(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.debug(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: ["test"]
81        };
82
83        var subscriber;
84        commoneventmanager.createSubscriber(commonEventSubscribeInfo).then(function (data) {
85            subscriber = data;
86            commoneventmanager.subscribe(subscriber, subscriberCallback);
87            console.debug(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, this.publishCallback);
137        }, 500);
138    }
139
140    private SUB_Misc_InputMethod_Manage_Physical_Buttons_0020(): void{
141        let commonEventPublishData = {
142            data: "FAILED"
143        };
144        console.info(TAG + '====>receive SUB_Misc_InputMethod_Manage_Physical_Buttons_0020 success');
145        inputKeyboardDelegate.on('keyEvent', (KeyEvents) => {
146            inputKeyboardDelegate.off("keyEvent");
147            console.info(TAG + "====>inputKeyboardDelegate.on('keyEvent')");
148            try{
149                console.info(TAG + "====>SUB_Misc_InputMethod_Manage_Physical_Buttons_0020 keyEvent: "  + KeyEvents.action);
150                console.info(TAG + "====>SUB_Misc_InputMethod_Manage_Physical_Buttons_0020 keyEvent: "  + KeyEvents.key.code);
151                if (KeyEvents.key.code === 2017 && KeyEvents.action === 1){
152                    commonEventPublishData = {
153                        data: "SUCCESS"
154                    };
155                }else{
156                    commonEventPublishData = {
157                        data: "FAILED"
158                    };
159                }
160            }catch(err){
161                console.error("====>SUB_Misc_InputMethod_Manage_Physical_Buttons_0020 keyDown throw_error: " + err);
162            }
163            return true;
164        });
165        let t = setTimeout(() => {
166            clearTimeout(t);
167            commoneventmanager.publish("SUB_Misc_InputMethod_Manage_Physical_Buttons_0020", commonEventPublishData, this.publishCallback);
168        }, 500);
169    }
170
171    private SUB_Misc_InputMethod_Manage_Physical_Buttons_0030(): void{
172        let commonEventPublishData = {
173            data: "FAILED"
174        };
175        let arr = new Array();
176        console.info(TAG + '====>receive SUB_Misc_InputMethod_Manage_Physical_Buttons_0030 success');
177        inputKeyboardDelegate.on('keyEvent', (KeyEvents) => {
178            console.info(TAG + "====>inputKeyboardDelegate.on('keyEvent')");
179            try{
180                console.info(TAG + "====>SUB_Misc_InputMethod_Manage_Physical_Buttons_0030 keyEvent: " + KeyEvents.action);
181                console.info(TAG + "====>SUB_Misc_InputMethod_Manage_Physical_Buttons_0030 keyEvent: " + KeyEvents.key.code);
182                arr.push(KeyEvents.action);
183                arr.push(KeyEvents.key.code);
184            }catch(err){
185                console.error("====>SUB_Misc_InputMethod_Manage_Physical_Buttons_0030 keyDown throw_error: " + err);
186            }
187            return true;
188        });
189        let t = setTimeout(() => {
190            clearTimeout(t);
191            inputKeyboardDelegate.off("keyEvent");
192            console.info(TAG + "====>SUB_Misc_InputMethod_Manage_Physical_Buttons_0030 arr: " + JSON.stringify(arr));
193            console.info(TAG + "====>SUB_Misc_InputMethod_Manage_Physical_Buttons_0030 arr: " + arr.join());
194            if (arr.join() === '1,2045,1,2049,2,2049,2,2045'){
195                commonEventPublishData = {
196                    data: "SUCCESS"
197                };
198                commoneventmanager.publish("SUB_Misc_InputMethod_Manage_Physical_Buttons_0030", commonEventPublishData, this.publishCallback);
199            }else{
200                commoneventmanager.publish("SUB_Misc_InputMethod_Manage_Physical_Buttons_0030", commonEventPublishData, this.publishCallback);
201            }
202        }, 500);
203    }
204
205    private SUB_Misc_InputMethod_Manage_Physical_Buttons_0040(): void{
206        let commonEventPublishData = {
207            data: "FAILED"
208        };
209        let arr = new Array();
210        console.info(TAG + '====>receive SUB_Misc_InputMethod_Manage_Physical_Buttons_0040 success');
211        inputKeyboardDelegate.on('keyEvent', (KeyEvents) => {
212            console.info(TAG + "====>inputKeyboardDelegate.on('keyEvent')");
213            try{
214                console.info(TAG + "====>SUB_Misc_InputMethod_Manage_Physical_Buttons_0040 keyEvent: " + KeyEvents.action);
215                console.info(TAG + "====>SUB_Misc_InputMethod_Manage_Physical_Buttons_0040 keyEvent: " + KeyEvents.key.code);
216                arr.push(KeyEvents.action);
217                arr.push(KeyEvents.key.code);
218            }catch(err){
219                console.error("====>SUB_Misc_InputMethod_Manage_Physical_Buttons_0040 keyDown throw_error: " + err);
220            }
221            return true;
222        });
223        let t = setTimeout(() => {
224            clearTimeout(t);
225            inputKeyboardDelegate.off("keyEvent");
226            console.info(TAG + "====>SUB_Misc_InputMethod_Manage_Physical_Buttons_0040 arr: " + JSON.stringify(arr));
227            console.info(TAG + "====>SUB_Misc_InputMethod_Manage_Physical_Buttons_0040 arr: " + arr.join());
228            if (arr.join() === '1,2017,1,2047,2,2017,2,2047'){
229                commonEventPublishData = {
230                    data: "SUCCESS"
231                };
232                commoneventmanager.publish("SUB_Misc_InputMethod_Manage_Physical_Buttons_0040", commonEventPublishData, this.publishCallback);
233            }else {
234                commoneventmanager.publish("SUB_Misc_InputMethod_Manage_Physical_Buttons_0040", commonEventPublishData, this.publishCallback);
235            }
236        }, 500);
237    }
238
239    private SUB_Misc_InputMethod_Manage_Physical_Buttons_0050(): void{
240        let commonEventPublishData = {
241            data: "FAILED"
242        };
243        let arr = new Array();
244        console.info(TAG + '====>receive SUB_Misc_InputMethod_Manage_Physical_Buttons_0050 success');
245        inputKeyboardDelegate.on('keyEvent', (KeyEvents) => {
246            console.info(TAG + "====>inputKeyboardDelegate.on('keyEvent')");
247            try{
248                console.info(TAG + "====>SUB_Misc_InputMethod_Manage_Physical_Buttons_0050 keyEvent: " + KeyEvents.action);
249                console.info(TAG + "====>SUB_Misc_InputMethod_Manage_Physical_Buttons_0050 keyEvent: " + KeyEvents.key.code);
250                arr.push(KeyEvents.action);
251                arr.push(KeyEvents.key.code);
252            }catch(err){
253                console.error("====>SUB_Misc_InputMethod_Manage_Physical_Buttons_0050 keyDown throw_error: " + err);
254            }
255            return true;
256        });
257        let t = setTimeout(() => {
258            clearTimeout(t);
259            inputKeyboardDelegate.off("keyEvent");
260            console.info(TAG + "====>SUB_Misc_InputMethod_Manage_Physical_Buttons_0050 arr: " + JSON.stringify(arr));
261            console.info(TAG + "====>SUB_Misc_InputMethod_Manage_Physical_Buttons_0050 arr: " + arr.join());
262            if (arr.join() === '1,2047,1,2017,2,2017,2,2047'){
263                commonEventPublishData = {
264                    data: "SUCCESS"
265                };
266                commoneventmanager.publish("SUB_Misc_InputMethod_Manage_Physical_Buttons_0050", commonEventPublishData, this.publishCallback);
267            }else {
268                commoneventmanager.publish("SUB_Misc_InputMethod_Manage_Physical_Buttons_0050", commonEventPublishData, this.publishCallback);
269            }
270        }, 500);
271    }
272
273    private SUB_Misc_InputMethod_Manage_Physical_Buttons_0060(): void{
274        let commonEventPublishData = {
275            data: "FAILED"
276        };
277        let arr = new Array();
278        console.info(TAG + '====>receive SUB_Misc_InputMethod_Manage_Physical_Buttons_0060 success');
279        inputKeyboardDelegate.on('keyEvent', (KeyEvents) => {
280            console.info(TAG + "====>inputKeyboardDelegate.on('keyEvent')");
281            try{
282                console.info(TAG + "====>SUB_Misc_InputMethod_Manage_Physical_Buttons_0060 keyEvent: " + KeyEvents.action);
283                console.info(TAG + "====>SUB_Misc_InputMethod_Manage_Physical_Buttons_0060 keyEvent: " + KeyEvents.key.code);
284                arr.push(KeyEvents.action);
285                arr.push(KeyEvents.key.code);
286            }catch(err){
287                console.error("====>SUB_Misc_InputMethod_Manage_Physical_Buttons_0060 keyDown throw_error: " + err);
288            }
289            return true;
290        });
291        let t = setTimeout(() => {
292            clearTimeout(t);
293            inputKeyboardDelegate.off("keyEvent");
294            console.info(TAG + "====>SUB_Misc_InputMethod_Manage_Physical_Buttons_0060 arr: " + JSON.stringify(arr));
295            console.info(TAG + "====>SUB_Misc_InputMethod_Manage_Physical_Buttons_0060 arr: " + arr.join());
296            if (arr.join() === '1,2072,1,2045,1,2055,2,2055,2,2045,2,2072'){
297                commonEventPublishData = {
298                    data: "SUCCESS"
299                };
300                commoneventmanager.publish("SUB_Misc_InputMethod_Manage_Physical_Buttons_0060", commonEventPublishData, this.publishCallback);
301            }else {
302                commoneventmanager.publish("SUB_Misc_InputMethod_Manage_Physical_Buttons_0060", commonEventPublishData, this.publishCallback);
303            }
304        }, 500);
305    }
306
307    private SUB_Misc_InputMethod_Manage_Physical_Buttons_0070(): void{
308        let commonEventPublishData = {
309            data: "FAILED"
310        };
311        let arr = new Array();
312        console.info(TAG + '====>receive r success');
313        inputKeyboardDelegate.on('keyEvent', (KeyEvents) => {
314            console.info(TAG + "====>inputKeyboardDelegate.on('keyEvent')");
315            try{
316                console.info(TAG + "====>SUB_Misc_InputMethod_Manage_Physical_Buttons_0070 keyEvent: " + KeyEvents.action);
317                console.info(TAG + "====>SUB_Misc_InputMethod_Manage_Physical_Buttons_0070 keyEvent: " + KeyEvents.key.code);
318                arr.push(KeyEvents.action);
319                arr.push(KeyEvents.key.code);
320            }catch(err){
321                console.error("====>SUB_Misc_InputMethod_Manage_Physical_Buttons_0070 keyDown throw_error: " + err);
322            }
323            return true;
324        });
325        let t = setTimeout(() => {
326            clearTimeout(t);
327            inputKeyboardDelegate.off("keyEvent");
328            console.info(TAG + "====>SUB_Misc_InputMethod_Manage_Physical_Buttons_0070 arr: " + JSON.stringify(arr));
329            console.info(TAG + "====>SUB_Misc_InputMethod_Manage_Physical_Buttons_0070 arr: " + arr.join());
330            if (arr.join() === '1,2072,1,2045,1,2017,2,2017,2,2045,2,2072'){
331                commonEventPublishData = {
332                    data: "SUCCESS"
333                };
334                commoneventmanager.publish("SUB_Misc_InputMethod_Manage_Physical_Buttons_0070", commonEventPublishData, this.publishCallback);
335            }else {
336                commoneventmanager.publish("SUB_Misc_InputMethod_Manage_Physical_Buttons_0070", commonEventPublishData, this.publishCallback);
337            }
338        }, 500);
339    }
340
341
342
343}