How to Install keyboard in Python
Hook and simulate keyboard events on Windows and Linux
pip install keyboard
What is keyboard?
Hook and simulate keyboard events on Windows and Linux
Take full control of your keyboard with this small Python library. Hook global events, register hotkeys, simulate key presses and much more.
- Global event hook on all keyboards (captures keys regardless of focus). - Listen and send keyboard events. - Works with Windows and Linux (requires sudo), with experimental OS X support (thanks @glitchassassin!). - Pure Python, no C modules to be compiled. - Zero dependencies. Trivial to install and deploy, just copy the files. - Python 2 and 3. - Complex hotkey support (e.g. ) with controllable timeout. - Includes high level API (e.g. record and play, addabbreviation). - Maps keys as they actually are in your layout, with full internationalization support (e.g. ). - Events automatically captured in separate thread, doesn't block main program. - Tested and documented. - Doesn't break accented dead keys (I'm looking at you, pyHook). - Mouse support available via project mouse ().
or clone the repository (no installation required, source files are sufficient):
Quick Start
Minimal example to get started with keyboard:
import keyboard
print(keyboard.__version__)
Installation
pip (standard)
pip install keyboard
Virtual environment (recommended)
python -m venv venv
source venv/bin/activate # Windows: venv\Scripts\activate
pip install keyboard
pip3
pip3 install keyboard
conda
conda install -c conda-forge keyboard
Poetry
poetry add keyboard
Dependencies
Installing keyboard will also install these packages:
Verify the Installation
After installing, confirm the package is available:
python -c "import keyboard; print(keyboard.__version__)"
If this prints a version number, installation succeeded. If you see a ModuleNotFoundError, see the errors section below.
Installation Errors
Common errors when installing keyboard with pip.
ModuleNotFoundError: No module named 'keyboard'
Cause: The package is not installed in the current Python environment.
Fix: Run pip install keyboard. If using a virtual environment, ensure it is activated first.
ModuleNotFoundError: No module named 'keyboard' (installed but still failing)
Cause: pip installed the package into a different Python than the one running your script.
Fix: Use python -m pip install keyboard to install into the interpreter you are running.
ImportError: cannot import name 'X' from 'keyboard'
Cause: The function or class does not exist in the installed version.
Fix: Check the version with pip show keyboard and upgrade with pip install --upgrade keyboard.
pip: command not found
Cause: pip is not in PATH or Python was not added to PATH during installation.
Fix: Try python -m pip install keyboard. On macOS/Linux try pip3.
PermissionError: [Errno 13] Permission denied
Cause: No write access to the system Python package directory.
Fix: Use a virtual environment, or add --user: pip install --user keyboard
SSL: CERTIFICATE_VERIFY_FAILED
Cause: pip cannot verify PyPI's SSL certificate — common behind corporate proxies.
Fix: Try: pip install --trusted-host pypi.org --trusted-host files.pythonhosted.org keyboard
Recent Releases
| Version | Released |
|---|---|
0.13.5 latest |
2020-03-23 |
0.13.4 |
2019-09-25 |
0.13.3 |
2019-03-18 |
0.13.2 |
2018-05-18 |
0.13.1 |
2018-03-27 |
Manage keyboard
Upgrade to latest version
pip install --upgrade keyboard
Install a specific version
pip install keyboard==0.13.5
Uninstall
pip uninstall keyboard
Check what is installed
pip show keyboard