1 /*
2  * Copyright (c) 2020-2021 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 
16 #include "key_input.h"
17 
18 #include <QtCore/qcoreevent.h>
19 #include <QtCore/qvariant.h>
20 
21 #include "events/key_event.h"
22 
23 namespace OHOS {
24 #if defined(USE_KEY) && USE_KEY
25 namespace {
26 static uint16_t g_lastKeyId = 0;
27 static uint16_t g_lastKeyState = INVALID_KEY_STATE;
28 } // namespace
29 
GetInstance()30 KeyInput* KeyInput::GetInstance()
31 {
32     static KeyInput keyInput;
33     return &keyInput;
34 }
35 
Read(DeviceData & data)36 bool KeyInput::Read(DeviceData& data)
37 {
38     data.keyId = g_lastKeyId;
39     data.state = g_lastKeyState;
40     g_lastKeyState = INVALID_KEY_STATE;
41     return false;
42 }
43 
KeyHandler(QKeyEvent * event)44 void KeyInput::KeyHandler(QKeyEvent* event)
45 {
46     if (event == nullptr) {
47         return;
48     }
49     switch (event->type()) {
50         case QKeyEvent::Type::KeyPress:
51             g_lastKeyState = InputDevice::STATE_PRESS;
52             break;
53         case QKeyEvent::Type::KeyRelease:
54             g_lastKeyState = InputDevice::STATE_RELEASE;
55             break;
56         default:
57             break;
58     }
59 }
60 #endif // USE_KEY
61 } // namespace OHOS
62