Keylogging is legal if used ethically and legally. It becomes illegal when used to breach someone's privacy intentionally.