tags:

views:

34

answers:

2

Hi All. I have a problem here and need your help. I've been trying to capture keyboard strokes from a created window using cvWaitKey() function. The function works fine if I called the cvWaitKey from the same thread that created the window, but when I create the window from a thread and call cvWaitKey() from another thread it doesn't return correct key, it blocks for cvWaitKey(0) and returns -1 for any timeout grater than zero.

+1  A: 

Yes, this cannot work. cvWaitKey() is implemented by calling the PeekMessage() API function. That can only see messages on the message queue which is associated with the thread. The thread you created doesn't have any windows.

There is no obvious workaround for this, you have to call it on the thread that created the window. Calling GetAsyncKeyState() could work, a very different approach though.

Hans Passant
Thank you Hans for your answer. I'd be grateful if you explain in more details how to overcome such a situation using GetAsyncKeyState()
Mohammad Elwakeel
while (GetAsyncKeyState(VK_ESCAPE) >= 0) Sleep(50);
Hans Passant
A: 

In you "other" thread, you can set some shared variable to true or false (or some value), and make the "window thread" checks it before deciding whether to call cvWaitKey() in its local scope.

Adi