Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenCv Restrict cv::waitKey to only one wait for one specific key?

Tags:

c++

opencv

Is there a way to restrict cv::WaitKey() to only wait for one key press? ie the esc button? I want to be able to press any key aside from this target key and have the window remain open.

like image 934
Michael Rickert Avatar asked Oct 04 '22 22:10

Michael Rickert


1 Answers

I just stumbled upon this question and I'm pretty sure there will be more people looking for the same answer. There is in fact a pretty easy way to do this. cv::waitKey() returns an integer that corresponds to the keycode of the pressed key. By putting the waitKey call in a loop that compares the return value to the keycode you're looking for, you can wait for a specific key.

There is a pretty big pitfall here though: on some platforms, the most significant bit is set in the return value, meaning the loop will never break if you just compare them to the normal keycodes. Get around this by using a bitwise AND with everything but the most significant bit like so:

while((cv::waitKey() & 0xEFFFFF) != 27); //27 is the keycode for ESC
like image 141
Louis Avatar answered Oct 13 '22 10:10

Louis