Sunday, December 29, 2024
Google search engine
HomeLanguagesHow to detect if a specific key pressed using Python?

How to detect if a specific key pressed using Python?

In this article, we will learn how can we detect if a specific key is pressed by the user or not. The whole Module is divided into 3 segments, The 1st segment deal with simple integers, 2nd Alphanumerical characters and In 3rd we will use a python module to detect a key.

Using Keyboard Module to detect if a specific key pressed

Here we are importing the keyboard Module and with the help of read_key() method checking what key is pressed.

Python3




import keyboard
 
while True:
   
    print(keyboard.read_key())
    if keyboard.read_key() == "a":
        break


Output:

 

Using pynput to detect if a specific key pressed

In this method, we will use pynput Python module to detecting any key press. “pynput.keyboard” contains classes for controlling and monitoring the keyboard. It Calls pynput.keyboard.Listener. stop from anywhere, or return False from a callback to stop the listener. This library allows you to control and monitor input devices.

Approach:

  • Import key, Listener from pynput.keyboard
  • Create a with Statement: The with statement is used to wrap the execution of a block with methods defined by a context manager.
  • Define functions

For installation run this code into your terminal.

pip install pynput

Example 1: Here you will see which key is being pressed.

Python3




from pynput.keyboard import Key, Listener
 
def show(key):
 
    print('\nYou Entered {0}'.format( key))
 
    if key == Key.delete:
        # Stop listener
        return False
 
# Collect all event until released
with Listener(on_press = show) as listener:  
    listener.join()


Output:

Example 2: Here you can detect a specific key is being been pressed or not.

Python3




from pynput.keyboard import Key, Listener
 
def show(key):
   
    if key == Key.tab:
        print("good")
         
    if key != Key.tab:
        print("try again")
         
    # by pressing 'delete' button
    # you can terminate the loop
    if key == Key.delete:
        return False
 
# Collect all event until released
with Listener(on_press = show) as listener:
    listener.join()


Output:

Checking if  a specific key is being been pressed or not

Approach:

  • Take user input
  • Create a loop
  • Use condition
  • Print output

Here we are taking an input from a user and detecting that the user has entered the specified Alphanumerical(characters representing the numbers 0 – 9, the letters A – Z (both uppercase and lowercase), and some common symbols such as @ # * and &.) or not.

Python3




for _ in range(3):
 
    user_input = input(" Please enter your lucky word or type 'END' to terminate loop: ")
     
    if user_input == "geek":
        print("You are really a geek")
        break
 
    elif user_input == "END":
        break
 
    else:
        print("Try Again")


Output:

RELATED ARTICLES

Most Popular

Recent Comments