detect enter key python code example

Example 1: how make python listen for enter key

import keyboard

# Check if b was pressed
if keyboard.is_pressed('b'):
	print('b Key was pressed')

Example 2: key code for enter key in python

from pynput.keyboard import Key, Controller

keyboard = Controller()

# Press and release space
keyboard.press(Key.space)
keyboard.release(Key.space)

# Type a lower case A; this will work even if no key on the
# physical keyboard is labelled 'A'
keyboard.press('a')
keyboard.release('a')

# Type two upper case As
keyboard.press('A')
keyboard.release('A')
with keyboard.pressed(Key.shift):
    keyboard.press('a')
    keyboard.release('a')

# Type 'Hello World' using the shortcut type method
keyboard.type('Hello World')

Example 3: how to check if user pressed enter in python

# credit to the Stack Overflow user in source link

text = input("type in enter")  # or raw_input in python2
if text == "":
    print("you pressed enter")
else:
    print("you typed some text before pressing enter")

Tags:

C Example