QtGlobalInput is small library that implements windows global hooks in Qt.
All funktions in QtGlobalInput are static and should be called with
QtGlobalInput::functionName()
Before creating hooks, QtGlobalInput must be initialized with
QtGlobalInput::init(hwnd)
Example:
QtGlobalInput::init((HWND)MainWindow::winId());
If you don't need hooks anymore, you can deinialize QtGlobalInput with
QtGlobalInput::deinitialize()
All funktions are returning hook id. With this id hook can be removed.
enum class EventType
{
All,
ButtonDown,
ButtonUp
};
static uint setLlKeyboardHook(uint vkCode, EventType type, A callback, B obj, bool async = false);
- vkCode - vkCode of the key you want to hook. If vkCode is 0, will hook all key presses
- type - decides if should listen to key down or key up
- callback - funktion that will be called when hook triggers. Should have form:
void function(int nCode, WPARAM wParam, LPARAM lParam)
- obj - object that has callback funktion
- async - if true, callback funktion will be called asynchronous
static uint waitForKeyPress(uint vkCode, EventType type, A callback, B obj, bool async = false);
- vkCode - vkCode of the key you want to hook. If vkCode is 0, will hook all key presses
- type - decides if should listen to key down or key up
- callback - funktion that will be called when hook triggers. Should have form:
void function(RAWKEYBOARD rawkeyboard)
- obj - object that has callback funktion
- async - if true, callback funktion will be called asynchronous
static uint waitForMousePress(uint vkCode, EventType type, A callback, B obj, bool async = false);
- vkCode - vkCode of the key you want to hook. If vkCode is 0, will hook all key presses
- type - decides if should listen to key down or key up
- callback - funktion that will be called when hook triggers. Should have form:
void function(RAWMOUSE rawmouse)
- obj - object that has callback funktion
- async - if true, callback funktion will be called asynchronous
static uint setWindowSwitch(A callback, B obj)
- callback - funktion that will be called when hook triggers. Should have form -
void function(HWND hwnd)
- obj - object that has callback funktion
This funktions will remove hook by his id and return true if hook is successfully deleted.
1. static bool removeLlKeyboardHook(uint id);
2. static bool removeKeyPress(uint id);
3. static bool removeMousePress(uint id);
4. static bool removeWindowSwitch(uint id);
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
QtGlobalInput::init((HWND)MainWindow::winId());
uint keyPressId = QtGlobalInput::waitForKeyPress(0, EventType::ButtonUp, &MainWindow::handleKey, this, true);
uint windowSwitchId = QtGlobalInput::setWindowSwitch(&MainWindow::windowSwitched, this);
}
void MainWindow::windowSwitched(HWND hwnd) { ... }
void MainWindow::handleKey(RAWKEYBOARD keyboard) { ... }