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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With