I normally write a c program or go linked to xlib to do this sort of work, but I just used xdotool in a bash script for this.
https://gist.github.com/mbrumlow/49f0a3fa311cc3002e4e1fae2e2...
https://gist.github.com/mbrumlow/49f0a3fa311cc3002e4e1fae2e2...