Move IKeyboard and subclasses to new library BlackInput

refs #256
This commit is contained in:
Roland Winklmeier
2014-06-04 14:23:14 +02:00
committed by Roland Winklmeier
parent 14e9e01b90
commit a131e8c91c
25 changed files with 105 additions and 65 deletions

View File

@@ -8,7 +8,7 @@ QT += network dbus xml multimedia
TARGET = blackcore
TEMPLATE = lib
CONFIG += staticlib
CONFIG += blackmisc blacksim
CONFIG += blackmisc blackinput blacksim
INCLUDEPATH += ..
DEPENDPATH += . ..
@@ -25,21 +25,6 @@ DEFINES += LOG_IN_FILE
HEADERS += *.h
SOURCES += *.cpp
win32 {
HEADERS += $$PWD/win/*.h
SOURCES += $$PWD/win/*.cpp
}
unix:!macx {
HEADERS += $$PWD/linux/*.h
SOURCES += $$PWD/linux/*.cpp
}
macx {
HEADERS += $$PWD/mac/*.h
SOURCES += $$PWD/mac/*.cpp
}
win32:!win32-g++*: PRE_TARGETDEPS += ../../lib/blackmisc.lib ../../lib/blacksound.lib ../../lib/blacksim.lib
else: PRE_TARGETDEPS += ../../lib/libblackmisc.a ../../lib/libblacksound.a ../../lib/libblacksim.a

View File

@@ -34,8 +34,7 @@ namespace BlackCore
m_voice->moveToThread(&m_threadVoice);
m_threadVoice.start();
// 2. Hotkeys
m_keyboard = IKeyboard::getInstance();
m_keyboard = BlackInput::IKeyboard::getInstance();
// 3. own aircraft, if possible
if (this->getIContextOwnAircraft()) m_voice->setMyAircraftCallsign(this->getIContextOwnAircraft()->getOwnAircraft().getCallsign());

View File

@@ -11,7 +11,7 @@
#include "context_runtime.h"
#include "dbus_server.h"
#include "voice_vatlib.h"
#include "blackcore/keyboard.h"
#include "blackinput/keyboard.h"
#include <QThread>
#include <QQueue>
@@ -127,8 +127,8 @@ namespace BlackCore
bool inTransitionState() const;
CVoiceVatlib *m_voice; //!< underlying voice lib
IKeyboard *m_keyboard;
IKeyboard::RegistrationHandle m_handlePtt;
BlackInput::IKeyboard *m_keyboard;
BlackInput::IKeyboard::RegistrationHandle m_handlePtt;
QThread m_threadVoice;
};
}

View File

@@ -8,7 +8,7 @@
#include "blackcore/context.h"
#include "blackcore/dbus_server.h"
#include "blackcore/keyboard.h"
#include "blackinput/keyboard.h"
#include "blackmisc/hwkeyboardkeylist.h"
#include "blackmisc/statusmessagelist.h"
#include "blackmisc/settingutilities.h"

View File

@@ -1,52 +0,0 @@
/* 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 "keyboard.h"
#if defined(Q_OS_WIN)
#include "blackcore/win/keyboard_windows.h"
#elif defined(Q_OS_LINUX)
#include "blackcore/linux/keyboard_linux.h"
#elif defined(Q_OS_OSX)
#include "blackcore/mac/keyboard_mac.h"
#else
#error "Platform is not supported!"
#endif
namespace BlackCore
{
IKeyboard *IKeyboard::m_instance = nullptr;
IKeyboard::IKeyboard(QObject *parent) :
QObject(parent)
{
}
IKeyboard *IKeyboard::getInstance()
{
if (!m_instance)
{
#if defined(Q_OS_WIN)
m_instance = new CKeyboardWindows;
#elif defined(Q_OS_LINUX)
m_instance = new CKeyboardLinux;
#elif defined(Q_OS_OSX)
m_instance = new CKeyboardMac;
#endif
Q_ASSERT_X(m_instance, "IKeyboard::getInstance", "Pointer to IKeyboard is NULL!");
m_instance->init();
}
return m_instance;
}
bool operator==(IKeyboard::RegistrationHandle const &lhs, IKeyboard::RegistrationHandle const &rhs)
{
if ((lhs.m_key == rhs.m_key) && (lhs.m_receiver == rhs.m_receiver))
return true;
else
return false;
}
} // BlackCore

View File

@@ -1,184 +0,0 @@
/* 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/. */
/*!
\file
*/
#ifndef BLACKCORE_KEYBOARD_H
#define BLACKCORE_KEYBOARD_H
#include "../blackmisc/hwkeyboardkey.h"
#include <QMultiMap>
#include <QObject>
#include <QPointer>
#include <functional>
namespace BlackCore
{
/*!
* \brief Abstract interface for native keyboard handling.
* \todo Add implementation for Linux and OSX.
*/
class IKeyboard : public QObject
{
Q_OBJECT
public:
//! \brief Handle to a registered hotkey function
struct RegistrationHandle
{
//! \brief Constructor
RegistrationHandle() {}
BlackMisc::Hardware::CKeyboardKey m_key; //!< Registered key set
QPointer<QObject> m_receiver; //!< Registered receiver
std::function<void(bool)> function; //!< Registered function
};
//! Operation mode
enum Mode
{
Mode_Nominal,
Mode_Capture
};
//! Constructor
IKeyboard(QObject *parent = nullptr);
//! Destructor
virtual ~IKeyboard() {}
//! Creates a native keyboard handler object
static IKeyboard *getInstance();
/*!
* \brief Register a invokable slot as hotkey target
* \param key
* \param receiver
* \param slotName
* \return RegistrationHandle
*/
IKeyboard::RegistrationHandle registerHotkey(BlackMisc::Hardware::CKeyboardKey key, QObject *receiver, const QByteArray &slotName)
{
auto function = [=](bool isPressed){ QMetaObject::invokeMethod(receiver, slotName, Q_ARG(bool, isPressed)); };
return registerHotkeyImpl(key, receiver, function);
}
/*!
* \brief Register a member function as hotkey target
* \param key
* \param receiver
* \param slotPointer
* \return RegistrationHandle
*/
template <class T>
IKeyboard::RegistrationHandle registerHotkey(BlackMisc::Hardware::CKeyboardKey key, T *receiver, void (T:: *slotPointer)(bool))
{
using namespace std::placeholders;
auto function = std::bind(slotPointer, receiver, _1);
return registerHotkeyImpl(key, receiver, function);
}
/*!
* \brief Register a function object as hotkey target
* \param key
* \param receiver
* \param functionObject
* \return RegistrationHandle
*/
template <class F>
IKeyboard::RegistrationHandle registerHotkey(BlackMisc::Hardware::CKeyboardKey key, QObject *receiver, F functionObject)
{
return registerHotkeyImpl(key, receiver, functionObject);
}
/*!
* \brief Unregister hotkey target
* \param handle
*/
void unregisterHotkey(const IKeyboard::RegistrationHandle &handle)
{
unregisterHotkeyImpl(handle);
}
//! \brief Unregister all hotkeys
void unregisterAllHotkeys()
{
unregisterAllHotkeysImpl();
}
/*!
* \brief Select a key combination as hotkey. This method returns immediatly.
* Listen for signals keySelectionChanged and keySelectionFinished
* to retrieve the user input.
* \param ignoreNextKey
* Set to true, if you want to ignore the first key,
* e.g. [ENTER] in case you are running from command line.
*/
virtual void startCapture(bool ignoreNextKey) = 0;
/*!
* \brief Returns the amount of registered hotkey functions
* \return Size
*/
virtual int sizeOfRegisteredFunctions() const = 0;
/*!
* \brief Triggers a key event manually and calls the registered functions.
* \param key
* \param isPressed
*/
virtual void triggerKey(const BlackMisc::Hardware::CKeyboardKey key, bool isPressed) = 0;
signals:
/*!
* \brief Key selection has changed, but is not finished yet.
* \param key
*/
void keySelectionChanged(BlackMisc::Hardware::CKeyboardKey key);
/*!
* \brief Key selection has finished.
* \param key
*/
void keySelectionFinished(BlackMisc::Hardware::CKeyboardKey key);
protected:
/*!
* \brief Initializes the platform keyboard device
*/
virtual bool init() = 0;
/*!
* \brief Register implementation
*/
virtual IKeyboard::RegistrationHandle registerHotkeyImpl(BlackMisc::Hardware::CKeyboardKey key, QObject *receiver, std::function<void(bool)> function) = 0;
/*!
* \brief Unregister implementation
*/
virtual void unregisterHotkeyImpl(const IKeyboard::RegistrationHandle &handle) = 0;
//! \brief Unregister implementation
virtual void unregisterAllHotkeysImpl() = 0;
private:
static IKeyboard *m_instance;
};
/*!
* \brief Equal operator ==
* \param lhs
* \param rhs
* \return
*/
bool operator==(IKeyboard::RegistrationHandle const &lhs, IKeyboard::RegistrationHandle const &rhs);
}
#endif // BLACKCORE_KEYBOARD_H

View File

@@ -1,110 +0,0 @@
/* 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 "keyboard_linux.h"
#include <QDebug>
using namespace BlackMisc::Hardware;
namespace BlackCore
{
CKeyboardLinux::CKeyboardLinux(QObject *parent) :
IKeyboard(parent),
m_mode(Mode_Nominal)
{
}
CKeyboardLinux::~CKeyboardLinux()
{
}
bool CKeyboardLinux::init()
{
return true;
}
void CKeyboardLinux::startCapture(bool ignoreNextKey)
{
m_mode = Mode_Capture;
m_ignoreNextKey = ignoreNextKey;
m_pressedKey.setKeyObject(CKeyboardKey());
}
int CKeyboardLinux::sizeOfRegisteredFunctions() const
{
int size = 0;
foreach (QList<IKeyboard::RegistrationHandle> functions, m_registeredFunctions)
{
size += functions.size();
}
return size;
}
void CKeyboardLinux::triggerKey(const CKeyboardKey key, bool isPressed)
{
callFunctionsBy(key, isPressed);
}
IKeyboard::RegistrationHandle CKeyboardLinux::registerHotkeyImpl(BlackMisc::Hardware::CKeyboardKey key, QObject *receiver, std::function<void(bool)> function)
{
IKeyboard::RegistrationHandle handle;
// Workaround: Remove key function. Otherwise operator== will not
// work when we create the key value object by pressed keys
key.setFunction(BlackMisc::Hardware::CKeyboardKey::HotkeyNone);
if (!key.hasModifier() && !key.hasKey())
{
return handle;
}
if (receiver == nullptr)
return handle;
handle.m_key = key;
handle.m_receiver = receiver;
handle.function = function;
QList<IKeyboard::RegistrationHandle> functions = m_registeredFunctions.value(key);
functions.append(handle);
m_registeredFunctions.insert(key, functions);
return handle;
}
void CKeyboardLinux::unregisterHotkeyImpl(const IKeyboard::RegistrationHandle &handle)
{
QList<IKeyboard::RegistrationHandle> functions = m_registeredFunctions.value(handle.m_key);
functions.removeAll(handle);
m_registeredFunctions.insert(handle.m_key, functions);
}
void CKeyboardLinux::unregisterAllHotkeysImpl()
{
m_registeredFunctions.clear();
}
void CKeyboardLinux::sendCaptureNotification(const CKeyboardKey &key, bool isFinished)
{
if (isFinished)
emit keySelectionFinished(key);
else
emit keySelectionChanged(key);
}
void CKeyboardLinux::callFunctionsBy(const CKeyboardKey &key, bool isPressed)
{
QList<IKeyboard::RegistrationHandle> functionHandles = m_registeredFunctions.value(key);
foreach (IKeyboard::RegistrationHandle functionHandle, functionHandles)
{
if (functionHandle.m_receiver.isNull())
{
continue;
}
functionHandle.function(isPressed);
}
}
}

View File

@@ -1,87 +0,0 @@
/* 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/. */
/*!
\file
*/
#ifndef BLACKCORE_KEYBOARD_LINUX_H
#define BLACKCORE_KEYBOARD_LINUX_H
#include "blackcore/keyboard.h"
#include "blackmisc/hwkeyboardkey.h"
#include <QHash>
namespace BlackCore
{
//! \brief Linux implemenation of IKeyboard using hook procedure
//! \todo Change QHash to a CCollection object
class CKeyboardLinux : public IKeyboard
{
Q_OBJECT
public:
//! \brief Destructor
virtual ~CKeyboardLinux();
//! \copydoc IKeyboard::selectKey()
virtual void startCapture(bool ignoreNextKey = false) override;
//! \copydoc IKeyboard::sizeOfRegisteredFunctions()
virtual int sizeOfRegisteredFunctions() const override;
//! \copydoc IKeyboard::triggerKey()
virtual void triggerKey(const BlackMisc::Hardware::CKeyboardKey key, bool isPressed) override;
protected:
friend class IKeyboard;
//! \brief Constructor
CKeyboardLinux(QObject *parent = nullptr);
//! \brief Copy Constructor
CKeyboardLinux(CKeyboardLinux const&);
//! \copydoc IKeyboard::init()
virtual bool init() override;
//! \brief Assignment operator
void operator=(CKeyboardLinux const&);
//! \copydoc IKeyboard::registerHotKeyImpl()
virtual IKeyboard::RegistrationHandle registerHotkeyImpl(BlackMisc::Hardware::CKeyboardKey key, QObject *receiver, std::function<void(bool)> function) override;
//! \copydoc IKeyboard::unregisterHotkeyImpl()
virtual void unregisterHotkeyImpl(const IKeyboard::RegistrationHandle &handle) override;
//! \copydoc IKeyboard::unregisterHotkeyImpl()
virtual void unregisterAllHotkeysImpl() override;
private:
/*!
* \brief Constructor
* \param keySet
* \param isFinished
*/
void sendCaptureNotification(const BlackMisc::Hardware::CKeyboardKey &key, bool isFinished);
/*!
* \brief Calls registered functions on keyboard event
* \param keySet
* \param isPressed
*/
void callFunctionsBy(const BlackMisc::Hardware::CKeyboardKey &keySet, bool isPressed);
QHash<BlackMisc::Hardware::CKeyboardKey, QList<IKeyboard::RegistrationHandle>> m_registeredFunctions; //!< Registered hotkey functions
BlackMisc::Hardware::CKeyboardKey m_pressedKey; //!< Set of virtual keys pressed in the last cycle
bool m_ignoreNextKey; //!< Is true if the next key needs to be ignored
Mode m_mode; //!< Operation mode
};
}
#endif // BLACKCORE_KEYBOARD_LINUX_H

View File

@@ -1,110 +0,0 @@
/* 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 "keyboard_mac.h"
#include <QDebug>
using namespace BlackMisc::Hardware;
namespace BlackCore
{
CKeyboardMac::CKeyboardMac(QObject *parent) :
IKeyboard(parent),
m_mode(Mode_Nominal)
{
}
CKeyboardMac::~CKeyboardMac()
{
}
bool CKeyboardMac::init()
{
return true;
}
void CKeyboardMac::startCapture(bool ignoreNextKey)
{
m_mode = Mode_Capture;
m_ignoreNextKey = ignoreNextKey;
m_pressedKey.setKeyObject(CKeyboardKey());
}
int CKeyboardMac::sizeOfRegisteredFunctions() const
{
int size = 0;
foreach (QList<IKeyboard::RegistrationHandle> functions, m_registeredFunctions)
{
size += functions.size();
}
return size;
}
void CKeyboardMac::triggerKey(const CKeyboardKey key, bool isPressed)
{
callFunctionsBy(key, isPressed);
}
IKeyboard::RegistrationHandle CKeyboardMac::registerHotkeyImpl(BlackMisc::Hardware::CKeyboardKey key, QObject *receiver, std::function<void(bool)> function)
{
IKeyboard::RegistrationHandle handle;
// Workaround: Remove key function. Otherwise operator== will not
// work when we create the key value object by pressed keys
key.setFunction(BlackMisc::Hardware::CKeyboardKey::HotkeyNone);
if (!key.hasModifier() && !key.hasKey())
{
return handle;
}
if (receiver == nullptr)
return handle;
handle.m_key = key;
handle.m_receiver = receiver;
handle.function = function;
QList<IKeyboard::RegistrationHandle> functions = m_registeredFunctions.value(key);
functions.append(handle);
m_registeredFunctions.insert(key, functions);
return handle;
}
void CKeyboardMac::unregisterHotkeyImpl(const IKeyboard::RegistrationHandle &handle)
{
QList<IKeyboard::RegistrationHandle> functions = m_registeredFunctions.value(handle.m_key);
functions.removeAll(handle);
m_registeredFunctions.insert(handle.m_key, functions);
}
void CKeyboardMac::unregisterAllHotkeysImpl()
{
m_registeredFunctions.clear();
}
void CKeyboardMac::sendCaptureNotification(const CKeyboardKey &key, bool isFinished)
{
if (isFinished)
emit keySelectionFinished(key);
else
emit keySelectionChanged(key);
}
void CKeyboardMac::callFunctionsBy(const CKeyboardKey &key, bool isPressed)
{
QList<IKeyboard::RegistrationHandle> functionHandles = m_registeredFunctions.value(key);
foreach (IKeyboard::RegistrationHandle functionHandle, functionHandles)
{
if (functionHandle.m_receiver.isNull())
{
continue;
}
functionHandle.function(isPressed);
}
}
}

View File

@@ -1,87 +0,0 @@
/* 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/. */
/*!
\file
*/
#ifndef BLACKCORE_KEYBOARD_MAC_H
#define BLACKCORE_KEYBOARD_MAC_H
#include "blackcore/keyboard.h"
#include "blackmisc/hwkeyboardkey.h"
#include <QHash>
namespace BlackCore
{
//! \brief Linux implemenation of IKeyboard using hook procedure
//! \todo Change QHash to a CCollection object
class CKeyboardMac : public IKeyboard
{
Q_OBJECT
public:
//! \brief Destructor
virtual ~CKeyboardMac();
//! \copydoc IKeyboard::selectKey()
virtual void startCapture(bool ignoreNextKey = false) override;
//! \copydoc IKeyboard::sizeOfRegisteredFunctions()
virtual int sizeOfRegisteredFunctions() const override;
//! \copydoc IKeyboard::triggerKey()
virtual void triggerKey(const BlackMisc::Hardware::CKeyboardKey key, bool isPressed) override;
protected:
friend class IKeyboard;
//! \brief Constructor
CKeyboardMac(QObject *parent = nullptr);
//! \brief Copy Constructor
CKeyboardMac(CKeyboardMac const&);
//! \copydoc IKeyboard::init()
virtual bool init() override;
//! \brief Assignment operator
void operator=(CKeyboardMac const&);
//! \copydoc IKeyboard::registerHotKeyImpl()
virtual IKeyboard::RegistrationHandle registerHotkeyImpl(BlackMisc::Hardware::CKeyboardKey key, QObject *receiver, std::function<void(bool)> function) override;
//! \copydoc IKeyboard::unregisterHotkeyImpl()
virtual void unregisterHotkeyImpl(const IKeyboard::RegistrationHandle &handle) override;
//! \copydoc IKeyboard::unregisterHotkeyImpl()
virtual void unregisterAllHotkeysImpl() override;
private:
/*!
* \brief Constructor
* \param keySet
* \param isFinished
*/
void sendCaptureNotification(const BlackMisc::Hardware::CKeyboardKey &key, bool isFinished);
/*!
* \brief Calls registered functions on keyboard event
* \param keySet
* \param isPressed
*/
void callFunctionsBy(const BlackMisc::Hardware::CKeyboardKey &keySet, bool isPressed);
QHash<BlackMisc::Hardware::CKeyboardKey, QList<IKeyboard::RegistrationHandle>> m_registeredFunctions; //!< Registered hotkey functions
BlackMisc::Hardware::CKeyboardKey m_pressedKey; //!< Set of virtual keys pressed in the last cycle
bool m_ignoreNextKey; //!< Is true if the next key needs to be ignored
Mode m_mode; //!< Operation mode
};
}
#endif // BLACKCORE_KEYBOARD_MAC_H

View File

@@ -1,185 +0,0 @@
/* 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 "keyboard_windows.h"
#include "keymapping_windows.h"
#include <QDebug>
using namespace BlackMisc::Hardware;
namespace BlackCore
{
CKeyboardWindows::CKeyboardWindows(QObject *parent) :
IKeyboard(parent),
m_keyboardHook(nullptr),
m_mode(Mode_Nominal)
{
}
CKeyboardWindows::~CKeyboardWindows()
{
if (m_keyboardHook)
UnhookWindowsHookEx(m_keyboardHook);
}
bool CKeyboardWindows::init()
{
m_keyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL, CKeyboardWindows::keyboardProc, GetModuleHandle(NULL), 0);
return true;
}
void CKeyboardWindows::startCapture(bool ignoreNextKey)
{
m_mode = Mode_Capture;
m_ignoreNextKey = ignoreNextKey;
m_pressedKey.setKeyObject(CKeyboardKey());
}
int CKeyboardWindows::sizeOfRegisteredFunctions() const
{
int size = 0;
foreach (QList<IKeyboard::RegistrationHandle> functions, m_registeredFunctions)
{
size += functions.size();
}
return size;
}
void CKeyboardWindows::triggerKey(const CKeyboardKey key, bool isPressed)
{
callFunctionsBy(key, isPressed);
}
void CKeyboardWindows::keyEvent(WPARAM vkcode, uint event)
{
BlackMisc::Hardware::CKeyboardKey lastPressedKey = m_pressedKey;
if (m_ignoreNextKey)
{
m_ignoreNextKey = false;
return;
}
bool isFinished = false;
if ((event == WM_KEYDOWN) || (event == WM_SYSKEYDOWN))
{
if (CKeyMappingWindows::isModifier(vkcode))
m_pressedKey.addModifier(CKeyMappingWindows::convertToModifier(vkcode));
else
{
m_pressedKey.setKey(CKeyMappingWindows::convertToKey(vkcode));
m_pressedKey.setNativeVirtualKey(vkcode);
}
}
else if ((event == WM_KEYUP) || (event == WM_SYSKEYUP) )
{
if (CKeyMappingWindows::isModifier(vkcode))
m_pressedKey.removeModifier(CKeyMappingWindows::convertToModifier(vkcode));
else
{
m_pressedKey.setKey(Qt::Key_unknown);
m_pressedKey.setNativeVirtualKey(0);
}
isFinished = true;
}
if (lastPressedKey == m_pressedKey)
return;
#ifdef DEBUG_KEYBOARD_WINDOWS
qDebug() << "Virtual key: " << vkcode;
#endif
if (m_mode == Mode_Capture)
{
if (isFinished)
{
sendCaptureNotification(lastPressedKey, true);
m_mode = Mode_Nominal;
}
else
{
sendCaptureNotification(m_pressedKey, false);
}
}
else
{
callFunctionsBy(lastPressedKey, false);
callFunctionsBy(m_pressedKey, true);
}
}
IKeyboard::RegistrationHandle CKeyboardWindows::registerHotkeyImpl(BlackMisc::Hardware::CKeyboardKey key, QObject *receiver, std::function<void(bool)> function)
{
IKeyboard::RegistrationHandle handle;
// Workaround: Remove key function. Otherwise operator== will not
// work when we create the key value object by pressed keys
key.setFunction(BlackMisc::Hardware::CKeyboardKey::HotkeyNone);
if (!key.hasModifier() && !key.hasKey())
{
return handle;
}
if (receiver == nullptr)
return handle;
handle.m_key = key;
handle.m_receiver = receiver;
handle.function = function;
QList<IKeyboard::RegistrationHandle> functions = m_registeredFunctions.value(key);
functions.append(handle);
m_registeredFunctions.insert(key, functions);
return handle;
}
void CKeyboardWindows::unregisterHotkeyImpl(const IKeyboard::RegistrationHandle &handle)
{
QList<IKeyboard::RegistrationHandle> functions = m_registeredFunctions.value(handle.m_key);
functions.removeAll(handle);
m_registeredFunctions.insert(handle.m_key, functions);
}
void CKeyboardWindows::unregisterAllHotkeysImpl()
{
m_registeredFunctions.clear();
}
void CKeyboardWindows::sendCaptureNotification(const CKeyboardKey &key, bool isFinished)
{
if (isFinished)
emit keySelectionFinished(key);
else
emit keySelectionChanged(key);
}
void CKeyboardWindows::callFunctionsBy(const CKeyboardKey &key, bool isPressed)
{
QList<IKeyboard::RegistrationHandle> functionHandles = m_registeredFunctions.value(key);
foreach (IKeyboard::RegistrationHandle functionHandle, functionHandles)
{
if (functionHandle.m_receiver.isNull())
{
continue;
}
functionHandle.function(isPressed);
}
}
LRESULT CALLBACK CKeyboardWindows::keyboardProc(int nCode, WPARAM wParam, LPARAM lParam)
{
CKeyboardWindows *keyboardWindows = qobject_cast<CKeyboardWindows*>(IKeyboard::getInstance());
if (nCode == HC_ACTION)
{
KBDLLHOOKSTRUCT *keyboardEvent =reinterpret_cast<KBDLLHOOKSTRUCT*>(lParam);
WPARAM vkCode = keyboardEvent->vkCode;
keyboardWindows->keyEvent(vkCode, wParam);
}
return CallNextHookEx(keyboardWindows->keyboardHook(), nCode, wParam, lParam);
}
}

View File

@@ -1,110 +0,0 @@
/* 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/. */
/*!
\file
*/
#ifndef BLACKCORE_KEYBOARD_WINDOWS_H
#define BLACKCORE_KEYBOARD_WINDOWS_H
#include "blackcore/keyboard.h"
#include "blackmisc/hwkeyboardkey.h"
#include <QHash>
#ifndef NOMINMAX
#define NOMINMAX
#endif
#include <windows.h>
namespace BlackCore
{
//! \brief Windows implemenation of IKeyboard using hook procedure
//! \todo Change QHash to a CCollection object
class CKeyboardWindows : public IKeyboard
{
Q_OBJECT
public:
//! \brief Destructor
virtual ~CKeyboardWindows();
//! \copydoc IKeyboard::selectKey()
virtual void startCapture(bool ignoreNextKey = false) override;
//! \copydoc IKeyboard::sizeOfRegisteredFunctions()
virtual int sizeOfRegisteredFunctions() const override;
//! \copydoc IKeyboard::triggerKey()
virtual void triggerKey(const BlackMisc::Hardware::CKeyboardKey key, bool isPressed) override;
//! \brief Keyboard hook handle
HHOOK keyboardHook() const { return m_keyboardHook; }
//! \private
void keyEvent(WPARAM vkCode, uint event);
protected:
friend class IKeyboard;
//! \brief Constructor
CKeyboardWindows(QObject *parent = nullptr);
//! \brief Copy Constructor
CKeyboardWindows(CKeyboardWindows const&);
//! \copydoc IKeyboard::init()
virtual bool init() override;
//! \brief Assignment operator
void operator=(CKeyboardWindows const&);
//! \copydoc IKeyboard::registerHotKeyImpl()
virtual IKeyboard::RegistrationHandle registerHotkeyImpl(BlackMisc::Hardware::CKeyboardKey key, QObject *receiver, std::function<void(bool)> function) override;
//! \copydoc IKeyboard::unregisterHotkeyImpl()
virtual void unregisterHotkeyImpl(const IKeyboard::RegistrationHandle &handle) override;
//! \copydoc IKeyboard::unregisterHotkeyImpl()
virtual void unregisterAllHotkeysImpl() override;
private:
/*!
* \brief Constructor
* \param keySet
* \param isFinished
*/
void sendCaptureNotification(const BlackMisc::Hardware::CKeyboardKey &key, bool isFinished);
/*!
* \brief Calls registered functions on keyboard event
* \param keySet
* \param isPressed
*/
void callFunctionsBy(const BlackMisc::Hardware::CKeyboardKey &keySet, bool isPressed);
void addKey(WPARAM vkcode);
void removeKey(WPARAM vkcode);
/*!
* \brief Keyboard hook procedure
* \param nCode
* \param wParam
* \param lParam
* \return
*/
static LRESULT CALLBACK keyboardProc(int nCode, WPARAM wParam, LPARAM lParam);
QHash<BlackMisc::Hardware::CKeyboardKey, QList<IKeyboard::RegistrationHandle>> m_registeredFunctions; //!< Registered hotkey functions
BlackMisc::Hardware::CKeyboardKey m_pressedKey; //!< Set of virtual keys pressed in the last cycle
bool m_ignoreNextKey; //!< Is true if the next key needs to be ignored
HHOOK m_keyboardHook; //!< Keyboard hook handle
Mode m_mode; //!< Operation mode
};
}
#endif // BLACKCORE_KEYBOARD_WINDOWS_H

View File

@@ -1,94 +0,0 @@
/* 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 "keymapping_windows.h"
#include <QDebug>
#ifndef NOMINMAX
#define NOMINMAX
#endif
#include <windows.h>
using namespace BlackMisc::Hardware;
namespace BlackCore
{
Qt::Key CKeyMappingWindows::convertToKey(WPARAM virtualKey)
{
switch (virtualKey)
{
case '0': return Qt::Key_0; break;
case '1': return Qt::Key_1; break;
case '2': return Qt::Key_2; break;
case '3': return Qt::Key_3; break;
case '4': return Qt::Key_4; break;
case '5': return Qt::Key_5; break;
case '6': return Qt::Key_6; break;
case '7': return Qt::Key_7; break;
case '8': return Qt::Key_8; break;
case '9': return Qt::Key_9; break;
case 'A': return Qt::Key_A; break;
case 'B': return Qt::Key_B; break;
case 'C': return Qt::Key_C; break;
case 'D': return Qt::Key_D; break;
case 'E': return Qt::Key_E; break;
case 'F': return Qt::Key_F; break;
case 'G': return Qt::Key_G; break;
case 'H': return Qt::Key_H; break;
case 'I': return Qt::Key_I; break;
case 'J': return Qt::Key_J; break;
case 'K': return Qt::Key_K; break;
case 'L': return Qt::Key_L; break;
case 'M': return Qt::Key_M; break;
case 'N': return Qt::Key_N; break;
case 'O': return Qt::Key_O; break;
case 'P': return Qt::Key_P; break;
case 'Q': return Qt::Key_Q; break;
case 'R': return Qt::Key_R; break;
case 'S': return Qt::Key_S; break;
case 'T': return Qt::Key_T; break;
case 'U': return Qt::Key_U; break;
case 'V': return Qt::Key_V; break;
case 'W': return Qt::Key_W; break;
case 'X': return Qt::Key_X; break;
case 'Y': return Qt::Key_Y; break;
case 'Z': return Qt::Key_Z; break;
default: return Qt::Key_unknown; break;
}
}
CKeyboardKey::Modifier CKeyMappingWindows::convertToModifier(WPARAM virtualKey)
{
switch (virtualKey)
{
qDebug() << virtualKey;
case VK_LSHIFT: return CKeyboardKey::ModifierShiftLeft; break;
case VK_RSHIFT: return CKeyboardKey::ModifierShiftRight; break;
case VK_LCONTROL: return CKeyboardKey::ModifierCtrlLeft; break;
case VK_RCONTROL: return CKeyboardKey::ModifierCtrlRight; break;
case VK_LMENU: return CKeyboardKey::ModifierAltLeft; break;
case VK_RMENU: return CKeyboardKey::ModifierAltRight; break;
default: return CKeyboardKey::ModifierNone; break;
}
}
bool CKeyMappingWindows::isModifier(WPARAM vkcode)
{
switch (vkcode)
{
case VK_LSHIFT:
case VK_RSHIFT:
case VK_LCONTROL:
case VK_RCONTROL:
case VK_LMENU:
case VK_RMENU:
case VK_LWIN:
case VK_RWIN:
return true;
default: return false;
}
}
} // namespace BlackCore

View File

@@ -1,45 +0,0 @@
/* 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_KEYMAPPING_WINDOWS_H
#define BLACKCORE_KEYMAPPING_WINDOWS_H
#include "blackmisc/hwkeyboardkey.h"
#ifndef NOMINMAX
#define NOMINMAX
#endif
#include <windows.h>
namespace BlackCore
{
//! \brief This class provides methods to map between windows virtual keys and CKeyboardKey
class CKeyMappingWindows
{
public:
/*!
* \brief Converts a set of windows virtual keys to a CKeySet object
* \param virtualKey
* \return
*/
static BlackMisc::Hardware::CKeyboardKey::Modifier convertToModifier(WPARAM virtualKey);
/*!
* \brief Convert to Qt key
* \param virtualKey
* \return
*/
static Qt::Key convertToKey(WPARAM virtualKey);
/*!
* \brief Checks if its a modifier key
* \param vkcode
* \return
*/
static bool isModifier(WPARAM vkcode);
};
} // namespace BlackCore
#endif // guard