1

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.

asked May 13, 2013 at 18:08

1 Answer 1

1

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()
answered May 13, 2013 at 18:40
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.