Ask Your Question

Does waitKey() work correctly in Linux?

asked 2014-10-20 14:47:43 -0500

st5000 gravatar image

updated 2014-10-20 14:53:12 -0500

Porting a OpenCV program from Windows to Linux and found that the value returned from waitKey() had to be "%256"'ed. Looked at the docs and OpenCV is of type integer: So, is the waitKey() method broken in Linux. Or is there a reasonable explanation as to why this method works differently between Windows and Linux.


edit retag flag offensive close merge delete


Yeah, the web interface appears to be buggy (for me at least). I couldn't get rid of the suggested posts which obscured the entire TEXT window. So it left me with posting a blank question (where did "strong text" come from) and editing it afterwards.

st5000 gravatar imagest5000 ( 2014-10-20 14:55:19 -0500 )edit

%255, btw. (or 0xff)

berak gravatar imageberak ( 2014-10-20 15:04:36 -0500 )edit

@berak: %0xff will give a value between 0 and 254. I'll think it should be %256.

unxnut gravatar imageunxnut ( 2014-10-20 15:25:48 -0500 )edit

aww, sorry, i indeed misread it.

if your os is returning scankeys instead of ascii, you'd want to mask the lsb, and this is: key & 0xff;

(modulo is the wrong answer anyway.)

berak gravatar imageberak ( 2014-10-20 16:15:09 -0500 )edit

I use this command a lot in Linux, and as far i know, it does return the ASCII code of the keys as integer, so you can do comparisons such as:

int key = cv::waitKey(0); if (key == 'a'){ ...}

It will return -1 if the specified time interval in the function call expires without any key being pressed This function works without any problems in Debian Squeeze and Wheezy, with Qt support.

R.Saracchini gravatar imageR.Saracchini ( 2014-10-20 17:03:23 -0500 )edit

I have two linux ubuntu systems. For some reason 1 system allows to use the function reading the asci value, the other one requires me to add & 0xff. No idea however why this happens.

StevenPuttemans gravatar imageStevenPuttemans ( 2014-10-21 06:03:45 -0500 )edit

Hmm, I never had a different behaviour either in CentOS or Debian, as far I never used OpenCV in Ubuntu for anything serious. It is noteworthy mention that it also gives keycodes that are not ASCII for directional keys, and it aborts your program with an Qt assertion (not OpenCV error) if you press any non-ascii key from Spanish/Brazilian/French keyboards (such as "ñ","ç"). I´m not sure if this happens if compiled without Qt support, thought.

R.Saracchini gravatar imageR.Saracchini ( 2014-10-21 07:57:04 -0500 )edit

@StevenPuttemans Can you check the locales of the two machines you have? If the locale is different, that may be the answer to this puzzle.

unxnut gravatar imageunxnut ( 2014-10-21 12:03:26 -0500 )edit

@unxnut that did the trick. Set the locale both to US_ENG fixed the non working ASCI returns.

StevenPuttemans gravatar imageStevenPuttemans ( 2014-10-22 04:54:19 -0500 )edit

1 answer

Sort by » oldest newest most voted

answered 2014-10-22 05:43:12 -0500

R.Saracchini gravatar image

updated 2014-10-22 05:55:18 -0500

I cant test it now, but at normal conditions, I set up all my machines with US_ENG locale. Maybe that´s why I never observed non-ascii characters return values besides arrow keys. I may try distinct locales in a ubuntu and Debian machine later.

edit flag offensive delete link more

Question Tools


Asked: 2014-10-20 14:47:43 -0500

Seen: 4,489 times

Last updated: Oct 20 '14