2017-08-09 91 views
1

我想處理箭頭鍵。但是當我打印出waitKey()函數的輸入值時,它是0. 我不知道爲什麼。 我嘗試從「int」更改爲「char」,但它不起作用。 我該如何解決這個問題。opencv處理與waitKey()函數的箭頭鍵

int pos = 100; 
imshow("image", image); 
onChange(pos, (void *)&image); 
createTrackbar("threshold", "image", &pos, 255, onChange, (void*)&image); 
while (1) { 
    int Key = waitKey(); 
    cout << Key << endl; 
    if (Key == 27) break; 
    if (Key == 2490368) { 
     pos--; 
     onChange(pos, (void *)&image); 
    } 
    if(Key == 2621440){ 
     pos++; 
     onChange(pos, (void *)&image); 
    } 
    if (pos < 0 || pos > 255) pos = 0; 
} 

回答

2

代替使用waitKeyEx()函數。正如文檔所述:

與waitKey()類似,但返回完整的密鑰代碼。

鍵碼爲特定實現,取決於所使用的後端: QT/GTK/Win32的

在我的系統它提供: 左:2424832 最多:2490368 右:2555904 下:2621440

儘管有很多在線消息來源說waitKey()使用箭頭,但它並沒有在我的Windows系統上返回正確的密鑰代碼(總是返回0)。猜猜這也是實現特定的。也許因爲waitKey()返回ASCII碼,但箭頭鍵沒有它們(如解釋here)。

+0

哇........................超級超級謝謝。這是非常有用的信息。謝謝! – Amily

1

請注意,這取決於版本,在Windows 3.0上使用waitKey()給出了完整的密鑰代碼,但是當我更改爲3.3時,它突然返回0作爲方向鍵。

0

下面是我的臨時解決方法

#ifndef _WIN32 
int myWaitKey(int wait) { 
    int c = cv::waitKey(wait); 
    return c; 
} 
#pragma message("linux..") 
#else 
#include <conio.h> // to support _getch 
int myWaitKey(int wait) { 
    int c = cvWaitKey(wait); 
    if (c == 0) { 
     int c = _getch(); //capture the key code and insert into c 
     if (c == 0 || c == 224) 
      c = _getch(); 
    } 
    //if (c!=-1) printf("%d\n",c); 
    return c; 
} 
#endif