Ich mache ein Programm, das eine blaue Farbe erkennt, und entsprechend den Koordinaten auf dem Bildschirm bewegt sich die Maus.SendInput bei Verwendung eines Systembefehls funktioniert nicht
Die Erkennung funktioniert gut, aber wenn ich eine virtuelle Tastatur mit der Zeile "system (" osk ")" hinzufügen, habe ich ein Problem festgestellt.
Wenn das aktive Fenster die virtuelle Tastatur war, hat das Programm nicht mehr im Hintergrund ausgeführt. Ich meine, dass das Programm läuft, aber die Maus bewegt sich nicht oder klickt. Und wenn das aktive Fenster nicht die virtuelle Tastatur ist, funktioniert alles gut.
Können Sie mir helfen zu wissen, was in meinem Code falsch ist?
Dies ist der Code, der die Maus bewegt:
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));
}
Und das ist die Hauptfunktion, wo der Befehl System ("osk") befindet:
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);
}
Thanks :)