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
24 using namespace OHOS::MMI;
25
KeyInputImpl()26 KeyInputImpl::KeyInputImpl() : KeyInput(), pressedCodes(0)
27 {
28 }
29
GetInstance()30 KeyInputImpl& KeyInputImpl::GetInstance()
31 {
32 static KeyInputImpl instance;
33 return instance;
34 }
35
DispatchOsInputMethodEvent() const36 void KeyInputImpl::DispatchOsInputMethodEvent() const
37 {
38 JsAppImpl::GetInstance().DispatchInputMethodEvent(codePoint);
39 }
40
DispatchOsKeyEvent() const41 void 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
SetKeyEvent(const int32_t keyCodeVal,const int32_t keyActionVal,const std::vector<int32_t> pressedCodesVal,const std::string keyStrVal)54 void 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
SetCodePoint(const unsigned int codePointVal)66 void KeyInputImpl::SetCodePoint(const unsigned int codePointVal)
67 {
68 codePoint = codePointVal;
69 }
70