问题描述
我想处理箭头键.但是当我打印出waitKey()函数的输入值时,它是0.我不知道为什么我尝试将"int"更改为"char",但这是行不通的.我该如何解决这个问题.
I want to handling arrow keys. but when I print out the input value for waitKey() function, It's 0.I don't know why.I try to change from "int" to "char" , but It doesn't work.How can I solve this problem.
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;
}
推荐答案
请改用 waitKeyEx()
函数.如文档所述:
Use waitKeyEx()
function instead. As the documentation says:
密钥代码是特定于实现的,并且取决于所使用的后端:QT/GTK/Win32
Key code is implementation specific and depends on used backend: QT/GTK/Win32
在我的系统上,它给出:左:2424832最多:2490368右:2555904向下:2621440
On my system it gives:Left: 2424832Up: 2490368Right: 2555904Down: 2621440
尽管有许多在线消息说 waitKey()
可使用箭头,但它在Windows系统上也未返回正确的键码(始终返回0).猜猜这也是特定于实现的.也许是因为 waitKey()
返回ASCII码,但是箭头键没有它们(如).
Although there are many online sources saying waitKey()
works with arrows, it didn't return proper key codes on my Windows system either (always returned 0). Guess that is also implementation specific. Maybe because waitKey()
returns ASCII-codes, but arrow keys don't have them (as explained here).
这篇关于opencv使用waitKey()函数处理箭头键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!