When Wayland was born you couldn't do anything to prevent that, now we have like 6 sandboxing/light-virtualization options for Linux, so I suspect that by containerizing applications in Xorg you could prevent them from keylogging easily. That too right now is mostly crappy and fragmented in Linux with the AppImage Snap and flatpak circus.
And no, I don’t think you can really do that with x11. There was Trusted X11 long time ago, but I don’t think it was practically useful - as in, maintainable and sustainable.
As far as I understand sandboxing in Linux is “70% there” - it will get there eventually, but for now the 30% hole makes it not practically useful. It’s not bad at pretending it’s doing something though, and for (many? most?) paying customers that’s enough.