keyboard if is combination python code example
Example 1: how to create a hotkey in python
import keyboard
import time
hotkey = "shift + ctrl + F2"
while True:
if keyboard.is_pressed(hotkey):
print("Hotkey is being pressed")
time.sleep(0.05)
time.sleep(0.01)
Example 2: how to execute key combinations with keyboard python lib
from pynput.keyboard import Key, Controller
keyboard = Controller()
keyboard.press(Key.space)
keyboard.release(Key.space)
keyboard.press('a')
keyboard.release('a')
keyboard.press('A')
keyboard.release('A')
with keyboard.pressed(Key.shift):
keyboard.press('a')
keyboard.release('a')
keyboard.press(KeyCode.from_vk(0x5C))
keyboard.press(KeyCode.from_vk(0x27))
keyboard.release(KeyCode.from_vk(0x27))
keyboard.release(KeyCode.from_vk(0x5C))
keyboard.type('Hello World')