我正在制作一个检测蓝色的程序,根据屏幕上的坐标,鼠标会移动/点击。
检测工作正常,但是当我添加一个带有“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);
}
谢谢:)
答案 0 :(得分:0)
我找到了一种方法来解决那些有同样问题的人:)
我们可以代替使用Windows的虚拟键盘,从互联网安装VK,它不会被视为&#34;系统程序&#34; (如果我们这样说的话)。然后,我们只需要打开我们安装的新VK代码:
ShellExecute(NULL, "open", "VK.exe", NULL, NULL, SW_SHOWDEFAULT);
然后VK将在程序中打开,问题得到解决! :)