I've been trying to use binding to a KeyPress/KeyRelease such that a particular function runs as long as the key is pressed and ceases when it is released. However, it seems like my major problem is that most keys are considered to continually activate as long as you hold them down... so my function either appears to start over and over again or sometimes immediately ends right after the key is first pressed. Does anyone have any advice?
aot
0
Junior Poster in Training
Recommended Answers
Jump to PostThat may be tough because a continuous key press is interpreted by your operating system as a repeated key press.
Jump to PostIn elec eng., this problem is called 'key debouncing.' When I type here at the keyboard, the keyboard device (or its driver) has to decide whether my keypresses that last tens of milliseconds ... practically forever! ... are supposed to be single keypresses or multiple presses or press-and-hold.
The …
Jump to PostOdd. Here's a full working version:
from Tkinter import * class MyFrame(Frame): def __init__(self, master): Frame.__init__(self, master) self.go = False self.bind('<a>', self.showJudgments) self.bind('<KeyRelease-a>', self.makeChoice) self.pack(expand=YES, fill=BOTH) self.focus_force() def showJudgments(self, event=None): if self.go == False: self.go = True self.showJudgmentsA() else: self.keepShowing() def keepShowing(self): print 'a key being pressed' …
All 12 Replies
vegaseat
1,735
DaniWeb's Hypocrite
Team Colleague
jrcagle
77
Practically a Master Poster
aot
0
Junior Poster in Training
jrcagle
77
Practically a Master Poster
aot
0
Junior Poster in Training
jrcagle
77
Practically a Master Poster
aot
0
Junior Poster in Training
vegaseat
1,735
DaniWeb's Hypocrite
Team Colleague
jrcagle
77
Practically a Master Poster
aot
0
Junior Poster in Training
vegaseat
1,735
DaniWeb's Hypocrite
Team Colleague
Quarks
0
Newbie Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.