2

So i am using python inside an ubntu and what i am doing is reading frame data from a game which i am playing in realtime (for genrating traning data for ML) and for each frame i capture in realtime (from the game screen) i want to know weather any keyboard key is pressed or not and finally which one and if no key is pressed i should get a clear indication for that as well.

NOTE: My program is running in a seprate window while i'll be playing in some other window.

I want a function like:

z=get_current_key_input(event)
if z == None: #This may cause warning form the warnings module
    print 'No key is pressed for this frame move on to the next.'
else:
    print str(z.key_code),'key is pressed for this frame.'

I can provide you with the code but it's messy and has cv2 and few more painful dependencies , so i think i'll provide it on demand.

All i am getting through googling is a function that gets called everytime i press a key but it's asyncronous and causes delays and so, the traning data i genrate using that is crap as hell.

[UPDATE]:

I have found a quiet promising function through intense search but can't actually work my way around it's complex usage and painful documentation. If anyone can understands how to work these documentations then it would be a great help to me. link to that function :here!

Ubdus Samad
  • 1,218
  • 1
  • 15
  • 27

1 Answers1

2

Here's a simple program from pynput example:

def on_press(key):
    try:
        print('alphanumeric key {0} pressed'.format(key.char))
    except:
        print('special key {0} pressed'.format(key))

def on_release(key):
    print('{0} released'.format(key))
    if key == keyboard.Key.esc:
        return False

def get_current_key_input():
    with keyboard.Listener(on_press=on_press, on_release=on_release) as listener:
        listener.join()
Gahan
  • 4,075
  • 4
  • 24
  • 44
  • That's what am here for, don't worry. – Ubdus Samad May 28 '17 at 05:58
  • A problem here , it activates whenever i press any key, but i want a function like i explained in the fuction which checks weather a key is pressed or not , NOT a function which gets called when a key is pressed. Can you make something like that? – Ubdus Samad May 28 '17 at 06:12
  • what you want to do if key is not pressed? – Gahan May 28 '17 at 06:13
  • i just want to get an output like False or 'no key is pressed right now' , when i call to check the key else if a key is pressed when i check i want true or something like that. – Ubdus Samad May 28 '17 at 06:18
  • is this even possible? – Ubdus Samad May 28 '17 at 06:19
  • yeah but it requires more effort. you need to create another function which [breaks if no activity for certain amount of time](https://stackoverflow.com/questions/25027122/break-the-function-after-certain-time) and during that activity period you need to call this function. to integrate directly to your another program I have also made some changes in answer just call get_current_key_input(). – Gahan May 28 '17 at 06:25
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/145297/discussion-between-gahan-and-ubdus-samad). – Gahan May 28 '17 at 06:25
  • Thanks, I found another way around using a bit from your answer! – Ubdus Samad Jun 04 '17 at 11:43