• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 */
15import { formatJson } from '@ohos/common/src/main/ets/util/JSONUtils';
16/**
17 * 输入按键事件管理
18 */
19@Preview
20@Component
21export struct InputKeyEventManager {
22  @State mKeyEventData: string = '' // 按键数据
23
24  build() {
25    Scroll() {
26      Column({ space: 12 }) {
27        Column({ space: 8 }) {
28          Text($r('app.string.input_key_event_listener'))
29            .fontColor($r('sys.color.ohos_id_color_text_primary'))
30            .fontSize($r('sys.float.ohos_id_text_size_body1'))
31            .fontWeight(FontWeight.Medium)
32            .width('100%')
33            .focusable(true)
34
35          Text($r('app.string.input_key_event_hint'))
36            .fontColor($r('app.color.battery_info_value_text'))
37            .fontSize($r('sys.float.ohos_id_text_size_body2'))
38            .width('100%')
39        }
40        .padding(px2vp(24))
41        .backgroundColor($r('sys.color.ohos_id_color_list_card_bg'))
42        .border({
43          radius: $r('sys.float.ohos_id_corner_radius_default_l')
44        })
45        .id('columnKeyListener')
46
47        Text($r('app.string.input_key_event_data'))
48          .fontColor($r('sys.color.ohos_id_color_text_secondary'))
49          .fontSize($r('sys.float.ohos_id_text_size_body2'))
50          .fontWeight(FontWeight.Regular)
51          .fontFamily('HarmonyHeiTi')
52        Column() {
53          Text(this.mKeyEventData)
54            .fontColor($r('app.color.input_consumer_hint'))
55            .fontSize(14)
56            .fontWeight(FontWeight.Regular)
57            .width('100%')
58        }
59        .padding(px2vp(24))
60        .backgroundColor($r('sys.color.ohos_id_color_list_card_bg'))
61        .border({
62          radius: $r('sys.float.ohos_id_corner_radius_default_l')
63        })
64        .id('columnKeyEventData')
65      }
66      .justifyContent(FlexAlign.Center)
67      .alignItems(HorizontalAlign.Start)
68      .margin(px2vp(24))
69      .focusable(true)
70      .onKeyEvent((event) => {
71        console.log('InputKeyEventManager Column ' + JSON.stringify(event))
72        this.mKeyEventData = formatJson(JSON.stringify(event))
73      })
74    }
75    .width('100%')
76    .scrollBar(BarState.Off)
77    .id('scrollKeyEvent')
78  }
79}