Skip to content

Commit 6d4dc8b

Browse files
committed
added controlling keyboard tutorial
1 parent 7ce7e62 commit 6d4dc8b

File tree

4 files changed

+27
-0
lines changed

4 files changed

+27
-0
lines changed

Diff for: README.md

+1
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,5 @@ This is a repository of all the tutorials of [The Python Code](https://www.thepy
2828
- [How to Transfer Files in the Network using Sockets in Python](https://www.thepythoncode.com/article/send-receive-files-using-sockets-python). ([code](general/transfer-files/))
2929
- [How to Get Hardware and System Information in Python](https://www.thepythoncode.com/article/get-hardware-system-information-python). ([code](general/sys-info))
3030
- [How to Control your Mouse in Python](https://www.thepythoncode.com/article/control-mouse-python). ([code](general/mouse-controller))
31+
- [How to Control your Keyboard in Python](https://www.thepythoncode.com/article/control-keyboard-python). ([code](general/keyboard-controller))
3132

Diff for: general/keyboard-controller/README.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# [How to Control your Keyboard in Python](https://www.thepythoncode.com/article/control-keyboard-python)
2+
To make this work:
3+
- pip3 install -r requirements.txt
4+
5+
And run the lines of code in `control_keyboard.py` individually, that's because it makes no sense to run them at once.

Diff for: general/keyboard-controller/control_keyboard.py

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import keyboard
2+
3+
# registering a hotkey that replaces one typed text with another
4+
# replaces every "@email" followed by a space with my actual email
5+
keyboard.add_abbreviation("@email", "[email protected]")
6+
7+
# invokes a callback everytime a hotkey is pressed
8+
keyboard.add_hotkey("ctrl+alt+p", lambda: print("CTRL+ALT+P Pressed!"))
9+
10+
# check if a ctrl is pressed
11+
print(keyboard.is_pressed('ctrl'))
12+
13+
# press space
14+
keyboard.send("space")
15+
16+
# sends artificial keyboard events to the OS
17+
# simulating the typing of a given text
18+
# setting 0.1 seconds to wait between keypresses to look fancy
19+
keyboard.write("Python Programming is always fun!", delay=0.1)
20+

Diff for: general/keyboard-controller/requirements.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
keyboard

0 commit comments

Comments
 (0)