How can I detect the amount of time that a button is pressed in python?

1.2k views Asked by At

I need to make a morse code encoder in python.
Based on the length of time, the key pressed will be a dot (.) or a dash (-).

Ideally I would like this input process to be "running in the background" so I can do other things in my program in the mean time. I was thinking about using threading, but I think Tk actually does this for me, even though I don't need the graphics capabilities.

Any idea how to get the length of time the key was pressed down?

import threading
import Tkinter as tk

def onKeyPress(event):
    print event.char
    print "time =", "????"

root = tk.Tk()
root.bind('<KeyPress>', onKeyPress)
root.mainloop()



#class KeyEventThread(threading.Thread):
#    def run(self):
#         your while-loop here
#kethread = KeyEventThread()
#kethread.start()
0

There are 0 answers