# How to use waitkey with VideoCapture()

**URL:** https://forum.opencv.org/t/how-to-use-waitkey-with-videocapture/10718
**Category:** Python
**Created:** [October 28, 2022, 4:14am UTC](https://forum.opencv.org/t/how-to-use-waitkey-with-videocapture/10718 "2022-10-28T04:14:13Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Maia1003](https://avatars.discourse-cdn.com/v4/letter/m/ecc23a/32.png) [@Maia1003](https://forum.opencv.org/u/Maia1003)
#### Post date: [October 28, 2022, 4:14am UTC](https://forum.opencv.org/t/how-to-use-waitkey-with-videocapture/10718/1 "2022-10-28T04:14:13Z")

</div>

Hi,  
I am struggling about this example. I’d like to update the frame window wrt key press, e.g. display grayscale image when I press ‘c’ on the keyboard, but this does not work:  
My codes:

```auto
import numpy as np
import cv2 
cap = cv2.VideoCapture(0)
if not cap.isOpened():
    print("Cannot open camera")
    exit()
while True:
    # Capture frame-by-frame
    ret, frame = cap.read()
    # if frame is read correctly ret is True
    if not ret:
        print("Can't receive frame (stream end?). Exiting ...")
        break
    # Our operations on the frame come here
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    # Display the resulting frame
    cv2.imshow('frame', frame)
    key = 0xFF & cv2.waitKey(1)
    if key == ord('q'):
        break
    elif key == ord('c'):
        print('pressed c')
        cv2.imshow('frame', gray)
        
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()

```

---

<div class="post-metadata">

### Author: ![sturkmen](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/sturkmen/32/13_2.png) [@sturkmen](https://forum.opencv.org/u/sturkmen)
#### Post date: [October 28, 2022, 12:29pm UTC](https://forum.opencv.org/t/how-to-use-waitkey-with-videocapture/10718/2 "2022-10-28T12:29:19Z")

</div>

Your question related to general programming skill  
Try the following code.

```auto
import numpy as np
import cv2 
cap = cv2.VideoCapture(0)
if not cap.isOpened():
    print("Cannot open camera")
    exit()

convert_to_gray = False
while True:
    # Capture frame-by-frame
    ret, frame = cap.read()
    # if frame is read correctly ret is True
    if not ret:
        print("Can't receive frame (stream end?). Exiting ...")
        break
    # Our operations on the frame come here
    if convert_to_gray:
        frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    # Display the resulting frame
    cv2.imshow('frame', frame)
    key = 0xFF & cv2.waitKey(1)
    if key == ord('q'):
        break
    elif key == ord('c'):
        convert_to_gray = not convert_to_gray # Toggle convert_to_gray value
        
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()

```

---

<div class="post-metadata">

### Author: ![Maia1003](https://avatars.discourse-cdn.com/v4/letter/m/ecc23a/32.png) [@Maia1003](https://forum.opencv.org/u/Maia1003)
#### Post date: [October 28, 2022, 1:02pm UTC](https://forum.opencv.org/t/how-to-use-waitkey-with-videocapture/10718/3 "2022-10-28T13:02:53Z")

</div>

Thank you.  
I found that I had issues while using several waitkeys for the same window.  
Best
