DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   Python (http://www.daniweb.com/forums/forum114.html)
-   -   KeyPress event with holding down the key (http://www.daniweb.com/forums/thread70746.html)

aot Mar 2nd, 2007 5:24 am
Re: KeyPress event with holding down the key
 
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

vegaseat Mar 2nd, 2007 1:37 pm
Re: KeyPress event with holding down the key
 
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()

Quarks Nov 4th, 2007 6:06 pm
Re: KeyPress event with holding down the key
 
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()


All times are GMT -4. The time now is 12:20 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC