KeyPress event with holding down the key

Reply

Join Date: Feb 2007
Posts: 77
Reputation: aot is an unknown quantity at this point 
Solved Threads: 1
aot's Avatar
aot aot is offline Offline
Junior Poster in Training

Re: KeyPress event with holding down the key

 
0
  #11
Mar 2nd, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 3,959
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 918
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: KeyPress event with holding down the key

 
0
  #12
Mar 2nd, 2007
You will have to sit there with a stopwatch, but this could do the timing for you ...
  1. # count how many times the keyboard is scanned on
  2. # one continuous keypress of character 'a'
  3. import Tkinter as tk
  4. class MyFrame(tk.Frame):
  5. def __init__(self, master):
  6. tk.Frame.__init__(self, master)
  7. # method call counter
  8. self.counter_press = 0
  9. self.pack()
  10. root.bind_all('<a>', self.key_press)
  11.  
  12. def key_press(self, event):
  13. self.counter_press += 1
  14. # show result in title
  15. root.title(str(self.counter_press))
  16.  
  17. root = tk.Tk()
  18. root.geometry("400x30+0+0")
  19. app1 = MyFrame(root)
  20. root.mainloop()
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 1
Reputation: Quarks is an unknown quantity at this point 
Solved Threads: 0
Quarks Quarks is offline Offline
Newbie Poster

Re: KeyPress event with holding down the key

 
0
  #13
Nov 4th, 2007
I was having the same problem, the ideas here brought me to the solution, using the after_idle methode:

  1. from Tkinter import *
  2.  
  3. class MyFrame(Frame):
  4. def __init__(self, master):
  5. tk.Frame.__init__(self, master)
  6. # method call counter
  7. self.pack()
  8. self.afterId = None
  9. root.bind('<KeyPress-a>', self.key_press)
  10. root.bind('<KeyRelease-a>', self.key_release)
  11.  
  12. def key_press(self, event):
  13. if self.afterId != None:
  14. self.after_cancel( self.afterId )
  15. self.afterId = None
  16. else:
  17. print 'key pressed %s' % event.time
  18.  
  19. def key_release(self, event):
  20. self.afterId = self.after_idle( self.process_release, event )
  21.  
  22. def process_release(self, event):
  23. print 'key release %s' % event.time
  24. self.afterId = None
  25.  
  26.  
  27.  
  28. root = Tk()
  29. root.geometry("400x30+0+0")
  30. app1 = MyFrame(root)
  31. root.mainloop()
Last edited by Quarks; Nov 4th, 2007 at 6:09 pm.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC