匯東網


使用键盘控制鼠标的程序

[編輯] [转简体]
|
作者:huidong | 分類:【編程】雜項
[ 20 瀏覽 0 評論 7 贊 6 踩 ]

概要

正文

临时写的,需要用。


程序(50kb):

链接: https://pan.baidu.com/s/1qi0wZOrotiEwSwFeKOcJ-w  提取码: 7chk


代码

#include <stdio.h>
#include <Windows.h>

#define KEY_DOWN(VK_NONAME) ((GetAsyncKeyState(VK_NONAME) & 0x8000) ? 1:0)

bool bOn = false;
POINT pOldMouse = { 0,0 };
int nDPI = 2;

// 记录鼠标左右键是否被按下
bool bLeft = false;
bool bRight = false;

int main()
{
    printf("kbCtrlMouse  使用键盘控制鼠标 hd[2021.7.25]\n");
    printf("按下 Ctrl + Alt + B 启用键盘控制鼠标,再按一次关闭\n");
    printf("启用键盘控制鼠标后:\n");
    printf("使用 WSAD 或 方向键 控制鼠标光标移动\n");
    printf("使用 J 键模拟鼠标左键,使用 K 键模拟鼠标右键\n");
    printf(" [ 和 ] 键分别可以调小或调大每次鼠标移动的距离(DPI)\n\n");
    printf("--- 事件记录 ---\n");

    while (true)
    {
        if (KEY_DOWN(VK_CONTROL) && KEY_DOWN(VK_MENU) && KEY_DOWN('B'))
        {
            bOn = !bOn;
            if (bOn)
            {
                printf("开启了键盘控制。\n");
                GetCursorPos(&pOldMouse);
            }
            else
            {
                printf("关闭了键盘控制。\n");
            }

            Sleep(300);
        }

        if (bOn)
        {
            // 方向键
            if (KEY_DOWN('W') || KEY_DOWN(VK_UP))
            {
                pOldMouse.y -= nDPI;
                SetCursorPos(pOldMouse.x, pOldMouse.y);
                printf("键盘控制鼠标上移 up move\n");
            }
            if (KEY_DOWN('S') || KEY_DOWN(VK_DOWN))
            {
                pOldMouse.y += nDPI;
                SetCursorPos(pOldMouse.x, pOldMouse.y);
                printf("键盘控制鼠标下移 down move\n");
            }
            if (KEY_DOWN('A') || KEY_DOWN(VK_LEFT))
            {
                pOldMouse.x -= nDPI;
                SetCursorPos(pOldMouse.x, pOldMouse.y);
                printf("键盘控制鼠标左移 left move\n");
            }
            if (KEY_DOWN('D') || KEY_DOWN(VK_RIGHT))
            {
                pOldMouse.x += nDPI;
                SetCursorPos(pOldMouse.x, pOldMouse.y);
                printf("键盘控制鼠标右移 right move\n");
            }

            // 左键
            if (KEY_DOWN('J'))
            {
                bLeft = true;
                mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
                printf("键盘控制鼠标左键按下 Left down\n");
            }
            else if (bLeft)
            {
                // 松开鼠标
                bLeft = false;
                mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
                printf("键盘控制鼠标左键松开 Left up\n");
            }

            // 右键
            if (KEY_DOWN('K'))
            {
                bRight = true;
                mouse_event(MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, 0);
                printf("键盘控制鼠标右键按下 Right down\n");
            }
            else if (bRight)
            {
                // 松开鼠标
                bRight = false;
                mouse_event(MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0);
                printf("键盘控制鼠标右键松开 Right up\n");
            }

            // [ 键
            if (KEY_DOWN(VK_OEM_4))
            {
                if (nDPI - 1 >= 1)
                {
                    nDPI--;
                    printf("DPI 减小为 %d\n", nDPI);
                }
                else
                {
                    printf("DPI 为 1,无法再减小了\n");
                }

                Sleep(200);
            }

            // ] 键
            if (KEY_DOWN(VK_OEM_6))
            {
                if (nDPI + 1 <=200)
                {
                    nDPI++;
                    printf("DPI 增大为 %d\n", nDPI);
                }
                else
                {
                    printf("DPI 为 200,无法再增大了\n");
                }

                Sleep(200);
            }
        }

        Sleep(50);
    }

    return 0;
}


[ 7] [ 6]


 評論區  0 條評論

+ 添加評論