refs #313 Remove the responsibility from IKeyboard to register

and call functions. Instead just monitor key ups and downs.
This commit is contained in:
Roland Winklmeier
2014-08-04 20:24:48 +02:00
parent e06cb7d683
commit 2f527f72c3
8 changed files with 53 additions and 374 deletions

View File

@@ -10,7 +10,7 @@
#ifndef BLACKINPUT_KEYBOARD_H
#define BLACKINPUT_KEYBOARD_H
#include "../blackmisc/hwkeyboardkey.h"
#include "blackmisc/hwkeyboardkeylist.h"
#include <QMultiMap>
#include <QObject>
#include <QPointer>
@@ -27,17 +27,6 @@ namespace BlackInput
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
{
@@ -51,64 +40,7 @@ namespace BlackInput
//! 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();
}
virtual void setKeysToMonitor(const BlackMisc::Hardware::CKeyboardKeyList &keylist) = 0;
/*!
* \brief Select a key combination as hotkey. This method returns immediatly.
@@ -120,12 +52,6 @@ namespace BlackInput
*/
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
@@ -133,6 +59,9 @@ namespace BlackInput
*/
virtual void triggerKey(const BlackMisc::Hardware::CKeyboardKey key, bool isPressed) = 0;
//! Creates a native keyboard handler object
static IKeyboard *getInstance();
signals:
/*!
@@ -147,6 +76,9 @@ namespace BlackInput
*/
void keySelectionFinished(BlackMisc::Hardware::CKeyboardKey key);
void keyDown(const BlackMisc::Hardware::CKeyboardKey &);
void keyUp(const BlackMisc::Hardware::CKeyboardKey &);
protected:
/*!
@@ -154,31 +86,10 @@ namespace BlackInput
*/
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 // BLACKINPUT_KEYBOARD_H