# Use setMouseCallBack in class and show image in a loop

**URL:** <https://forum.opencv.org/t/use-setmousecallback-in-class-and-show-image-in-a-loop/3570>\
**Category:** Uncategorized\
**Tags:** highgui\
**Created:** [May 28, 2021, 8:54am UTC](https://forum.opencv.org/t/use-setmousecallback-in-class-and-show-image-in-a-loop/3570 "2021-05-28T08:54:32Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![MakeItGreatAgain](https://avatars.discourse-cdn.com/v4/letter/m/b19c9b/32.png) [@MakeItGreatAgain](https://forum.opencv.org/u/MakeItGreatAgain)\
**Post date:** [May 28, 2021, 8:54am UTC](https://forum.opencv.org/t/use-setmousecallback-in-class-and-show-image-in-a-loop/3570/1 "2021-05-28T08:54:32Z")

</div>

Hello,

> <https://stackoverflow.com/questions/25748404/how-to-use-cvsetmousecallback-in-class>

The link is the way I use setMouseCallBack in my class. Before this, I show only images without problem. Showing image no longer work after I added waitKey(0) into the code, see the one behind setMouseCallBack. Mouse action was not captured without using this waitkey(0).

```auto
	cv::namedWindow("L");
	cv::setMouseCallback("L", onMouse, this);
	cv::waitKey(0);
	for (size_t i = 0; i < corners.size(); i++)
	{
		circle(copy, corners[i], radius, cv::Scalar(moRNG.uniform(0, 255), moRNG.uniform(0, 256), moRNG.uniform(0, 256)), cv::FILLED);
		cv::imshow("L", copy);
		cv::waitKey(1);
	} // end for

```

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [May 28, 2021, 3:20pm UTC](https://forum.opencv.org/t/use-setmousecallback-in-class-and-show-image-in-a-loop/3570/2 "2021-05-28T15:20:36Z")

</div>

imshow windows **only** work while `waitKey` is executing.

`waitKey(0)` waits (forever) until a key is pressed.

`waitKey(1)` waits at most a millisecond or so, then it’s done and returning.

there **must not** be any `waitKey` calls inside of event handlers. they don’t belong there. they belong in a single thread, the main thread (other threads sometimes allowed, but always exactly one).

please post _complete_ but _minimal_ code to reproduce your issue. the code you posted here lacks the mouse callback function. the code on stackoverflow contains a bunch of stuff that is clearly irrelevant to the issue, hence a distraction.

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [May 29, 2021, 8:25am UTC](https://forum.opencv.org/t/use-setmousecallback-in-class-and-show-image-in-a-loop/3570/3 "2021-05-29T08:25:38Z")

</div>

2 posts were split to a new topic: [waitKey without delay](https://forum.opencv.org/t/waitkey-without-delay/3582)

---

<div class="post-metadata">

**Author:** ![MakeItGreatAgain](https://avatars.discourse-cdn.com/v4/letter/m/b19c9b/32.png) [@MakeItGreatAgain](https://forum.opencv.org/u/MakeItGreatAgain)\
**Post date:** [May 31, 2021, 4:20am UTC](https://forum.opencv.org/t/use-setmousecallback-in-class-and-show-image-in-a-loop/3570/4 "2021-05-31T04:20:47Z")

</div>

Hello, @crackwitz .  
The codes in my post is the main part of a member function of my class. The name could be classA::init, for example.

In main function, we simply declare an object of classA and call init() with the object.

---

<div class="post-metadata">

**Author:** ![MakeItGreatAgain](https://avatars.discourse-cdn.com/v4/letter/m/b19c9b/32.png) [@MakeItGreatAgain](https://forum.opencv.org/u/MakeItGreatAgain)\
**Post date:** [June 7, 2021, 6:07am UTC](https://forum.opencv.org/t/use-setmousecallback-in-class-and-show-image-in-a-loop/3570/5 "2021-06-07T06:07:33Z")

</div>

solved with replacing waitkey(0) by waitkey(1)
