Reputation: 21
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.
Upvotes: 2
Views: 4029
Reputation: 21
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.
Upvotes: 0
Reputation: 7361
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.
Upvotes: 6