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?

That may be tough because a continuous key press is interpreted by your operating system as a repeated key press.

In 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 usual way to solve the problem in software is to put a flag or timer on the key.

The flag system is tied to events: if the action is supposed to take place on a single keypress, then set a flag when the action starts and clear it when the action stops. Here's one possibility:

class mywidget(Button):
   def __init__(self, master):
    .... 
        self.bind('<KeyPress-b>', do_bleah)
        self.bind('<KeyRelease-b>',reset_bleah)
        self.reset_bleah() # must initialize the flag!

   def print_bleah(self, event=None):
        if self.bleahOK:
             self.bleahOK = False
             self.set_up_bleah()
             # do other bleah-y stuff if needed
       else:
             self.continue_bleahing() 

   def reset_bleah(self, event=None):
       self.bleahOK = True

This way, the initial press sets up the action, but subsequent holding causes the action to continue. Basically, your function acts like two functions in one. It has one action on first keypress and a different action on key-holding.

You may recognize the flag as similar to a lock as used in threaded programs.

A second way to solve this for real-time systems (like pygame) is to set a timer of sorts:

self.oldtime = 0
do_bleah(self,event=None):
   if time.time() - self.oldtime > LONGENOUGH:
      self.oldtime = time.time()
      set_up_bleah()
   else:
      continue_bleahing()

Here, you impose a delay by setting oldtime when the button is first pressed and requiring that the next registered press be at least LONGENOUGH later.

IMO, this might be quirky in a system like Tkinter where the results of your keypress might not be finished when time is up. So I would probably go for the flag system with Tkinter and the timer system with something like pygame.

Jeff

Okay, I've been testing out your advice with my code. Maybe I'm being thick, but so far it's just not working. Here's what I've got:

self.go = False

        self.innerframe = Frame(frame)
        self.innerframe.bind('<a>', self.showJudgments)
        self.innerframe.bind('<KeyRelease-a>', self.makeChoice)
        self.innerframe.pack(expand=YES, fill=BOTH)
        self.innerframe.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'

Yet what happens when I hold the 'a' key is that both showJudgmentsA and makeChoice immediately get called over and over again, and keepShowing never gets called. I just don't get it.

Odd. 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'

    def showJudgmentsA(self):
        print "key-press started"

    def makeChoice(self, event=None):
        print "choice made"
        self.go = False


mainw = Tk()
mainw.f = MyFrame(mainw)
mainw.f.grid()
mainw.mainloop()

with output

>>> 
key-press started       # pressed 'a' here
a key being pressed
a key being pressed
a key being pressed
a key being pressed
a key being pressed
a key being pressed
a key being pressed
a key being pressed
a key being pressed
a key being pressed
a key being pressed
a key being pressed
a key being pressed
a key being pressed
a key being pressed
a key being pressed
a key being pressed
choice made            # let go here
key-press started     # next press here
a key being pressed
a key being pressed
a key being pressed
a key being pressed
a key being pressed
a key being pressed
a key being pressed
a key being pressed
a key being pressed
choice made            # ...and release.
>>>

Hope it helps,
Jeff

Thanks for this. It must be that something is wrong with my setup because this code also doesn't work for me. When I run it, the frame flashes at a painfully visible rate, and nothing whatsoever happens when I press and release the 'a' key.

I'm using a Mac and the Eclipse IDE. I'll give it a try on my PC at home tonight, but I really do need this program to work on Macs, since that's what I have at work. Any thoughts?

P.S. Is there an easy way to copy/paste example code? Whatever I tried, I always ended up without tabbed spacing but with the unnecessary line numbers.

Bummer! Macs aren't in my area, sorry to say. The unnecessary line numbers go away if you click the 'toggle plain text' widget underneath.

Can you get the individual events, '<a>' and '<KeyRelease-a>' to work correctly?

Jeff

Yep, by themselves they work fine.

Also, an update: I managed to get your example to work (I had to pack the frame instead of gridding it). However, my output is different:

key-press started
choice made
key-press started
choice made
key-press started
choice made
key-press started
choice made

As you can see, my computer treats holding down the 'a' key as pressing and releasing it over and over (instead of just pressing it). As such, I don't really see a way to code what I want, as there's no way of determining what the final KeyRelease action is (unless there's some way of testing whether a key's been pressed in the last X milliseconds, then I could act only when it hasn't been, although there'd be a delay).

However, I've found an easy way out for now: it works fine with mouse button presses.

...
P.S. Is there an easy way to copy/paste example code? Whatever I tried, I always ended up without tabbed spacing but with the unnecessary line numbers.

For code enclosed in the [code=python] and [/code] tag pair, click on "Toggle Plain Text" so you can highlight and copy the code to your editor without the line numbers.

If I get this right, with the Mac OS a continuous key press is interpreted as a repeated key press/key release event pair?

unless there's some way of testing whether a key's been pressed in the last X milliseconds, then I could act only when it hasn't been, although there'd be a delay

I think that'll be your winner. It'll be more of the timer method mentioned above rather than the flag method.

I'm surprised that the MacOS treats keypresses like this; could it be a feature of Tkinter for Macs? I'm curious enough that I'll post it on the Tkinter listserv and see what the pros know.

Jeff

Ah, thanks to both of you. Yup, as far as I can tell, it's interpreting the continuous press as you said, as a repeated keypress/keyrelease event pair. My guess is that it's to do with the Mac or possibly the version of Tkinter I'm using.

I'm looking forward to what you find out, Jeff. Could you please post it here or send me a private message when you hear?

Cheers,

Aot

You will have to sit there with a stopwatch, but this could do the timing for you ...

# count how many times the keyboard is scanned on
# one continuous keypress of character 'a' 
import Tkinter as tk
class MyFrame(tk.Frame):
    def __init__(self, master):
        tk.Frame.__init__(self, master)
        # method call counter
        self.counter_press = 0
        self.pack()
        root.bind_all('<a>', self.key_press)
        
    def key_press(self, event):
        self.counter_press += 1
        # show result in title
        root.title(str(self.counter_press))
        
root = tk.Tk()
root.geometry("400x30+0+0")
app1 = MyFrame(root)
root.mainloop()

I was having the same problem, the ideas here brought me to the solution, using the after_idle methode:

from Tkinter import *

class MyFrame(Frame):
    def __init__(self, master):
        tk.Frame.__init__(self, master)
        # method call counter
        self.pack()
        self.afterId = None
        root.bind('<KeyPress-a>', self.key_press)
        root.bind('<KeyRelease-a>', self.key_release)
        
    def key_press(self, event):
        if self.afterId != None:
            self.after_cancel( self.afterId )
            self.afterId = None
        else:
            print 'key pressed %s' % event.time

    def key_release(self, event):
        self.afterId = self.after_idle( self.process_release, event )

    def process_release(self, event):
        print 'key release %s' % event.time
        self.afterId = None

        
        
root = Tk()
root.geometry("400x30+0+0")
app1 = MyFrame(root)
root.mainloop()
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.