我正在使用 SendInput() 发送鼠标的相对位置。首先生病告诉你在做什么。
我用手指移动鼠标。因此,首先我在 640x480 图像中跟踪我的手指并获取图像中像素的绝对位置。
然后我将该位置发送到以下方法,以使用发送输入生成相对鼠标位置命令。
当手指移动到左边界 (xlim1) 或右边界 (xlim2) 时,光标会根据哪个限制水平向左或向右滚动。问题是当我运行代码时,就在光标开始移动时,屏幕变黑。
当我评论其他部分 if(cx >= prevX && cx > xlim2){ .... } 部分时,它可以工作.. (所以当手指指向图像的右边界时,它的光标会一直水平滚动到对。注释部分启用左侧水平滚动)。
如果这是我们第一次捕获手指,则 bool first 变量将为真。否则为假。
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;
}
}