hunting/utils/KeyControl/include/KeyControl.h
2024-06-15 08:36:44 +08:00

68 lines
2.0 KiB
C++

/*
* Copyright (c) 2023 Fancy Code.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef KEY_CONTROL_H
#define KEY_CONTROL_H
#include <functional>
#include <memory>
#include <mutex>
constexpr int KEY_ACTION_SHORT_CLICK = 100;
constexpr int KEY_ACTION_HOLD_DWON = 500;
constexpr long int KEY_NOT_PRESSING = -1;
enum class KeyHalEvent
{
PRESSING = 0,
NOT_PRESSING,
END
};
enum class KeyEvent
{
SHORT_CLICK = 0,
HOLD_DOWN,
HOLD_UP,
END
};
class VKeyControl
{
public:
VKeyControl() = default;
virtual ~VKeyControl() = default;
virtual const std::string GetKeyName(void);
virtual unsigned int GetStatusCheckPeriodMs(void);
virtual void KeyEventTrigger(const std::string &keyName, const KeyEvent &event, const unsigned int &timeMs);
};
class KeyControl : public VKeyControl, public std::enable_shared_from_this<KeyControl>
{
public:
KeyControl();
virtual ~KeyControl();
virtual void Init(void);
virtual void UnInit(void);
void KeyHalEventHandle(const std::string &key, const KeyHalEvent &keyEvent);
void TimerKeyEventTrigger(const KeyHalEvent &event);
void KeyHalEventTrigger(const KeyHalEvent &event);
long int GetHoldPressingTimeMs(void);
private:
void KeyPressingTrigger(const std::string &key);
void KeyNotPressingTrigger(const std::string &key);
bool IsKeyPressing(void);
private:
std::mutex mMutex;
long int mPressingTime;
long int mLongClickTime;
};
const char *PrintKeyEvent(const KeyEvent &event);
#endif