xref: /ide/tools/previewer/mock/rich/KeyInputImpl.cpp (revision 7c804472)
1/*
2 * Copyright (c) 2023 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 "KeyInputImpl.h"
17#include <chrono>
18#include <mutex>
19#include "PreviewerEngineLog.h"
20#include "ClipboardHelper.h"
21#include "KeyboardHelper.h"
22#include "JsAppImpl.h"
23
24using namespace OHOS::MMI;
25
26KeyInputImpl::KeyInputImpl() : KeyInput(), pressedCodes(0)
27{
28}
29
30KeyInputImpl& KeyInputImpl::GetInstance()
31{
32    static KeyInputImpl instance;
33    return instance;
34}
35
36void KeyInputImpl::DispatchOsInputMethodEvent() const
37{
38    JsAppImpl::GetInstance().DispatchInputMethodEvent(codePoint);
39}
40
41void KeyInputImpl::DispatchOsKeyEvent() const
42{
43    auto keyEvent = std::make_shared<OHOS::MMI::KeyEvent>();
44    keyEvent->code = KeyCode(keyCode);
45    keyEvent->action = KeyAction(keyAction);
46    keyEvent->pressedCodes = pressedCodes;
47    keyEvent->timeStamp = std::chrono::high_resolution_clock::now();
48    keyEvent->key = keyString.c_str();
49    keyEvent->enableCapsLock_ = KeyboardHelper::GetKeyStateByKeyName("CapsLock");
50    keyEvent->enableNumLock_ = KeyboardHelper::GetKeyStateByKeyName("NumLock");
51    JsAppImpl::GetInstance().DispatchKeyEvent(keyEvent);
52}
53
54void KeyInputImpl::SetKeyEvent(const int32_t keyCodeVal, const int32_t keyActionVal,
55    const std::vector<int32_t> pressedCodesVal, const std::string keyStrVal)
56{
57    keyCode = keyCodeVal;
58    keyAction = keyActionVal;
59    pressedCodes.clear();
60    for (int32_t num : pressedCodesVal) {
61        pressedCodes.push_back(KeyCode(num));
62    }
63    keyString = keyStrVal;
64}
65
66void KeyInputImpl::SetCodePoint(const unsigned int codePointVal)
67{
68    codePoint = codePointVal;
69}
70