1import pyautogui
2
3# Holds down the alt key
4pyautogui.keyDown("alt")
5
6# Presses the tab key once
7pyautogui.press("tab")
8
9# Lets go of the alt key
10pyautogui.keyUp("alt")
1pip install keyboard
2
3import keyboard
4
5keyboard.press_and_release('shift+s, space')
6
7keyboard.write('The quick brown fox jumps over the lazy dog.')
8
9keyboard.add_hotkey('ctrl+shift+a', print, args=('triggered', 'hotkey'))
10
11# Press PAGE UP then PAGE DOWN to type "foobar".
12keyboard.add_hotkey('page up, page down', lambda: keyboard.write('foobar'))
13
14# Blocks until you press esc.
15keyboard.wait('esc')
16
17# Record events until 'esc' is pressed.
18recorded = keyboard.record(until='esc')
19# Then replay back at three times the speed.
20keyboard.play(recorded, speed_factor=3)
21
22# Type @@ then press space to replace with abbreviation.
23keyboard.add_abbreviation('@@', 'my.long.email@example.com')
24
25# Block forever, like `while True`.
26keyboard.wait()
1import keyboard # using module keyboard
2import time
3
4stop = False
5def onkeypress(event):
6 global stop
7 if event.name == 'q':
8 stop = True
9
10# ---------> hook event handler
11keyboard.on_press(onkeypress)
12# --------->
13
14while True: # making a loop
15 try: # used try so that if user pressed other than the given key error will not be shown
16 print("sleeping")
17 time.sleep(5)
18 print("slept")
19 if stop: # if key 'q' is pressed
20 print('You Pressed A Key!')
21 break # finishing the loop
22 except:
23 print("#######")
24 break # if user pressed a key other than the given key the loop will break
25
1import keyboard # using module keyboard
2while True: # making a loop
3 try: # used try so that if user pressed other than the given key error will not be shown
4 if keyboard.is_pressed('q'): # if key 'q' is pressed
5 print('You Pressed A Key!')
6 break # finishing the loop
7 except:
8 break # if user pressed a key other than the given key the loop will break
9