2

I am running ML model, which predicts fingure gestures.I am trying to simulate key press event in python using pynput library and I check it's working fine.But I have other program which Is a game written in python using pygame library , which opens up in a new window , but the problem is key press controls doesn't work on that, but it works when I manually press keyboard buttons.

Aviii04
  • 21
  • 1
  • 5
  • PyGame uses SDL library (Simple DirectMedia Layer) and probably it can use direct access to system's events so it may works on lower level than pynput. When I see PyGame in Reinforcement Learning then it uses code created only for Reinforcement Learning – furas Apr 17 '19 at 14:31
  • Is there any way to perform key press simulation at lower level.? – Aviii04 Apr 17 '19 at 15:47
  • Is there any way to perform key press simulation at that lower level? How about ctype? – Aviii04 Apr 17 '19 at 15:48

2 Answers2

5

In pygame you can add events to the event queue by doing:

newevent = pygame.event.Event(type, **attributes) #create the event
    pygame.event.post(newevent) #add the event to the queue

Where type is the type of the event (a numerical constant) and **attributes a keyarg list of attributes, also predefined constants.

All these constants are defined in the pygame.locals module. pygame event docs and pygame key docs list all of them.

So if you want to simulate the press of the 'a' key for example, the code would be:

newevent = pygame.event.Event(pygame.locals.KEYDOWN, unicode="a", key=pygame.locals.K_a, mod=pygame.locals.KMOD_NONE) #create the event
    pygame.event.post(newevent) #add the event to the queue

KEYDOWN is the constant corresponding to the keydown event.
unicode is the unicode representation of the pressed key.
key is a constant associated to the pressed key.
mod is a constant representing a modifier (if the button is pressed while also SHIFT or CAPS_LOCK is pressed, for example).

Hope it helps.

Valentino
  • 7,291
  • 6
  • 18
  • 34
  • Will it work if I simulate this in one python program , and listen this event in another program? – Aviii04 Apr 17 '19 at 20:40
  • This, I do not know. Have a try (and let me know, I am curious too). – Valentino Apr 17 '19 at 20:50
  • After running above command in pygame , should that print "a" ,if I have opened a notepad ? – Aviii04 Apr 18 '19 at 11:57
  • Ah, I see what you want now. No, is not that simple. Pygame events must be listened by the pygame listener event. As far as I know, they are not at OS level. Achieving that would require some stdout redirect to stdin of the other program or a similar trick. I think pygame is not the right tool for this. – Valentino Apr 18 '19 at 12:37
  • I am listening event using keys[pygame.K_a] ....but it doesn't seems to be working. How about ctype library – Aviii04 Apr 18 '19 at 12:45
  • Maybe. But I do not know how to help you with that, it's better you open a new question focused on the use of the library you want to use. – Valentino Apr 18 '19 at 12:53
  • I solved the problem by emulating key press event using keyboard.press and listen the same event using keyboard.Listner() both are present in keyboard library.So I didn't use pygame functions to listen the event. Thanx everyone for ur help. – Aviii04 Apr 20 '19 at 13:29
0

I solved the problem by emulating key press event using keyboard.press and listen the same event using keyboard.Listner() both are present in keyboard library.So I didn't use pygame functions to listen the event. Thanx everyone for ur help.

Aviii04
  • 21
  • 1
  • 5