Detect keypress in Python

Detect keypress in Python

Python allows us to work with user input in its programs. We can also work with hardware devices in Python.

In this article, we will discuss how to detect keypress in Python.

Using the keyboard module to detect keypress in Python

The keyboard module is well equipped with different functions to perform operations related to keyboard input, detecting-simulating key presses, and more. This module works normally on Windows but requires the device to be rooted on Linux devices. To detect keypress, we can use a few functions from this module.

The read_key() function from this module is used to read the key pressed by the user. We can check whether the pressed key matches our specified key.

See the following example.

Output:

A Key Pressed

If you get output as below:

ModuleNotFoundError: No module named ‘keyboard’

You need to install keyboard module first.
You can use following command to install keyboard module.

pip install keyboard

The wait() function waits for the user to press some specific key and then resume the program execution.

For example,

Output:

A Key Pressed

We can also use the is_pressed() function to check whether a specific key is pressed or not. It will return a True or False value.

For example,

Output:

True
A Key Pressed

The on_press_key() function can execute a function when a specific key is pressed. We have to pass the function and the required key as parameters to this function.

We will use this function in the following code.

Output:

A Key Pressed

Using the pynput module to detect keypress in Python

The pynput module allows us to work with different input devices like keyboard and mouse.

We will create two functions that will be collected using the Listener function of this module.

See the following code.

Output:

‘a’
‘b’
Key.space

There are a lot of things happening in the above example.

  • The press() function will be executed when we press the key and print the key.
  • Similarly, the on_release() function will be executed when the key is released.
  • These two functions are collected in the Listener() function using the on_press and on_release parameters.
  • We can also stop the Listener() function by pressing some specific key. In our example, if the spacebar is pressed, the release function will return False and the Listener() function will stop.

Using the msvcrt module to detect keypress in Python

The msvcrt is another Windows-only module that can detect keypresses.

We will detect a keypress and print that key using the following code.

Key Pressed b’r’

In the above example,

  • The kbhit() function waits for a key to be pressed and returns True when it is pressed.
  • The getch() function returns the key pressed in a byte string. Notice the b in the output.
  • The break statement will come out of this code block. If it is removed, then the code will keep on executing.
    That’s all about how to detect keypress in Python.

Was this post helpful?


import_contacts

You may also like:

Related Posts

  • 25 June

    Convert Roman Number to Integer in Python

    Table of ContentsHow to Convert Roman Number to Integer in PythonHow to convert roman number to integer in python?Using the if...else statement to convert roman number to integer in Python.Using classes and a dictionary to convert roman number to integer in Python.Using the roman module to convert roman number to integer in Python.ConclusionWas this post […]

  • 11 June

    Remove Single Quotes from String in Python

    Table of ContentsRemove single quotes from string in Python.How to remove single quotes from string in Python?Using the replace() function to remove single quotes from string in Python.Using a for loop to remove single quotes from string in Python.Using the join() function to remove single quotes from string in Python.Using the re module functions to […]

  • 11 June

    Escape Backslash Character in Python

    Table of ContentsEscape backslash character in python.How to use escape backslash character in python?Using the escape backslash character in python to represent whitespace characters.Using the escape backslash character in python to turn special characters into ordinary characters.Using the escape backslash character in f-strings in Python.Using the escape backslash character in raw strings in Python.Was this […]

  • 11 June

    Break out of function in Python

    Table of ContentsBreak out of function in PythonHow to break out of function in Python?Using the return statement to break out of function in PythonUsing the try and except block to break out of function in PythonUsing the sys.exit() function to break out of function in PythonConclusionWas this post helpful? In this post, we will […]

  • 11 June

    Check if Object Is Iterable in Python

    Table of ContentsWhat are iterables in Python?How to check if object is iterable in Python?Using the iter() function to check if object is iterable in PythonUsing the for loop to check if object is iterable in PythonUsing the isinstance() function to check if object is iterable in PythonConclusionWas this post helpful? In this post, we […]

  • 11 June

    Save Numpy Array as Image in Python

    Table of ContentsSave numpy array as image in PythonHow to save numpy array as image in Python?Using the PIL library to save numpy array as image in PythonUsing the matplotlib.image library to save numpy array as image in PythonUsing the opencv library to save numpy array as image in PythonUsing the imageio library to save […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.