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