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 "ace_ability.h"
23 #include "event_dispatcher.h"
24 #include "clipboard_proxy.h"
25 #include "clipboard_proxy_impl.h"
26
27 using namespace std;
28 using namespace OHOS::Ace;
29
KeyInputImpl()30 KeyInputImpl::KeyInputImpl() : KeyInput(), pressedCodes(0)
31 {
32 }
33
GetInstance()34 KeyInputImpl& KeyInputImpl::GetInstance()
35 {
36 static KeyInputImpl instance;
37 return instance;
38 }
39
SetDelegate()40 void KeyInputImpl::SetDelegate()
41 {
42 auto callbackSetClipboardData = [](const std::string& data) {
43 ClipboardHelper::SetClipboardData(data);
44 };
45 auto callbackGetClipboardData = []() {
46 return ClipboardHelper::GetClipboardData();
47 };
48 auto callbackGetCapsLockStatus = []() {
49 return KeyboardHelper::GetKeyStateByKeyName("CapsLock");
50 };
51 auto callbackGetNumLockStatus = []() {
52 return KeyboardHelper::GetKeyStateByKeyName("NumLock");
53 };
54 Platform::EventDispatcher::GetInstance().RegisterCallbackGetCapsLockStatus(callbackGetCapsLockStatus);
55 Platform::EventDispatcher::GetInstance().RegisterCallbackGetNumLockStatus(callbackGetNumLockStatus);
56 ClipboardProxy::GetInstance()->SetDelegate(
57 std::make_unique<Platform::ClipboardProxyImpl>(callbackSetClipboardData, callbackGetClipboardData));
58 ILOG("Bind key event callback function to ace successed.");
59 }
60
DispatchOsInputMethodEvent() const61 void KeyInputImpl::DispatchOsInputMethodEvent() const
62 {
63 Platform::EventDispatcher::GetInstance().DispatchInputMethodEvent(codePoint);
64 }
65
DispatchOsKeyEvent() const66 void KeyInputImpl::DispatchOsKeyEvent() const
67 {
68 KeyEvent keyEvent;
69 keyEvent.code = KeyCode(keyCode);
70 keyEvent.action = KeyAction(keyAction);
71 keyEvent.pressedCodes = pressedCodes;
72 keyEvent.timeStamp = chrono::high_resolution_clock::now();
73 keyEvent.key = keyString.c_str();
74 Platform::EventDispatcher::GetInstance().DispatchKeyEvent(keyEvent);
75 }
76
SetKeyEvent(const int32_t keyCodeVal,const int32_t keyActionVal,const vector<int32_t> pressedCodesVal,const string keyStrVal)77 void KeyInputImpl::SetKeyEvent(const int32_t keyCodeVal, const int32_t keyActionVal,
78 const vector<int32_t> pressedCodesVal, const string keyStrVal)
79 {
80 keyCode = keyCodeVal;
81 keyAction = keyActionVal;
82 pressedCodes.clear();
83 for (int32_t num : pressedCodesVal) {
84 pressedCodes.push_back(KeyCode(num));
85 }
86 keyString = keyStrVal;
87 }
88
SetCodePoint(const unsigned int codePointVal)89 void KeyInputImpl::SetCodePoint(const unsigned int codePointVal)
90 {
91 codePoint = codePointVal;
92 }