Keyboard module in Python

PythonServer Side ProgrammingProgramming

In this article, we will learn about the use of the Keyboard module in Python 3.x. Or earlier.

Ide preferred − Jupyter notebook

Installation

>>> pip install keyboard

Functionalities of the module −

  • Allows us to block the action of specific keys
  • We can manage intents from the keyboard using on click listeners.
  • Cross-platform compatibility.
  • Supports special & hotkeys available on the keyboard.

Now let’s implement this in the form of code −

Example

import keyboard
# It writes the content
keyboard.write("Tutorialspoint\n")
# It writes end of line
keyboard.press_and_release('shift + o, shift + y, \n')
keyboard.press_and_release('k, j')
# it blocks until ctrl
keyboard.wait('Ctrl')

Output

Tutorialspoint
O Y
k j

Example

import keyboard
# It writes the content
keyboard.write("tutor\n")
# It writes end of line
keyboard.press_and_release('shift + a, shift + w, \n')
keyboard.press_and_release('q, u')
# it blocks until enter
keyboard.wait('Enter')

Output

tutor
A W
q u

We can also use the record and the play method available in the keyboard module that can be directly imported into the console. In this way, we can also record keyboard activities. To know more details you may refer to https://pypi.org/project/keyboard/

Conclusion

In this article, we will learn about the application of the keyboard module in Python 3.x. Or earlier.

raja
Published on 28-Aug-2019 17:36:59
Advertisements