Reputation: 11
I am trying to make a program so that when I press a button on my gamepad and hold it, a certain character will be written and repeated as long as I hold the button, just as a keyboard works.
Until now, I have managed to insert any character that I wanted, but the hard part is keeping it pressed.
I tried using pyGame, win32api, win32con, but I couldn't find anything useful. (Maybe I dind't have a full understanding of these libraries)
Furthermore, I am a rookie when it comes to python programming, so if you need extra info to make my question more understandable, please do tell.
Thank you.
Upvotes: 1
Views: 2542
Reputation: 1403
For pygame try this one: http://www.pygame.org/docs/ref/key.html#pygame.key.set_repeat
seems to me like exactly what you're looking for.
If you don't want to solve it that way you would have to write a thread, that starts on the key down event, stops on the key release event and calls itself over and over again while invoking the key event. This would look somehow like this:
from threading import Thread, Event
from time import sleep
class BtnRepeater(Thread)
def __init__(self, *args, **kw):
self.stop = Event()
if 'startdelay' in kw:
self.stdelay = kw.pop('startdelay')
else:
self.stdelay = 1 default delay
if 'repeatdelay' in kw:
self.rpdelay = kw.pop('startdelay')
else:
self.rpdelay = 0.2 default delay
Thead.__init__(self, *args, **kw)
def run(self):
sleep(self.stdelay)
while not self.stop.is_set():
# invoke event here
sleep(self.rpdelay)
def event_handler_btn_down(event):
# needs to be binded somewhen
br = BtnRepeater()
bind(event.key,'key_release',br.stop.set) # this is no real pygame call - but you probably know what I'm talking about
br.start()
Upvotes: 1