[Python] OpenCV cv2.waitKey() cannot detect the Delete key, what should I do?

Discussão em 'Python' iniciado por Stack, Março 24, 2025.

  1. Stack

    Stack Membro Participativo

    I am using cv2.waitKey(1) & 0xFF to capture key events, but I found that it cannot correctly detect the Delete key. My current code is as follows:

    import cv2

    while True:
    key = cv2.waitKey(1)
    if key != -1: # -1 : timeout/no key event
    print(f"Key pressed: {key}")

    if key == 127:
    print("Delete key detected!")
    break

    cv2.destroyAllWindows()


    However, when I run this code and press the Delete key, it is not detected.

    Environment: Windows 10, Python 3.9, OpenCV 4.5.5

    Issue: key == 127 does not work

    Expected Behavior: When I press Delete, it should print Delete key detected.

    Questions:


      1. Can cv2.waitKey(1) correctly detect the Delete key?


      2. Is there a more accurate way to detect the Delete key?

    Continue reading...

Compartilhe esta Página