• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 
16 #include "core/event/key_event_recognizer.h"
17 
18 #include <ctime>
19 
20 #include "base/log/log.h"
21 
22 namespace OHOS::Ace {
23 namespace {
24 
25 constexpr int32_t LONG_PRESS_DURATION = 1;
26 
27 }
28 
GetKeyEvents(int32_t keyCode,int32_t keyAction,int32_t repeatTime,int64_t timeStamp,int64_t timeStampStart,int32_t metaKey,int32_t keySource,int64_t deviceId)29 std::vector<KeyEvent> KeyEventRecognizer::GetKeyEvents(int32_t keyCode, int32_t keyAction, int32_t repeatTime,
30     int64_t timeStamp, int64_t timeStampStart, int32_t metaKey, int32_t keySource, int64_t deviceId)
31 {
32     if (timeStamp == 0) {
33         timeStamp = clock();
34     }
35     std::vector<KeyEvent> keyEvents;
36     keyEvents.emplace_back(KeyEvent(static_cast<KeyCode>(keyCode), static_cast<KeyAction>(keyAction), repeatTime,
37         timeStamp, deviceId, static_cast<SourceType>(keySource)));
38     auto result = keyMap_.try_emplace(keyCode, false);
39     auto iter = result.first;
40 
41     // Recognize long press event.
42     if ((keyAction == static_cast<int32_t>(KeyAction::DOWN)) && (repeatTime >= LONG_PRESS_DURATION) &&
43         (!iter->second)) {
44         LOGD("this event is long press, key code is %{public}d", keyCode);
45         iter->second = true;
46         keyEvents.emplace_back(KeyEvent(static_cast<KeyCode>(keyCode), KeyAction::LONG_PRESS, repeatTime, timeStamp,
47             deviceId, static_cast<SourceType>(keySource)));
48     }
49     // Recognize click event.
50     if (keyAction == static_cast<int32_t>(KeyAction::UP)) {
51         if (iter->second) {
52             iter->second = false;
53         } else {
54             LOGD("this event is click, key code is %{public}d", keyCode);
55             keyEvents.emplace_back(KeyEvent(static_cast<KeyCode>(keyCode), KeyAction::CLICK, repeatTime, timeStamp,
56                 deviceId, static_cast<SourceType>(keySource)));
57         }
58     }
59     return keyEvents;
60 }
61 
62 } // namespace OHOS::Ace
63