Ask Your Question
2

cvWaitKey is not generating key codes for naviagation keys

asked 2013-05-23 08:02:00 -0600

Paramesh Reddy gravatar image

updated 2013-05-23 08:04:02 -0600

I have involved with opencv key function ie cvWaitKey(0).whenever i press the "r" key the point on opencv image window is moving right direction, for "L" towards left ,for "u" upward direction as bellow.

        while(true)
        {
            int key = cvWaitKey(0);
            cout<<"key="<<key<<endl;
            if (key == 'u'){
                y -= 1;

            }else if(key=='d'){
                y += 1;

            }else if(key=='l'){
                x -= 1;

            }else if(key=='r'){
                x+= 1;

            }
            showImage();
        }

yes it working fine.

My problem is with arrow keys the function cvWaitKey(0) is not working. Does Opencv provide a another function? if not can anyone give me suggestion about how to do that?

Thanking You.

edit retag flag offensive close merge delete

3 answers

Sort by ยป oldest newest most voted
2

answered 2017-08-13 06:04:54 -0600

NadavB gravatar image

The solution is simple, yet undocumented properly.

Just use waitKeyEx(1)

edit flag offensive delete link more
1

answered 2014-02-04 13:44:23 -0600

david laine gravatar image

My experience leads me to believe that cvWaitKey only picks up keys during the wait period and misses some or all keys pressed outside of the wait.

On the question of arrow keys I suggest you write a little piece of code to print out what cvWaitKey actually sees when you press the appropriate keys. I get 82 and 84 (decimal) for the up and down arrows but these values may be different on some other systems.

regards

David Laine

edit flag offensive delete link more

Comments

Actually ignore the answer below, the keys actually do work, in stead of accessing them by their char value, just access them by their decimal value like suggested above. Change the code to something like

 int key = waitKey(0);
 if (key == 82) { ... go up ...}

This actuall works perfectly on my system. However, the above author makes the mistake that he thinks the wait has something to do with it. Using a 0 makes sure you will capture any stroke... it waits infinitly.

StevenPuttemans gravatar imageStevenPuttemans ( 2014-02-05 03:08:00 -0600 )edit

That value depends from architecture of your system. For example, for me "up_key" equal (int) 2490368.

Flart gravatar imageFlart ( 2014-05-21 09:01:01 -0600 )edit

@Flart, they should be ASCII codes, which are universal for architectures. Sure you aren't doing anything wrong?

StevenPuttemans gravatar imageStevenPuttemans ( 2014-05-22 04:19:19 -0600 )edit
    int c = cv::waitKey(0);
    if (c == 2490368) y++; // up
    if (c == 2621440) y--;   // down
    if (c == 2424832) x--;   //  left
    if (c == 2555904)  x++;  // right

this is work for me. (dec)82 corresponds 'R'. http://en.wikipedia.org/wiki/ASCII May be that some locale issue? (which depend from system, too. Win, linux, etc.)

Flart gravatar imageFlart ( 2014-05-22 08:16:05 -0600 )edit
-1

answered 2013-05-23 08:32:24 -0600

unxnut gravatar image

Arrow keys provide a sequence of characters instead of a single character such as what you are using. So, arrow keys won't work on this.

edit flag offensive delete link more

Question Tools

Stats

Asked: 2013-05-23 08:02:00 -0600

Seen: 16,914 times

Last updated: Aug 13 '17