Saturday, November 16, 2024
Google search engine
HomeLanguagesPython OpenCV – waitKeyEx() Function

Python OpenCV – waitKeyEx() Function

Python OpenCv waitKeyEx() method is similar to waitKey() method but it also returns the full key code. The key code which is returned is implementation-specific and depends on the used backend: QT/GTK/Win32/etc.

Syntax: cv2.waitKey(delay)

Parameters:

  • delay: The time in milliseconds after which windows needs to destroyed. If given 0 it waits for infinite till any key is pressed to destroy window.

Return : This method return the full key code of the key which is pressed. If no key is pressed it return -1.

Example 1: 

In the below example we have implemented the waitKeyEx() method we have made a window that has an image named “gfg_logo.png” and then we display it and using waitKeyEx() method we delay the closing of the window and then press a key to close it. We store the returned value in the full_key_code variable and print it.

Python




# importing cv2 module
import cv2
  
# read the image
img = cv2.imread("gfg_logo.png")
  
# showing the image
cv2.imshow('gfg', img)
  
# waiting using waitKeyEX method and storing
# the returned value in full_key_code
full_key_code = cv2.waitKeyEx(0)
  
# printing the variable
print("The key code is:"+str(full_key_code))


Output:

The key code is:13

In the output, the value of full_key_code will be printed according to the key pressed When we press enter the value that is printed is as follows.

Example 2:

Another Example we can see is where we don’t press any key and wait for the window to destroy automatically after the delay that is given. We will pass 5000 as a parameter to wait for 5 seconds and then a window will close automatically without the need of pressing any key. In this case the function will return -1 as no key was pressed.

Python




# importing cv2 module
import cv2
  
# read the image
img = cv2.imread("gfg_logo.png")
  
# showing the image
cv2.imshow('gfg', img)
  
# waiting using waitKeyEX method and
# storing the returned value in full_key_code
full_key_code = cv2.waitKeyEx(5000)
  
# printing the variable
print("The key code is:"+str(full_key_code))


Output:

The key code is:-1

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments