Muhammed Ajwahir
Muhammed Ajwahir

Reputation: 45

Display when key is pressed in opencv

There is a program which takes live feed from camera: I want to perform some operations when a key from keyboard is pressed without disturbing the on going process. I tried

if(waitKey(30) == '27')
  cout << "ESC pressed";

But this one doesn't work.

Upvotes: 1

Views: 9244

Answers (2)

Automaton2000
Automaton2000

Reputation: 360

cv::waitKey() returns an integer. You can either convert it to a char and then compare to the decimal ASCII code of any key like this

if((char)cv::waitKey(1) == 27) std::cout << "ESC pressed" << std::endl;

or (equivalently) write

if(cv::waitKey(1) % 256 == 27) std::cout << "ESC pressed" << std::endl;

Upvotes: 3

reza
reza

Reputation: 100

Documentation:
The function only works if there is at least one HighGUI window created and the window is active. If there are several HighGUI windows, any of them can be active.

As I understand from your question, It seems that you don't have any active windows. If that is the case, first show up an image with the help of imshow function and then wait for any key to be pressed.

Upvotes: 3

Related Questions