Not all keyloggers are illegal. They’re often used legally for employee tracking and to maintain IT security. If you installed a keylogger on your device on purpose or consented to a keylogger being installed, its use is completely legal.