1/* 2 * Copyright (c) 2023 Fujian Newland Auto-ID Tech.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 InputConsumerUtil from '../util/InputConsumerUtil' 17import keyCode from '@ohos.multimodalInput.keyCode' 18import { formatJson } from '@ohos/common/src/main/ets/util/JSONUtils'; 19import inputConsumer from '@ohos.multimodalInput.inputConsumer' 20 21/** 22 * 组合按键 23 */ 24@Preview 25@Component 26export struct InputConsumer { 27 @State mIsShowImage: boolean = false 28 @State mIsShowInputConsumerInfo: boolean = false 29 @State mInputConsumerInfo: string = '' 30 31 callback(KeyOptions: inputConsumer.KeyOptions): void { 32 this.mInputConsumerInfo = formatJson(JSON.stringify(KeyOptions)) 33 const finalKey: number = KeyOptions.finalKey 34 switch (finalKey) { 35 case keyCode.KeyCode.KEYCODE_S: { 36 this.mIsShowImage = true 37 break 38 } 39 case keyCode.KeyCode.KEYCODE_D: { 40 this.mIsShowImage = false 41 break 42 } 43 default: { 44 break 45 } 46 } 47 } 48 49 aboutToDisappear() { 50 this.clear() 51 } 52 53 clear() { 54 InputConsumerUtil.unsubscribe() 55 this.mInputConsumerInfo = '' 56 this.mIsShowImage = false 57 this.mIsShowInputConsumerInfo = false 58 } 59 60 build() { 61 Column({ space: px2vp(8) }) { 62 Column() { 63 Row() { 64 Text($r('app.string.input_consumer_listener')) 65 .fontColor($r('sys.color.ohos_id_color_text_primary')) 66 .fontSize($r('sys.float.ohos_id_text_size_sub_title2')) 67 .fontWeight(FontWeight.Regular) 68 Blank() 69 Toggle({ 70 type: ToggleType.Switch, 71 isOn: false 72 }) 73 .width(42) 74 .onChange((isOn) => { 75 if (isOn) { 76 this.mIsShowInputConsumerInfo = true 77 InputConsumerUtil.subscribe((KeyOptions: inputConsumer.KeyOptions): void => this.callback(KeyOptions)) 78 } else { 79 this.clear() 80 } 81 }) 82 .id('inputConsumerToggle') 83 } 84 .width('100%') 85 86 Text($r('app.string.input_consumer_hint')) 87 .fontColor($r('app.color.battery_info_value_text')) 88 .fontSize($r('sys.float.ohos_id_text_size_sub_title3')) 89 .width('100%') 90 91 if (this.mIsShowInputConsumerInfo) { 92 Text(this.mInputConsumerInfo.length > 0 ? this.mInputConsumerInfo : $r('app.string.input_wait')) 93 .fontColor($r('app.color.input_consumer_hint')) 94 .fontSize(14) 95 .width('100%') 96 .fontWeight(FontWeight.Regular) 97 .margin({ 98 top: px2vp(20) 99 }) 100 .id('textInputConsumerInfo') 101 } 102 } 103 104 if (this.mIsShowImage) { 105 Image($r('app.media.img_openatom')) 106 .height('10%') 107 .objectFit(ImageFit.Contain) 108 } 109 } 110 .margin(px2vp(24)) 111 .padding(px2vp(24)) 112 .backgroundColor(Color.White) 113 .borderRadius($r('sys.float.ohos_id_corner_radius_default_l')) 114 } 115}