mirror of
https://github.com/kunkundi/crossdesk.git
synced 2025-10-27 04:35:34 +08:00
[feat] keyboard capture supported on Windows
This commit is contained in:
@@ -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
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
#include "keyboard_capturer.h"
|
||||
|
||||
KeyboardCapturer::KeyboardCapturer() {}
|
||||
|
||||
KeyboardCapturer::~KeyboardCapturer() {}
|
||||
|
||||
int KeyboardCapturer::Hook() { return 0; }
|
||||
|
||||
int KeyboardCapturer::Unhook() { return 0; }
|
||||
24
src/device_controller/keyboard/linux/keyboard_capturer.h
Normal file
24
src/device_controller/keyboard/linux/keyboard_capturer.h
Normal 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
|
||||
9
src/device_controller/keyboard/mac/keyboard_capturer.cpp
Normal file
9
src/device_controller/keyboard/mac/keyboard_capturer.cpp
Normal file
@@ -0,0 +1,9 @@
|
||||
#include "keyboard_capturer.h"
|
||||
|
||||
KeyboardCapturer::KeyboardCapturer() {}
|
||||
|
||||
KeyboardCapturer::~KeyboardCapturer() {}
|
||||
|
||||
int KeyboardCapturer::Hook() { return 0; }
|
||||
|
||||
int KeyboardCapturer::Unhook() { return 0; }
|
||||
24
src/device_controller/keyboard/mac/keyboard_capturer.h
Normal file
24
src/device_controller/keyboard/mac/keyboard_capturer.h
Normal 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
|
||||
55
src/device_controller/keyboard/windows/keyboard_capturer.cpp
Normal file
55
src/device_controller/keyboard/windows/keyboard_capturer.cpp
Normal 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;
|
||||
}
|
||||
27
src/device_controller/keyboard/windows/keyboard_capturer.h
Normal file
27
src/device_controller/keyboard/windows/keyboard_capturer.h
Normal 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
|
||||
Reference in New Issue
Block a user