当我使用SendInput发送鼠标光标位置时,屏幕变黑

use*_*323 8 c++ winapi sendinput

我正在使用SendInput()发送鼠标的相对位置.先生病了,你在做什么.

我用手指移动鼠标.因此,首先我在640x480图像中跟踪手指,并获得图像中的像素绝对位置.

然后我将该位置发送到以下方法,以使用发送输入生成相对鼠标位置命令.

当手指移动到左边界(xlim1)或右边界(xlim2)时,光标会根据哪个限制水平向左或向右滚动.问题是当我运行代码时,当光标开始移动时,屏幕变为黑色.

当我评论其他部分if(cx> = prevX && cx> xlim2){....}部分时,它的工作原理..(所以当手指点到图像的右边界时,光标一直水平滚动到右.评论部分启用左水平滚动).

bool first变量将为true,如果这是第一次,我们捕获手指.否则就是假的.

void movMouse(int cx, int cy, bool first){
static int prevX = 0;
static int prevY = 0;

static int leftPrevX;
static int rightPrevX;

int mx,my;

if(first == true){
    prevX = cx;
    prevY = cy;
}
else{
    mx = (cx - prevX);
    my = (cy - prevY);

    if(cx <= prevX && cx < xlim1){
        mx = -20;

        INPUT input;
        input.type          = INPUT_MOUSE;
        input.mi.mouseData  = 0;
        input.mi.dx         = -(mx);
        input.mi.dy         =  (my);

        input.mi.dwFlags    =  MOUSEEVENTF_MOVE;

        SendInput(1, &input, sizeof(input));
    }
    else if(cx >= prevX && cx > xlim2){
        mx = 20;

        INPUT input;
        input.type          = INPUT_MOUSE;
        input.mi.mouseData  = 0;
        input.mi.dx         = -(mx);
        input.mi.dy         =  (my);

        input.mi.dwFlags    =  MOUSEEVENTF_MOVE;

        SendInput(1, &input, sizeof(input));
    }
    else {
        INPUT input;
        input.type          = INPUT_MOUSE;
        input.mi.mouseData  = 0;
        input.mi.dx         = -(mx);
        input.mi.dy         =  (my);

        input.mi.dwFlags    =  MOUSEEVENTF_MOVE;

        SendInput(1, &input, sizeof(input));
    }

    prevX = cx;
    prevY = cy;
}
Run Code Online (Sandbox Code Playgroud)

}

max*_*mus 6

尝试

ZeroMemory(&input,sizeof(input));
Run Code Online (Sandbox Code Playgroud)

也初始化所有变量,包括input.time它为我工作:)