• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 std;
25 using namespace OHOS::MMI;
26 
KeyInputImpl()27 KeyInputImpl::KeyInputImpl() : KeyInput(), pressedCodes(0)
28 {
29 }
30 
GetInstance()31 KeyInputImpl& KeyInputImpl::GetInstance()
32 {
33     static KeyInputImpl instance;
34     return instance;
35 }
36 
DispatchOsInputMethodEvent() const37 void KeyInputImpl::DispatchOsInputMethodEvent() const
38 {
39     JsAppImpl::GetInstance().DispatchInputMethodEvent(codePoint);
40 }
41 
DispatchOsKeyEvent() const42 void KeyInputImpl::DispatchOsKeyEvent() const
43 {
44     auto keyEvent = std::make_shared<OHOS::MMI::KeyEvent>();
45     keyEvent->code = KeyCode(keyCode);
46     keyEvent->action = KeyAction(keyAction);
47     keyEvent->pressedCodes = pressedCodes;
48     keyEvent->timeStamp = chrono::high_resolution_clock::now();
49     keyEvent->key = keyString.c_str();
50     keyEvent->enableCapsLock_ = KeyboardHelper::GetKeyStateByKeyName("CapsLock");
51     keyEvent->enableNumLock_ = KeyboardHelper::GetKeyStateByKeyName("NumLock");
52     JsAppImpl::GetInstance().DispatchKeyEvent(keyEvent);
53 }
54 
SetKeyEvent(const int32_t keyCodeVal,const int32_t keyActionVal,const vector<int32_t> pressedCodesVal,const string keyStrVal)55 void KeyInputImpl::SetKeyEvent(const int32_t keyCodeVal, const int32_t keyActionVal,
56                                const vector<int32_t> pressedCodesVal, const string keyStrVal)
57 {
58     keyCode = keyCodeVal;
59     keyAction = keyActionVal;
60     pressedCodes.clear();
61     for (int32_t num : pressedCodesVal) {
62         pressedCodes.push_back(KeyCode(num));
63     }
64     keyString = keyStrVal;
65 }
66 
SetCodePoint(const unsigned int codePointVal)67 void KeyInputImpl::SetCodePoint(const unsigned int codePointVal)
68 {
69     codePoint = codePointVal;
70 }
71