[feat] keyboard capture supported on Windows

This commit is contained in:
dijunkun
2024-11-22 16:39:01 +08:00
parent df7489f8e2
commit 52828183a1
13 changed files with 285 additions and 55 deletions

View File

@@ -43,14 +43,20 @@ typedef struct {
};
} RemoteAction;
// int key_code, bool is_down
typedef void (*OnKeyAction)(int, bool, void*);
class DeviceController {
public:
virtual ~DeviceController() {}
public:
virtual int Init(int screen_width, int screen_height) = 0;
virtual int Destroy() = 0;
virtual int SendCommand(RemoteAction remote_action) = 0;
// virtual int Init(int screen_width, int screen_height);
// virtual int Destroy();
// virtual int SendCommand(RemoteAction remote_action);
// virtual int Hook();
// virtual int Unhook();
};
#endif

View File

@@ -8,6 +8,7 @@
#define _DEVICE_CONTROLLER_FACTORY_H_
#include "device_controller.h"
#include "keyboard_capturer.h"
#include "mouse_controller.h"
class DeviceControllerFactory {
@@ -23,7 +24,7 @@ class DeviceControllerFactory {
case Mouse:
return new MouseController();
case Keyboard:
return nullptr;
return new KeyboardCapturer();
default:
return nullptr;
}

View File

@@ -0,0 +1,9 @@
#include "keyboard_capturer.h"
KeyboardCapturer::KeyboardCapturer() {}
KeyboardCapturer::~KeyboardCapturer() {}
int KeyboardCapturer::Hook() { return 0; }
int KeyboardCapturer::Unhook() { return 0; }

View File

@@ -0,0 +1,24 @@
/*
* @Author: DI JUNKUN
* @Date: 2024-11-22
* Copyright (c) 2024 by DI JUNKUN, All Rights Reserved.
*/
#ifndef _KEYBOARD_CAPTURER_H_
#define _KEYBOARD_CAPTURER_H_
#include "device_controller.h"
class KeyboardCapturer : public DeviceController {
public:
KeyboardCapturer();
virtual ~KeyboardCapturer();
public:
virtual int Hook();
virtual int Unhook();
private:
};
#endif

View File

@@ -0,0 +1,9 @@
#include "keyboard_capturer.h"
KeyboardCapturer::KeyboardCapturer() {}
KeyboardCapturer::~KeyboardCapturer() {}
int KeyboardCapturer::Hook() { return 0; }
int KeyboardCapturer::Unhook() { return 0; }

View File

@@ -0,0 +1,24 @@
/*
* @Author: DI JUNKUN
* @Date: 2024-11-22
* Copyright (c) 2024 by DI JUNKUN, All Rights Reserved.
*/
#ifndef _KEYBOARD_CAPTURER_H_
#define _KEYBOARD_CAPTURER_H_
#include "device_controller.h"
class KeyboardCapturer : public DeviceController {
public:
KeyboardCapturer();
virtual ~KeyboardCapturer();
public:
virtual int Hook();
virtual int Unhook();
private:
};
#endif

View File

@@ -0,0 +1,55 @@
#include "keyboard_capturer.h"
#include "rd_log.h"
static OnKeyAction g_on_key_action = nullptr;
static void* g_user_ptr = nullptr;
LRESULT CALLBACK KeyboardProc(int nCode, WPARAM wParam, LPARAM lParam) {
if (nCode == HC_ACTION && g_on_key_action) {
KBDLLHOOKSTRUCT* kbData = reinterpret_cast<KBDLLHOOKSTRUCT*>(lParam);
// 处理键盘事件
if (wParam == WM_KEYDOWN) {
// LOG_ERROR("Keydown: [{}]", kbData->vkCode);
g_on_key_action(kbData->vkCode, true, g_user_ptr);
return 1;
} else if (wParam == WM_KEYUP) {
// LOG_ERROR("Keyup: [{}]", kbData->vkCode);
g_on_key_action(kbData->vkCode, false, g_user_ptr);
return 1;
} else if (wParam == WM_SYSKEYDOWN) {
// LOG_ERROR("System keydown: [{}]", kbData->vkCode);
g_on_key_action(kbData->vkCode, true, g_user_ptr);
return 1;
} else if (wParam == WM_SYSKEYUP) {
// LOG_ERROR("System keyup: [{}]", kbData->vkCode);
g_on_key_action(kbData->vkCode, false, g_user_ptr);
return 1;
}
}
// 调用下一个钩子
return CallNextHookEx(NULL, nCode, wParam, lParam);
}
KeyboardCapturer::KeyboardCapturer() {}
KeyboardCapturer::~KeyboardCapturer() {}
int KeyboardCapturer::Hook(OnKeyAction on_key_action, void* user_ptr) {
g_on_key_action = on_key_action;
g_user_ptr = user_ptr;
keyboard_hook_ = SetWindowsHookEx(WH_KEYBOARD_LL, KeyboardProc, NULL, 0);
if (!keyboard_hook_) {
LOG_ERROR("Failed to install keyboard hook!")
return -1;
}
return 0;
}
int KeyboardCapturer::Unhook() {
UnhookWindowsHookEx(keyboard_hook_);
return 0;
}

View File

@@ -0,0 +1,27 @@
/*
* @Author: DI JUNKUN
* @Date: 2024-11-22
* Copyright (c) 2024 by DI JUNKUN, All Rights Reserved.
*/
#ifndef _KEYBOARD_CAPTURER_H_
#define _KEYBOARD_CAPTURER_H_
#include <Windows.h>
#include "device_controller.h"
class KeyboardCapturer : public DeviceController {
public:
KeyboardCapturer();
virtual ~KeyboardCapturer();
public:
virtual int Hook(OnKeyAction on_key_action, void *user_ptr);
virtual int Unhook();
private:
HHOOK keyboard_hook_ = nullptr;
};
#endif