Homepage Forums RetroPie Project Everything else related to the RetroPie Project emulate keypress using python for emulationstation

Viewing 1 post (of 1 total)
  • Author
    Posts
  • #85364
    lorigio
    Participant

    Hi i’m trying to control emulationstation using python as keyboard. I have this code

    import time
    from evdev import uinput, ecodes as e
    
    def buttonEventHandler ():
       print "keypress"
       with uinput.UInput() as ui:
          ui.write(e.EV_KEY, e.KEY_UP, 1)   
          ui.syn()     
          time.sleep(5)
          ui.write(e.EV_KEY, e.KEY_SPACE, 0)
          ui.syn()
    
    def main():
         buttonEventHandler()
         while True:
             time.sleep(10)
    
    if __name__=="__main__":
        main()
       

    It works only if i first plug a real keyboard then i can disconnect the keyboard and the script starts working and i can see emulationstation reacting when i send the keys from the script. How can i make it working without plug a keyboard first?

    I also tried with a modified version of retrogame but nothing

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <unistd.h>
    #include <fcntl.h>
    #include <poll.h>
    #include <signal.h>
    #include <sys/mman.h>
    #include <linux/input.h>
    #include <linux/uinput.h>
    
    int main(int argc, char *argv[]) {
    
    // Retrogame normally uses /dev/uinput for generating key events.
    	// Cupcade requires this and it's the default.  SDL2 (used by
    	// some newer emulators) doesn't like it, wants /dev/input/event0
    	// instead.  Enable that code by changing to "#if 0" above.
        int fd;
        struct input_event     keyEv, synEv; // uinput events
        
    	if((fd = open("/dev/uinput", O_WRONLY | O_NONBLOCK)) < 0)
    		err("Can't open /dev/uinput");
    
    	struct uinput_user_dev uidev;
    	memset(&uidev, 0, sizeof(uidev));
    	snprintf(uidev.name, UINPUT_MAX_NAME_SIZE, "retrogame");
    	uidev.id.bustype = BUS_USB;
    	uidev.id.vendor  = 0x1;
    	uidev.id.product = 0x1;
    	uidev.id.version = 1;
    	if(write(fd, &uidev, sizeof(uidev)) < 0)
    		err("write failed");
    	if(ioctl(fd, UI_DEV_CREATE) < 0)
    		err("DEV_CREATE failed");
    
    	// Initialize input event structures
    	memset(&keyEv, 0, sizeof(keyEv));
    	keyEv.type  = EV_KEY;
    	memset(&synEv, 0, sizeof(synEv));
    	synEv.type  = EV_SYN;
    	synEv.code  = SYN_REPORT;
    	synEv.value = 0;
       
        keyEv.code = 97;
    
        for(int i=1; i>= 0; i--) { // Press, release
            keyEv.value = i;
            write(fd, &keyEv, sizeof(keyEv));
            usleep(10000); // Be slow, else MAME flakes
            write(fd, &synEv, sizeof(synEv));
            usleep(10000);
        }
        
        while (1 == 1)
        {}
        
        
        ioctl(fd, UI_DEV_DESTROY); // Destroy and
        close(fd);                 // close uinput    
    }

    I used 97 on keyEv.code because i have
    <input name=”a” type=”key” id=”97″ value=”1″ />
    on es_input.cfg is it right?

Viewing 1 post (of 1 total)
  • The forum ‘Everything else related to the RetroPie Project’ is closed to new topics and replies.