FakerInput 键盘鼠标输入封装

CFakerInputHelper.h

cpp 复制代码
#pragma once

#include <windows.h>
#include <vector>
#include "FakerInputDll/include/fakerinputclient.h"
#include <stdint.h>
#include <string>
#include <tchar.h>

#ifdef _UNICODE
using _tstring = std::wstring;
#else
using _tstring = std::string;
#endif

#ifdef _WIN64
#pragma comment(lib,"FakerInputDll/lib/x64/FakerInputDll.lib")
#else
#pragma comment(lib,"FakerInputDll/lib/x86/FakerInputDll.lib")
#endif

// 禁用无名结构体/联合体警告 Disable warning C4201:nameless struct/union
#if _MSC_VER >= 1200
#pragma warning(push)
#endif
#pragma warning(disable : 4201)

// 鼠标按键按下标志
enum eFakerInputMouseKey
{
    eMouseKey_Left        = 0x01 << 0,
    eMouseKey_Right       = 0x01 << 1,
    eMouseKey_Middle      = 0x01 << 2,
    eMouseKey_XButton1    = 0x01 << 3,
    eMouseKey_XButton2    = 0x01 << 4,
};

// 修饰键
enum eFakerInputShiftFlag
{
    eShiftFlag_LControl   = 0x01 << 0,
    eShiftFlag_LShift     = 0x01 << 1,
    eShiftFlag_LAlt       = 0x01 << 2,
    eShiftFlag_LWin       = 0x01 << 3,
    eShiftFlag_RControl   = 0x01 << 4,
    eShiftFlag_RShift     = 0x01 << 5,
    eShiftFlag_RAlt       = 0x01 << 6,
    eShiftFlag_RWin       = 0x01 << 7,
};

typedef union _MOUSE_BUTTON
{
    uint8_t Button;
    struct
    {
        uint8_t Left : 1;           // 鼠标左键
        uint8_t Right : 1;          // 鼠标右键
        uint8_t Middle : 1;         // 鼠标中键
        uint8_t X1 : 1;             // 附加按钮1
        uint8_t X2 : 1;             // 附加按钮2
    };

    _MOUSE_BUTTON(bool fL = 0, bool fR = 0, bool fM = 0, bool fX1 = 0, bool fX2 = 0)
    {
        Button = 0;
        Left = fL;
        Right = fR;
        Middle = fM;
        X1 = fX1;
        X2 = fX2;
    }

}MOUSE_BUTTON;

typedef union _MODIFIER_KEY
{
    uint8_t Key;
    struct
    {
        uint8_t LCtrl : 1;          // 左Ctrl
        uint8_t LShift : 1;         // 左Shift
        uint8_t LAlt : 1;           // 左Alt
        uint8_t LWin : 1;           // 左Win
        uint8_t RCtrl : 1;          // 右Ctrl
        uint8_t RShift : 1;         // 右Shift
        uint8_t RAlt : 1;           // 右Alt
        uint8_t RWin : 1;           // 右Win
    };

    _MODIFIER_KEY(
        bool lc = 0, bool ls = 0, bool la = 0, bool lw = 0, 
        bool rc = 0, bool rs = 0, bool ra = 0, bool rw = 0
    )
    {
        Key = 0;
        LCtrl   = lc;
        LShift  = ls;
        LAlt    = la;
        LWin    = lw;
        RCtrl   = rc;
        RShift  = rs;
        RAlt    = ra;
        RWin    = rw;
    }

}MODIFIER_KEY;

// 普通按键
enum eFakerInputKey
{
    eKey_A                  = 0x04,
    eKey_B                  = 0x05,
    eKey_C                  = 0x06,
    eKey_D                  = 0x07,
    eKey_E                  = 0x08,
    eKey_F                  = 0x09,
    eKey_G                  = 0x0A,
    eKey_H                  = 0x0B,
    eKey_I                  = 0x0C,
    eKey_J                  = 0x0D,
    eKey_K                  = 0x0E,
    eKey_L                  = 0x0F,
    eKey_M                  = 0x10,
    eKey_N                  = 0x11,
    eKey_O                  = 0x12,
    eKey_P                  = 0x13,
    eKey_Q                  = 0x14,
    eKey_R                  = 0x15,
    eKey_S                  = 0x16,
    eKey_T                  = 0x17,
    eKey_U                  = 0x18,
    eKey_V                  = 0x19,
    eKey_W                  = 0x1A,
    eKey_X                  = 0x1B,
    eKey_Y                  = 0x1C,
    eKey_Z                  = 0x1D,

    eKey_Number_1           = 0x1E,
    eKey_Number_2           = 0x1F,
    eKey_Number_3           = 0x20,
    eKey_Number_4           = 0x21,
    eKey_Number_5           = 0x22,
    eKey_Number_6           = 0x23,
    eKey_Number_7           = 0x24,
    eKey_Number_8           = 0x25,
    eKey_Number_9           = 0x26,
    eKey_Number_0           = 0x27,

    eKey_Enter              = 0x28,
    eKey_Escape             = 0x29,
    eKey_Backspace          = 0x2A,
    eKey_Tab                = 0x2B,
    eKey_Spacebar           = 0x2C,
    eKey_Minus              = 0x2D,
    eKey_Equals             = 0x2E,
    eKey_OpenBrace          = 0x2F,
    eKey_CloseBrace         = 0x30,
    eKey_Backslash          = 0x31,
    eKey_Hash               = 0x32,
    eKey_Semicolon          = 0x33,
    eKey_Quote              = 0x34,
    eKey_Tilde              = 0x35,
    eKey_Comma              = 0x36,
    eKey_Dot                = 0x37,
    eKey_ForwardSlash       = 0x38,
    eKey_CapsLock           = 0x39,

    eKey_F1                 = 0x3A,
    eKey_F2                 = 0x3B,
    eKey_F3                 = 0x3C,
    eKey_F4                 = 0x3D,
    eKey_F5                 = 0x3E,
    eKey_F6                 = 0x3F,
    eKey_F7                 = 0x40,
    eKey_F8                 = 0x41,
    eKey_F9                 = 0x42,
    eKey_F10                = 0x43,
    eKey_F11                = 0x44,
    eKey_F12                = 0x45,
    eKey_F13                = 0x68, //不支持
    eKey_F14                = 0x69, //不支持
    eKey_F15                = 0x6A, //不支持
    eKey_F16                = 0x6B, //不支持
    eKey_F17                = 0x6C, //不支持
    eKey_F18                = 0x6D, //不支持
    eKey_F19                = 0x6E, //不支持
    eKey_F20                = 0x6F, //不支持
    eKey_F21                = 0x70, //不支持
    eKey_F22                = 0x71, //不支持
    eKey_F23                = 0x72, //不支持
    eKey_F24                = 0x73, //不支持

    eKey_PrintScreen        = 0x46,
    eKey_ScrollLock         = 0x47,
    eKey_Pause              = 0x48,
    eKey_Insert             = 0x49,
    eKey_Home               = 0x4A,
    eKey_PageUp             = 0x4B,
    eKey_Delete             = 0x4C,
    eKey_End                = 0x4D,
    eKey_PageDown           = 0x4E,
    eKey_RightArrow         = 0x4F,
    eKey_LeftArrow          = 0x50,
    eKey_DownArrow          = 0x51,
    eKey_UpArrow            = 0x52,
    eKey_NumLock            = 0x53,
    eKey_Keypad_Divide      = 0x54,
    eKey_Keypad_Multiply    = 0x55,
    eKey_Keypad_Subtract    = 0x56,
    eKey_Keypad_Add         = 0x57,
    eKey_Keypad_Enter       = 0x58,
    eKey_Keypad_1           = 0x59,
    eKey_Keypad_2           = 0x5A,
    eKey_Keypad_3           = 0x5B,
    eKey_Keypad_4           = 0x5C,
    eKey_Keypad_5           = 0x5D,
    eKey_Keypad_6           = 0x5E,
    eKey_Keypad_7           = 0x5F,
    eKey_Keypad_8           = 0x60,
    eKey_Keypad_9           = 0x61,
    eKey_Keypad_0           = 0x62,
    eKey_Keypad_Decimal     = 0x63,
    eKey_Keypad_Separator   = 0x64,
    eKey_Keypad_Application = 0x65
};

class CFakerInputHelper
{
public:

    CFakerInputHelper();
    ~CFakerInputHelper();

    bool Initialize();
    void Uninitialize();
    bool IsConnected() const;
    void ReleaseKeys();
    void ReleaseMouseKey();

    // 相对移动
    bool MouseMove(
        uint16_t x,                 // 横向移动像素值
        uint16_t y                  // 纵向移动像素值
    );

    // 绝对移动
    bool MouseMoveTo(
        uint16_t x,                 // 归一化绝对横坐标 0 - 32767
        uint16_t y                  // 归一化绝对纵坐标 0 - 32767
    );

    // 滚轮
    bool MouseWheel(
        int8_t v,                   //  垂直滚动值
        int8_t h = 0                //  水平滚动值
    );

    // 单击
    bool MouseClick(
        MOUSE_BUTTON button,        // 鼠标按键
        uint16_t x = 0,             // 归一化绝对横坐标 0 - 32767
        uint16_t y = 0              // 归一化绝对纵坐标 0 - 32767
    );

    // 键盘
    bool Keyboard(
        MODIFIER_KEY modifier,      // 修饰键
        uint8_t keyCode,            // 普通键
        bool fDown = true           // 是否按下
    );

    // 普通键按下
    bool KeyboardDown(
        uint8_t key1 = 0,           // 普通键1
        uint8_t key2 = 0,           // 普通键2
        uint8_t key3 = 0,           // 普通键3
        uint8_t key4 = 0,           // 普通键4
        uint8_t key5 = 0,           // 普通键5
        uint8_t key6 = 0            // 普通键6
    );

    // 普通键弹起
    bool KeyboardUp(
        uint8_t key1 = 0,           // 普通键1
        uint8_t key2 = 0,           // 普通键2
        uint8_t key3 = 0,           // 普通键3
        uint8_t key4 = 0,           // 普通键4
        uint8_t key5 = 0,           // 普通键5
        uint8_t key6 = 0            // 普通键6
    );

    // 修饰键 + 普通键按下
    bool KeyboardDown(
        MODIFIER_KEY modifier,      // 修饰键
        uint8_t key1 = 0,           // 普通键1
        uint8_t key2 = 0,           // 普通键2
        uint8_t key3 = 0,           // 普通键3
        uint8_t key4 = 0,           // 普通键4
        uint8_t key5 = 0,           // 普通键5
        uint8_t key6 = 0            // 普通键6
    );

    // 修饰键 + 普通键弹起
    bool KeyboardUp(
        MODIFIER_KEY modifier,      // 修饰键
        uint8_t key1 = 0,           // 普通键1
        uint8_t key2 = 0,           // 普通键2
        uint8_t key3 = 0,           // 普通键3
        uint8_t key4 = 0,           // 普通键4
        uint8_t key5 = 0,           // 普通键5
        uint8_t key6 = 0            // 普通键6
    );

    // 键盘
    bool Keyboard(
        MODIFIER_KEY modifier,      // 修饰键
        std::vector<uint8_t> vkeys, // 普通键列表
        bool fDown = true           // 是否按下
    );

    // 按钮
    bool MouseButton(
        MOUSE_BUTTON button,        // 鼠标按钮
        bool fDown = true,          // 是否按下按钮
        uint16_t x = 0,             // 归一化绝对横坐标 0 - 32767
        uint16_t y = 0              // 归一化绝对纵坐标 0 - 32767
    );

private:

    // 键盘
    bool _Keyboard(
        MODIFIER_KEY modifier,      // 修饰键
        const uint8_t* keys,        // 普通键
        size_t count,               // 按键数量(0 - 6)
        bool fDown                  // 是否按下
    );

private:

    uint8_t                 m_Keys[6] = { 0 };          // 当前普通键
    MODIFIER_KEY            m_Modifier;                 // 当前修饰键
    MOUSE_BUTTON            m_MouseButton;              // 当前鼠标键
    pfakerinput_client      m_pClient;                  // 客户端
    bool                    m_fConnect;                 // 连接状态
};

CFakerInputHelper.cpp

cpp 复制代码
#include "CFakerInputHelper.h"

CFakerInputHelper::CFakerInputHelper()
    :
    m_pClient(nullptr),
    m_fConnect(false)
{
    memset(m_Keys, 0, sizeof(m_Keys));
    m_Modifier.Key = 0;
    m_MouseButton.Button = 0;
}

CFakerInputHelper::~CFakerInputHelper()
{
    Uninitialize();
}


bool CFakerInputHelper::Initialize()
{
    bool bResult = false;

    if (m_pClient)
    {
        return true;
    }

    do
    {
        m_pClient = fakerinput_alloc();
        if (!m_pClient)
        {
            break;
        }

        m_fConnect = fakerinput_connect(m_pClient);
        if (!m_fConnect)
        {
            break;
        }

        bResult = true;

    } while (false);

    if (!bResult)
    {
        Uninitialize();
    }

    return bResult;
}

void CFakerInputHelper::Uninitialize()
{
    if (m_pClient)
    {
        ReleaseKeys();
        ReleaseMouseKey();
        if (m_fConnect)
        {
            fakerinput_disconnect(m_pClient);
        }
        fakerinput_free(m_pClient);
        m_pClient = nullptr;
        m_fConnect = false;
    }
}

bool CFakerInputHelper::IsConnected() const
{
    return m_fConnect;
}

void CFakerInputHelper::ReleaseKeys()
{
    if (m_pClient)
    {
        uint8_t codes[6] = { 0 };
        fakerinput_update_keyboard(m_pClient, 0, codes);
    }
}

void CFakerInputHelper::ReleaseMouseKey()
{
    if (m_pClient)
    {
        fakerinput_update_absolute_mouse(m_pClient, 0, 0, 0, 0, 0);
    }
}

bool CFakerInputHelper::MouseMove(
    uint16_t x, 
    uint16_t y
)
{
    if (!(m_pClient && m_fConnect))
    {
        return false;
    }

    fakerinput_update_relative_mouse(m_pClient, m_MouseButton.Button, x, y, 0, 0);

    return true;
}

bool CFakerInputHelper::MouseMoveTo(
    uint16_t x, 
    uint16_t y
)
{
    if (!(m_pClient && m_fConnect))
    {
        return false;
    }

    fakerinput_update_absolute_mouse(m_pClient, m_MouseButton.Button, x, y, 0, 0);

    return true;
}

bool CFakerInputHelper::MouseWheel(
    int8_t v, 
    int8_t h
)
{
    if (!(m_pClient && m_fConnect))
    {
        return false;
    }

    fakerinput_update_relative_mouse(m_pClient, m_MouseButton.Button, 0, 0, v, h);

    return true;
}

bool CFakerInputHelper::MouseClick(
    MOUSE_BUTTON button, 
    uint16_t x, 
    uint16_t y
)
{
    if (!(m_pClient && m_fConnect))
    {
        return false;
    }

    MouseButton(button, true, x, y);
    MouseButton(button, false, x, y);

    return true;
}

bool CFakerInputHelper::Keyboard(
    MODIFIER_KEY modifier,
    uint8_t keyCode, 
    bool fDown/* = true*/
)
{
    if (!(m_pClient && m_fConnect))
    {
        return false;
    }

    return _Keyboard(modifier, &keyCode, 1, fDown);
}

bool CFakerInputHelper::KeyboardDown(
    uint8_t key1,
    uint8_t key2,
    uint8_t key3,
    uint8_t key4,
    uint8_t key5,
    uint8_t key6 
)
{
    uint8_t keys[6] = { key1,key2,key3,key4,key5,key6 };
    return _Keyboard(MODIFIER_KEY(), keys, 6, true);
}

bool CFakerInputHelper::KeyboardUp(
    uint8_t key1,
    uint8_t key2,
    uint8_t key3,
    uint8_t key4,
    uint8_t key5,
    uint8_t key6 
)
{
    uint8_t keys[6] = { key1,key2,key3,key4,key5,key6 };
    return _Keyboard(MODIFIER_KEY(), keys, 6, false);
}

bool CFakerInputHelper::KeyboardDown(
    MODIFIER_KEY modifier,
    uint8_t key1,
    uint8_t key2,
    uint8_t key3,
    uint8_t key4,
    uint8_t key5,
    uint8_t key6
)
{
    uint8_t keys[6] = { key1,key2,key3,key4,key5,key6 };
    return _Keyboard(modifier, keys, 6, true);
}

bool CFakerInputHelper::KeyboardUp(
    MODIFIER_KEY modifier,
    uint8_t key1,
    uint8_t key2,
    uint8_t key3,
    uint8_t key4,
    uint8_t key5,
    uint8_t key6
)
{
    uint8_t keys[6] = { key1,key2,key3,key4,key5,key6 };
    return _Keyboard(modifier, keys, 6, false);
}

bool CFakerInputHelper::Keyboard(
    MODIFIER_KEY modifier,
    std::vector<uint8_t> vkeys,
    bool fDown/* = true*/
)
{
    if (!(m_pClient && m_fConnect))
    {
        return false;
    }

    return _Keyboard(modifier, vkeys.data(), vkeys.size(), fDown);
}

bool CFakerInputHelper::MouseButton(
    MOUSE_BUTTON button,
    bool fDown,
    uint16_t x, 
    uint16_t y
)
{
    if (!(m_pClient && m_fConnect))
    {
        return false;
    }

    if (fDown)
    {
        m_MouseButton.Button |= button.Button;
        fakerinput_update_absolute_mouse(m_pClient, m_MouseButton.Button, x, y, 0, 0);
    }
    else
    {
        m_MouseButton.Button &= ~button.Button;
        fakerinput_update_absolute_mouse(m_pClient, m_MouseButton.Button, x, y, 0, 0);
    }

    return true;
}

bool CFakerInputHelper::_Keyboard(
    MODIFIER_KEY modifier,      // 修饰键
    const uint8_t* keys,        // 普通键
    size_t        count,        // 按键数量(0 - 6)
    bool fDown = true           // 是否按下
)
{
    if (!(m_pClient && m_fConnect))
    {
        return false;
    }

    if (count > 6)
    {
        count = 6;
    }

    if (fDown)
    {
        m_Modifier.Key |= modifier.Key;

        for (int i = 0; i < count; i++)
        {
            if (0 == keys[i])
            {
                continue;
            }

            // 查找是否已存在
            bool fExist = false;
            for (int j = 0; j < sizeof(m_Keys); j++)
            {
                if (keys[i] == m_Keys[j])
                {
                    fExist = true;
                    break;
                }
            }

            // 按键已经按下过了, 不处理
            if (fExist)
            {
                continue;
            }

            // 查找按键空位
            int nKeyIndex = 0;
            for (int j = 0; j < sizeof(m_Keys); j++)
            {
                if (0 == m_Keys[j])
                {
                    break;
                }
                nKeyIndex++;
            }

            // 插入新的按键
            if (nKeyIndex < sizeof(m_Keys))
            {
                m_Keys[nKeyIndex] = keys[i];
            }
        }
    }
    else
    {
        m_Modifier.Key &= ~modifier.Key;

        // 释放指定按键
        for (int i = 0; i < sizeof(m_Keys); i++)
        {
            // 查找未释放的按键
            if (0 == m_Keys[i])
            {
                continue;
            }

            //若未释放的按键与指定按键相同, 则释放掉
            for (int j = 0; j < count; j++)
            {
                if (keys[j] == m_Keys[i])
                {
                    m_Keys[i] = 0;       //清除按键状态
                }
            }
        }
    }

    return fakerinput_update_keyboard(m_pClient, m_Modifier.Key, m_Keys);
}

main.cpp

cpp 复制代码
#include <windows.h>
#include <locale.h>
#include "CFakerInputHelper.h"

int main()
{
	setlocale(LC_ALL, "");

	{
		CFakerInputHelper CKeyHelper;
		CKeyHelper.Initialize();
		CKeyHelper.KeyboardDown(MODIFIER_KEY(1, 0, 1, 0), eFakerInputKey::eKey_Delete);
		CKeyHelper.Uninitialize();
	}
	
    return 0;
}

		
相关推荐
yngsqq10 分钟前
029集——指定矩形范围内随机填充颜色半径不同的圆——vs CAD二次开发
windows
fhvyxyci10 分钟前
【C++之STL】一文学会使用 string
开发语言·数据结构·c++·stl
YxVoyager20 分钟前
【MFC编程(一)】MFC概述
c++·windows·mfc
编码追梦人32 分钟前
C++之数组和字符串
c++·算法
qwq_ovo_pwp34 分钟前
题解 洛谷 Luogu P1308 [NOIP2011 普及组] 统计单词数 C++
c++·洛谷
广州视觉芯软件有限公司1 小时前
MFC,DLL界面库设计注意
c++·人工智能·计算机视觉·mfc
doll ~CJ1 小时前
SQLite的BLOB数据类型与C++二进制存储学习记录
c++·sqlite·blob·图像数据存取·bitset标准库
这题怎么做?!?3 小时前
【Linux】网络编程:实现一个简易的基于HTTP协议格式、TCP传输的服务器,处理HTTP请求并返回HTTP响应;GET方法再理解
linux·服务器·c语言·网络·c++·tcp/ip·http
askah66447 小时前
mfc140u.dll丢失怎么办? mfc140u.dll文件缺失的修复技巧
windows·经验分享·游戏·microsoft·dll修复
Trouvaille ~8 小时前
【C++篇】在秩序与混沌的交响乐中: STL之map容器的哲学探寻
开发语言·数据结构·c++·算法·迭代器模式·stl·map