3

I'm very new to opencv, and I've been trying to get user input to exit out of all my open windows and escape the while loop. I've tried a bunch of different conditions, and checked that my Mac is giving keyboard permissions to python, but for some reason the waitKey function doesn't seem to be registering when I input anything into the keyboard. Any suggestions on what I should do ?

while True:
    screenshot = py.screenshot()
    screenshot = np.array(screenshot)
    screenshot = cv.cvtColor(screenshot, cv.COLOR_BGR2RGB)

    cv.imshow('Computer Vision', screenshot)

    if cv.waitKey(1) == ord('q'):
        print("shit")
        cv.destroyAllWindows()
        break
3
  • Does the window that OpenCV creates have focus when you're pressing the key? Commented Jun 18, 2021 at 15:51
  • yeah it does, but it doesn't seem to update even when its selected and im pressing a key. Commented Jun 18, 2021 at 15:54
  • OK. edit the question and add the version of OpenCV that this is occurring with, and make your example a proper minimal reproducible example to allow others that use Mac to easily reproduce it. Commented Jun 18, 2021 at 18:09

1 Answer 1

1

Try cv2.waitKey(1) & 0xFF == ord('q'): as 0xFF can be necessary on certain OSs.

You may also try inspecting the values of cv.waitKey(1) and ord('q') to see how they differ.

Sign up to request clarification or add additional context in comments.

9 Comments

im trying to get a constant screenshots displayed, and when I use waitKey(0) it won't continuously show me the screenshots, just a singular one
Apologies, i see. Perhaps try if cv2.waitKey(1) & 0xFF == ord('q'):. It appears 0xFF can be necessary on certain OSs. Refer to stackoverflow.com/questions/53357877/usage-of-ordq-and-0xff
You may also try inspecting the values of cv.waitKey(1) and ord('q') to see how they differ.
right, so I tried the & 0xFF thing didn't seem to make a difference, and then the values of waitKey and ord('q), are -1 and 113 respectively. I just can't get the wait key to return any value other than -1
it returns -1 upon timeout (1 means 1 millisecond of timeout). call waitKey(10000) so you have 10 seconds to press a key.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.