mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-04-07 11:05:33 +08:00
refs #313 Input manager connecting a keyboard event and a hotkey function together
This commit is contained in:
84
src/blackcore/input_manager.cpp
Normal file
84
src/blackcore/input_manager.cpp
Normal file
@@ -0,0 +1,84 @@
|
||||
/* Copyright (C) 2013 VATSIM Community / contributors
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
#include "input_manager.h"
|
||||
|
||||
using namespace BlackInput;
|
||||
using namespace BlackMisc;
|
||||
using namespace BlackMisc::Hardware;
|
||||
|
||||
namespace BlackCore
|
||||
{
|
||||
CInputManager *CInputManager::m_instance = nullptr;
|
||||
|
||||
CInputManager::CInputManager(QObject *parent) :
|
||||
QObject(parent),
|
||||
m_keyboard(IKeyboard::getInstance())
|
||||
{
|
||||
connect(m_keyboard, &IKeyboard::keyUp, this, &CInputManager::ps_processKeyboardKeyUp);
|
||||
connect(m_keyboard, &IKeyboard::keyDown, this, &CInputManager::ps_processKeyboardKeyDown);
|
||||
}
|
||||
|
||||
CInputManager *CInputManager::getInstance()
|
||||
{
|
||||
if (!m_instance)
|
||||
{
|
||||
m_instance = new CInputManager();
|
||||
}
|
||||
return m_instance;
|
||||
}
|
||||
|
||||
void CInputManager::changeHotkeySettings(Settings::CSettingKeyboardHotkeyList hotkeys)
|
||||
{
|
||||
CKeyboardKeyList keyList;
|
||||
for (Settings::CSettingKeyboardHotkey settingHotkey : hotkeys)
|
||||
{
|
||||
CKeyboardKey key = settingHotkey.getKey();
|
||||
if (key.isEmpty()) continue;
|
||||
|
||||
m_hashKeyboardKeyFunctions.insert(key, settingHotkey.getFunction());
|
||||
keyList.push_back(key);
|
||||
}
|
||||
m_keyboard->setKeysToMonitor(keyList);
|
||||
}
|
||||
|
||||
void CInputManager::ps_processKeyboardKeyDown(const CKeyboardKey &key)
|
||||
{
|
||||
if (!m_hashKeyboardKeyFunctions.contains(key)) return;
|
||||
|
||||
// Get configured hotkey function
|
||||
CHotkeyFunction hotkeyFunc = m_hashKeyboardKeyFunctions.value(key);
|
||||
callFunctionsBy(hotkeyFunc, true);
|
||||
}
|
||||
|
||||
void CInputManager::ps_processKeyboardKeyUp(const CKeyboardKey &key)
|
||||
{
|
||||
if (!m_hashKeyboardKeyFunctions.contains(key)) return;
|
||||
|
||||
// Get configured hotkey function
|
||||
CHotkeyFunction hotkeyFunc = m_hashKeyboardKeyFunctions.value(key);
|
||||
callFunctionsBy(hotkeyFunc, false);
|
||||
}
|
||||
|
||||
void CInputManager::callFunctionsBy(const CHotkeyFunction &hotkeyFunction, bool isKeyDown)
|
||||
{
|
||||
if (!m_hashRegisteredFunctions.contains(hotkeyFunction)) return;
|
||||
auto func = m_hashRegisteredFunctions.value(hotkeyFunction);
|
||||
func(isKeyDown);
|
||||
}
|
||||
|
||||
CInputManager::RegistrationHandle CInputManager::registerHotkeyFuncImpl(const BlackMisc::CHotkeyFunction &hotkeyFunction,
|
||||
QObject *receiver,
|
||||
std::function<void (bool)> function)
|
||||
{
|
||||
m_hashRegisteredFunctions.insert(hotkeyFunction, function);
|
||||
|
||||
RegistrationHandle handle;
|
||||
handle.function = function;
|
||||
handle.hotkeyFunction = hotkeyFunction;
|
||||
handle.m_receiver = receiver;
|
||||
return handle;
|
||||
}
|
||||
}
|
||||
99
src/blackcore/input_manager.h
Normal file
99
src/blackcore/input_manager.h
Normal file
@@ -0,0 +1,99 @@
|
||||
/* Copyright (C) 2013 VATSIM Community / contributors
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
#ifndef BLACKCORE_INPUTMANAGER_H
|
||||
#define BLACKCORE_INPUTMANAGER_H
|
||||
|
||||
#include "blackinput/keyboard.h"
|
||||
#include "blackmisc/hwkeyboardkeylist.h"
|
||||
#include "blackmisc/hotkeyfunction.h"
|
||||
#include "blackmisc/setkeyboardhotkeylist.h"
|
||||
#include <QObject>
|
||||
#include <QHash>
|
||||
#include <type_traits>
|
||||
#include <functional>
|
||||
|
||||
//! \file
|
||||
|
||||
namespace BlackCore
|
||||
{
|
||||
class CInputManager : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
|
||||
//! \brief Handle to a registered hotkey function
|
||||
struct RegistrationHandle
|
||||
{
|
||||
//! \brief Constructor
|
||||
RegistrationHandle() {}
|
||||
|
||||
BlackMisc::CHotkeyFunction hotkeyFunction; //!< Registered hotkey function
|
||||
QPointer<QObject> m_receiver; //!< Registered receiver
|
||||
std::function<void(bool)> function; //!< Registered function
|
||||
};
|
||||
|
||||
//! Register a new hotkey function
|
||||
RegistrationHandle registerHotkeyFunc(const BlackMisc::CHotkeyFunction &hotkeyFunction, QObject *receiver, const QByteArray &slotName)
|
||||
{
|
||||
auto function = [=](bool isKeyDown){ QMetaObject::invokeMethod(receiver, slotName, Q_ARG(bool, isKeyDown)); };
|
||||
return registerHotkeyFuncImpl(hotkeyFunction, receiver, function);
|
||||
}
|
||||
|
||||
//! Register a new hotkey function
|
||||
template <class RecvObj>
|
||||
RegistrationHandle registerHotkeyFunc(const BlackMisc::CHotkeyFunction &hotkeyFunction, RecvObj *receiver, void (RecvObj:: *slotPointer)(bool))
|
||||
{
|
||||
using namespace std::placeholders;
|
||||
auto function = std::bind(slotPointer, receiver, _1);
|
||||
return registerHotkeyFuncImpl(hotkeyFunction, receiver, function);
|
||||
}
|
||||
|
||||
//! Register a new hotkey function
|
||||
template <class Func>
|
||||
RegistrationHandle registerHotkeyFunc(const BlackMisc::CHotkeyFunction &hotkeyFunction, QObject *receiver, Func functionObject)
|
||||
{
|
||||
return registerHotkeyFuncImpl(hotkeyFunction, receiver, functionObject);
|
||||
}
|
||||
|
||||
//! Deletes all registered hotkeys. Be careful with this method!
|
||||
void resetAllHotkeyFuncs() { m_hashRegisteredFunctions.clear(); }
|
||||
|
||||
//! Creates a native keyboard handler object
|
||||
static CInputManager *getInstance();
|
||||
|
||||
public slots:
|
||||
|
||||
//! Change hotkey settings
|
||||
void changeHotkeySettings(BlackMisc::Settings::CSettingKeyboardHotkeyList hotkeys);
|
||||
|
||||
//! Call functions by hotkeyfunction
|
||||
void callFunctionsBy(const BlackMisc::CHotkeyFunction &hotkeyFunction, bool isKeyDown);
|
||||
|
||||
protected:
|
||||
//! Constructor
|
||||
CInputManager(QObject *parent = nullptr);
|
||||
|
||||
private slots:
|
||||
|
||||
void ps_processKeyboardKeyDown(const BlackMisc::Hardware::CKeyboardKey &);
|
||||
|
||||
void ps_processKeyboardKeyUp(const BlackMisc::Hardware::CKeyboardKey &);
|
||||
|
||||
private:
|
||||
|
||||
RegistrationHandle registerHotkeyFuncImpl(const BlackMisc::CHotkeyFunction &hotkeyFunction, QObject *receiver, std::function<void(bool)> function);
|
||||
|
||||
static CInputManager *m_instance;
|
||||
|
||||
BlackInput::IKeyboard *m_keyboard = nullptr;
|
||||
|
||||
QHash<BlackMisc::CHotkeyFunction, std::function<void(bool)> > m_hashRegisteredFunctions;
|
||||
QHash<BlackMisc::Hardware::CKeyboardKey, BlackMisc::CHotkeyFunction> m_hashKeyboardKeyFunctions;
|
||||
};
|
||||
}
|
||||
|
||||
#endif //BLACKCORE_INPUTMANAGER_H
|
||||
Reference in New Issue
Block a user