Can i send Keyboard strokes through SSH to my Raspberry to quit a game emulator?

So , the problem is im trying to exit NES emulator through SSH ,it says i need to click ESC to do so , and i want to send a simulated key press to make it work . SSH channel is working because im using it to start scripts. Im working in AndroidStudio

Here is the code

public class MainActivity extends AppCompatActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final Button button = findViewById(R.id.button);
        final Button button2 = findViewById(R.id.button2);




        button.setOnClickListener(new View.OnClickListener() {
            @Override

            //button do uruchomienia roma
            public void onClick(View v) {

                Thread thread = new Thread(new Runnable() {

                    public void run() {
                        try {

                            executeRemoteCommand("pi", "123","192.168.0.115", 22,"sudo python3 UruchomienieRoma.py");

                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }



       });
thread.start();

    }
    });


        button2.setOnClickListener(new View.OnClickListener() {
            @Override

            //button do uruchomienia roma
            public void onClick(View v) {

                Thread thread2 = new Thread(new Runnable() {

                    public void run() {
                        try {

                            executeRemoteCommand("pi", "123","192.168.0.115", 22,"sudo python3 ESC.py " );

                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }



                });
                thread2.start();

            }
        });




    }

    public  String executeRemoteCommand(String username,String password,String hostname,int port ,String command)
            throws Exception {
        JSch jsch = new JSch();
        Session session = jsch.getSession(username, hostname, port);
        session.setPassword(password);

        // Avoid asking for key confirmation
        Properties prop = new Properties();
        prop.put("StrictHostKeyChecking", "no");
        session.setConfig(prop);

        session.connect();

        // SSH Channel
        ChannelExec channelssh = (ChannelExec)
                session.openChannel("exec");
        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        channelssh.setOutputStream(baos);

        // Execute command
        channelssh.setCommand(command);
        channelssh.connect();
        channelssh.disconnect();

         return baos.toString();
    }
}

I've tried to make scripts in python to simulate ESC press (in pyautogui and some others ) but emulator did not respond , shell did

Thanks for response