You can use ADB commands to do this type of thing or send shell commands on a rooted phone.
Otherwise you need to create an Android 'Accessibility Service' app, this would need a special plugin and also need extra mods to the manifest.
Note. Google Play would probably reject this type of app unless you have a VERY good reason for it because of the security implications.