c++ - 使用系统命令时 SendInput 不起作用

标签 c++ opencv keyboard mouseevent sendinput

我正在制作一个检测蓝色的程序,根据它在屏幕上的坐标,鼠标会移动/点击。

检测工作正常,但是当我添加带有“system("osk")”行的虚拟键盘时,我遇到了问题。

当事件窗口为虚拟键盘时,程序停止在后台运行。我的意思是程序正在运行,但鼠标没有移动或点击。当事件窗口不是虚拟键盘时,一切正常。

你能帮我看看我的代码有什么问题吗?

这是移动鼠标的代码:

void MouseMove(int x, int y)
{
    double fScreenWidth = ::GetSystemMetrics(SM_CXSCREEN) - 1;
    double fScreenHeight = ::GetSystemMetrics(SM_CYSCREEN) - 1;
    double fx = 2 * x*(65535.0f / (fScreenWidth - 200));
    double fy = 2 * y*(65535.0f / fScreenHeight);
    INPUT Input = { 0 };
    Input.type = INPUT_MOUSE;
    Input.mi.dwFlags = MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE;
    Input.mi.dx = (LONG)fx;
    Input.mi.dy = (LONG)fy;
    ::SendInput(1, &Input, sizeof(INPUT));
}

这是命令 System("osk") 所在的主要函数:

int main()
{
VideoCapture cap(0);
if (!cap.isOpened())
    return -1;

system("osk"); // <-----------------------------------------
while (true)
{
    int xMouse = 0, yMouse = 0;
    float totalX = 0.0, totalY = 0.0;

    Mat frame, frame2;
    cap >> frame;
    cvtColor(frame, frame2, COLOR_BGR2HSV);
    Mat hsvImg = frame2;

    inRange(hsvImg, Scalar(78, 241, 59), Scalar(255, 255, 255), hsvImg); // Blue


    vector<vector<Point>> contours;
    vector<Vec4i> hierarchy;
    erode(hsvImg, hsvImg, getStructuringElement(MORPH_ELLIPSE, Size(5, 5)));
    dilate(hsvImg, hsvImg, getStructuringElement(MORPH_ELLIPSE, Size(5, 5)));
    dilate(hsvImg, hsvImg, getStructuringElement(MORPH_ELLIPSE, Size(5, 5)));
    erode(hsvImg, hsvImg, getStructuringElement(MORPH_ELLIPSE, Size(5, 5)));

    Mat drawing = Mat::zeros(frame.size(), CV_8UC3);

    flip(drawing, drawing, 1);
    flip(frame, frame, 1);
    flip(hsvImg, hsvImg, 1);

    RNG rng(12345);
    findContours(hsvImg, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0));

    if (contours.size() > 0)
    {
        float sumX = 0.0, sumY = 0.0;

        for (unsigned int i = 0; i < contours.size(); i++)
        {
            for (unsigned int j = 0; j < contours[i].size(); j++)
            {
                totalX += contours[i][j].x;
                sumX++;
                totalY += contours[i][j].y;
                sumY++;
            }
        }


        if (contours.size() == 2)
        {
            LeftClick();
            cout << contours.size() << endl << endl;
        }
        else if (contours.size() == 1)
        {
            xMouse = (int)(totalX / sumX);
            yMouse = (int)(totalY / sumY);
            MouseMove(xMouse, yMouse);
        }
        else{
            cout << "Too many Contours" << endl << endl;
        }

        for (unsigned int i = 0; i < contours.size(); i++)
        {
            Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255));
            drawContours(drawing, contours, i, color, 2, 8, hierarchy, 0, Point2f(xMouse, yMouse));
        }


    }

    imshow("Drawing", drawing);
    imshow("Original", frame);

    if (waitKey(30) == 'q') //Wait 30 milisec. If user pressed ‘q’ break the loop.
        break;
}
waitKey(0);
}

谢谢:)

最佳答案

我找到了一种方法来为那些有同样问题的人解决这个问题:)

我们可以不使用 Windows 的虚拟键盘,而是从互联网上安装一个 VK,它不会被视为“系统程序”(如果我们这样说的话)。然后,我们只需要打开我们用代码安装的新 VK:

ShellExecute(NULL, "open", "VK.exe", NULL, NULL, SW_SHOWDEFAULT);

然后 VK 将在程序中打开,问题就解决了! :)

关于c++ - 使用系统命令时 SendInput 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42903016/

相关文章:

c# - 在字符串输出中显示 ""

c++ - 我在 C++ 中创建阻塞队列 vector 时遇到问题

c++ - move 和模板化构造函数,取错了

python - 如何在opencv python中调整PNG图像的大小?

opencv - 使用 opencv 构建自定义 svm 内核矩阵

iOS 7 键盘预测

ios - 防止键盘隐藏 UITableView 底部的 UITextField

c++ - 数据结构是否适合放置 shared_ptr?

JavaCV 示例/教程

keyboard - 是否有一种编程语言使用(非扩展)ascii 表中的字符以外的字符?