A
A
Aristarkh Deryapa2017-05-21 22:35:57
Python
Aristarkh Deryapa, 2017-05-21 22:35:57

How to handle keypresses in python?

How to do something similar to "pygame.event.get()"? Didn't find the info I needed anywhere. It is desirable to need a structure something like in pygame, more precisely something like this:

for e in pygame.event.get():
   print(e)

PS If that my goal is to make another pygame.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
newpy, 2017-05-21
@newpy

can be used directly:

import msvcrt

msvcrt.getch() # вернет строку байтовую

But it is possible and more correct in your context to work with pygame tools. There is a module for working with the pygame.key keyboard,
for example, via
More details here:
https://www.pygame.org/docs/ref/key.html
Most likely, pygame.event.get() returns a list of events. In a cycle at you just each event is "e".
You just need to look at the dock, most likely one of the events, the key pressed, will have a type related to pygame.key
, that is, one of the events will be just conditionally e == pygame.key , which will most likely be able to call e.get_pressed (). I won’t say more precisely, I don’t have pygame, I didn’t work with it. Just judging by the dock, this should definitely help. Or show what your loop outputs from the code you provided in the question.

R
Roman Mindlin, 2017-05-22
@kgbplus

import win32api as wapi

keyList = ["\b"]
for char in "ABCDEFGHIJKLMNOPQRSTUVWXYZ 123456789,.'£$/\\":
    keyList.append(char)

def key_check():
    keys = []
    for key in keyList:
        if wapi.GetAsyncKeyState(ord(key)):
            keys.append(key)
return keys

GetAsyncKeyState judging by https://msdn.microsoft.com/en-us/library/windows/d...
just reports whether there has been a click since the last call to it
PS: This is all under Windows

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question