.Xmodmap in autostart (Desktop entry) - works logging out and back in, but inconsistent on restart

I created a script with Xmodmap and xcape to modify my keyboard. It’s all together in a script titled .Xmodmap in my home directory, and I added a Desktop entry so it would execute on startup. It works great when 1) I run it in the terminal, or 2) I log out and log back in.

However, when starting from shutdown (or restarting the computer), it isn’t very consistent. For example, some of the commands work, and some of them do not. Other times it all works fine.

I tried delaying the start of the script 5 seconds by running Exec=sh -c "sleep 5 && /home/user/.Xmodmap in my .desktop file, but it doesn’t seem to have an effect. Does anyone know why it’s doing this or how I can fix it? Any help or advice would be appreciated. Thank you.

.Xmodmap is for creating key map, not for running scripts. xmodmap - ArchWiki
And it’s run by xinit: xinit - ArchWiki

Thank you for your help. You’re right, I was formatting .Xmodmap like a script instead of like Xmodmap. So before I had this written in the .Xmodmap file I had created in my home directory:

xmodmap -e "keycode 23 = ISO_Level3_Shift"`

Then I changed it to this:

keycode 23 = ISO_Level3_Shift

And it works great. I don’t need a desktop entry, it’s all automatic because the code is already in .xinitrc.

But the xcape codes I’m using — xcape -e 'ISO_Level3_Shift=Tab' — won’t work with that format, unless I’m doing something wrong. So for a solution I created a separate .xprofile file in /home and put them there (This time with xcape -e) and again, it works automatically on startup without having to manually do it with a desktop entry. In case this helps anyone else.

Thanks again for your help.