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

@@ -30,6 +30,11 @@ namespace BlackInput
return true;
}
void CKeyboardWindows::setKeysToMonitor(const CKeyboardKeyList &keylist)
{
m_listMonitoredKeys = keylist;
}
void CKeyboardWindows::startCapture(bool ignoreNextKey)
{
m_mode = Mode_Capture;
@@ -37,22 +42,13 @@ namespace BlackInput
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);
if(!isPressed) emit keyUp(key);
else emit keyDown(key);
}
void CKeyboardWindows::keyEvent(WPARAM vkcode, uint event)
void CKeyboardWindows::processKeyEvent(WPARAM vkcode, uint event)
{
BlackMisc::Hardware::CKeyboardKey lastPressedKey = m_pressedKey;
if (m_ignoreNextKey)
@@ -103,51 +99,11 @@ namespace BlackInput
}
else
{
callFunctionsBy(lastPressedKey, false);
callFunctionsBy(m_pressedKey, true);
if (m_listMonitoredKeys.contains(lastPressedKey)) emit keyUp(lastPressedKey);
if (m_listMonitoredKeys.contains(m_pressedKey)) emit keyDown(m_pressedKey);
}
}
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)
@@ -156,27 +112,16 @@ namespace BlackInput
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)
{
// This is the reason why we have to use singleton pattern. We cannot pass a object pointer to
// keyboardProc.
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);
keyboardWindows->processKeyEvent(vkCode, wParam);
}
return CallNextHookEx(keyboardWindows->keyboardHook(), nCode, wParam, lParam);
}

View File

@@ -12,6 +12,7 @@
#include "blackinput/keyboard.h"
#include "blackmisc/hwkeyboardkey.h"
#include "blackmisc/hwkeyboardkeylist.h"
#include <QHash>
#ifndef NOMINMAX
#define NOMINMAX
@@ -30,12 +31,12 @@ namespace BlackInput
//! \brief Destructor
virtual ~CKeyboardWindows();
//! \copydoc IKeyboard::setKeysToMonitor()
virtual void setKeysToMonitor(const BlackMisc::Hardware::CKeyboardKeyList &keylist) override;
//! \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;
@@ -43,7 +44,7 @@ namespace BlackInput
HHOOK keyboardHook() const { return m_keyboardHook; }
//! \private
void keyEvent(WPARAM vkCode, uint event);
void processKeyEvent(WPARAM vkCode, uint event);
protected:
@@ -61,15 +62,6 @@ namespace BlackInput
//! \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:
/*!
@@ -79,13 +71,6 @@ namespace BlackInput
*/
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);
@@ -99,7 +84,7 @@ namespace BlackInput
static LRESULT CALLBACK keyboardProc(int nCode, WPARAM wParam, LPARAM lParam);
QHash<BlackMisc::Hardware::CKeyboardKey, QList<IKeyboard::RegistrationHandle>> m_registeredFunctions; //!< Registered hotkey functions
BlackMisc::Hardware::CKeyboardKeyList m_listMonitoredKeys; //!< Registered keys
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