Sorry, this content is no longer available

Ask Your Question
2

cvWaitKey is not generating key codes for naviagation keys

asked May 23 '13

Paramesh Reddy gravatar image

updated May 23 '13

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.

Preview: (hide)

3 answers

Sort by » oldest newest most voted
2

answered Aug 13 '17

NadavB gravatar image

The solution is simple, yet undocumented properly.

Just use waitKeyEx(1)

Preview: (hide)
1

answered Feb 4 '14

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

Preview: (hide)

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 (Feb 5 '14)edit

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

Flart gravatar imageFlart (May 21 '14)edit

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

StevenPuttemans gravatar imageStevenPuttemans (May 22 '14)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 (May 22 '14)edit
-1

answered May 23 '13

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.

Preview: (hide)

Question Tools

Stats

Asked: May 23 '13

Seen: 18,564 times

Last updated: Aug 13 '17