from pynput import keyboard

The short answer is , but with conditions. Keylogging software is legal to own and use on devices that you own.