In OpenCV, it seems a double-click action also triggers a single-click event. Here is a sample code. The single_click() is always called before double_click().
Is it possible to trigger double_click() without triggering single_click() first?
Thanks!
void double_click() {
std::cout << "Double click.\n";
}
void thisMouseCallBack(int event, int x, int y, int flags, void *param) {
if (event == cv::EVENT_LBUTTONDOWN) {
single_click();
}
if (event == cv::EVENT_LBUTTONDBLCLK) {
double_click();
}
}
int main() {
cv::Mat testImg(100, 500, CV_8UC3);
cv::namedWindow("thisWindow");
cv::setMouseCallback("thisWindow", thisMouseCallBack, NULL);
cv::imshow("thisWindow", testImg);
cv::waitKey(-1);
return 0;
}
To distinguish double and single clicks (ie fire only the double-click event on double click, and skip the single click event), the standard way to do it uses a timer.
Start a timer (~100-200ms) on the first click, but do not call
single_click
. If the timer finishes before another click is received, callsingle_click
. But if another click is received before the timer finishes, cancel the timer and calldouble_click
.However, as Rob Kennedy points out above, this will cause a small delay, so be careful about objects where you want to distinguish between single and double click. In most GUIs, single click is a select operation and double-click is an 'open' or 'execute' operation, so it makes sense to have to select an object before activating it.