'telegraphy' as a means of data entry

E

Elaine Jackson

I need to record the respective times of the events in a sequence of
presses/releases of a particular key on the computer keyboard. The key in
question should also make a sound when depressed (perhaps a forlorn sigh - tee
hee). On the face of it, it seems like it should be fairly straightforward to
achieve this with a suitable combination of ingredients from Tkinter, time and
winsound. If someone has already done it, I would be grateful if they were
willing to share the results with me. If not, I would welcome suggestions as to
how to proceed. Thanks.

Peace
 
P

Peter Hansen

Elaine said:
I need to record the respective times of the events in a sequence of
presses/releases of a particular key on the computer keyboard. The key in
question should also make a sound when depressed (perhaps a forlorn sigh - tee
hee). On the face of it, it seems like it should be fairly straightforward to
achieve this with a suitable combination of ingredients from Tkinter, time and
winsound.

Does this have to work regardless of what else is going on on
the computer, or is it okay if it works only when the window
for this particular application is active? (In other words,
it's easy to do if the keypresses go only to this program,
but if you want to capture *all* keypresses at all times,
you can't do it, AFAIK, using just a GUI toolkit like wxPython
or Tkinter... you have to use an OS hook of some kind.)

-Peter
 
E

Elaine Jackson

| Elaine Jackson wrote:
|
| > I need to record the respective times of the events in a sequence of
| > presses/releases of a particular key on the computer keyboard. The key in
| > question should also make a sound when depressed (perhaps a forlorn sigh -
tee
| > hee). On the face of it, it seems like it should be fairly straightforward
to
| > achieve this with a suitable combination of ingredients from Tkinter, time
and
| > winsound.
|
| Does this have to work regardless of what else is going on on
| the computer, or is it okay if it works only when the window
| for this particular application is active? (In other words,
| it's easy to do if the keypresses go only to this program,
| but if you want to capture *all* keypresses at all times,
| you can't do it, AFAIK, using just a GUI toolkit like wxPython
| or Tkinter... you have to use an OS hook of some kind.)
|
| -Peter

Just when the window is active.
 
P

Peter Hansen

Elaine said:
| Elaine Jackson wrote:
|
| > I need to record the respective times of the events in a sequence of
| > presses/releases of a particular key on the computer keyboard.
|
| Does this have to work regardless of what else is going on on
| the computer, or is it okay if it works only when the window
| for this particular application is active?

Just when the window is active.

Then the relevant bits in wxPython would involve lines like these
lines, pruned from a little app I've been working on:

class Frame(wx.Frame):
def __init__(self, title):
wx.Frame.__init__(self, None, -1, title)
p = wx.Panel(self, -1)

self.Bind(wx.EVT_KEY_DOWN, self.OnKeyDown)
# self.Bind(wx.EVT_KEY_DOWN, self.OnKeyUp)


def OnKeyDown(self, evt):
if evt.GetKeyCode() == wx.WXK_DELETE:
# whatever


You would be able to use time.time() or time.clock() to record
the times of the keypresses, and I don't generally work with
audio so I don't know what would be best.

An alternative would be Pygame, http://www.pygame.org/, which
certainly could handle both the key stuff and the audio.

And Tkinter could certainly do it to, but I don't do Tkinter. :)

-Peter
 
F

Fredrik Lundh

And Tkinter could certainly do it to, but I don't do Tkinter. :)

in Tkinter, you'll find the event time (in milliseconds) in the time
attribute of the event descriptor:

from Tkinter import *

frame = Frame(width=100, height=100)
frame.pack()
frame.focus()

def handler(event):
print event.time / 1000.0

frame.bind("<Key>", handler)

mainloop()

the above only tracks key presses; if you need to track releases
as well, add an extra binding for KeyRelease.

also note that unlike Peter's example, the time attribute contains
the time when the event was generated, not when it reached your
program.

</F>
 
P

Peter Hansen

Fredrik said:
in Tkinter, you'll find the event time (in milliseconds) in the time
attribute of the event descriptor: ....
also note that unlike Peter's example, the time attribute contains
the time when the event was generated, not when it reached your
program.

My example didn't show the time at all. If you want
the time of the event generation in wxPython, you would
use the evt.GetTimestamp() call, which returns the
time of the event generation in milliseconds.

-Peter
 
F

Fredrik Lundh

Peter Hansen said:
My example didn't show the time at all.

and you probably didn't write

"you would be able to use time.time() or time.clock() to record
the times of the keypresses"

either. must be a bug in my newsreader; sorry for that.

</F>
 
P

Peter Hansen

Fredrik said:
and you probably didn't write

"you would be able to use time.time() or time.clock() to record
the times of the keypresses"

either. must be a bug in my newsreader; sorry for that.

Of course I wrote that, obviously. What I didn't do
was include that code in my example... you know, the
code. At least, I don't see that in any of my code,
but perhaps it's a bug in _my_ newsreader.

The reference to using the time module actually comes from
the OP, and I was merely agreeing that it could be possible to
use that module for the purpose intended (presumably what
is best depends on requirements we don't yet know about).

I point that out merely because one might infer from your
reply that Tkinter was capable of something that wxPython
was not in this case, which wouldn't be true.

-Peter
 
E

Elaine Jackson

This is exactly what I was looking for. Thanks very much for the assist.

| Peter Hansen wrote:
|
| > > | > I need to record the respective times of the events in a sequence of
| > > | > presses/releases of a particular key on the computer keyboard.
|
| > And Tkinter could certainly do it to, but I don't do Tkinter. :)
|
| in Tkinter, you'll find the event time (in milliseconds) in the time
| attribute of the event descriptor:
|
| from Tkinter import *
|
| frame = Frame(width=100, height=100)
| frame.pack()
| frame.focus()
|
| def handler(event):
| print event.time / 1000.0
|
| frame.bind("<Key>", handler)
|
| mainloop()
|
| the above only tracks key presses; if you need to track releases
| as well, add an extra binding for KeyRelease.
|
| also note that unlike Peter's example, the time attribute contains
| the time when the event was generated, not when it reached your
| program.
|
| </F>
|
|
|
 
C

Cliff Wells

in Tkinter, you'll find the event time (in milliseconds) in the time
attribute of the event descriptor:
also note that unlike Peter's example, the time attribute contains
the time when the event was generated, not when it reached your
program.


Here's a more complete example in wxPython. Like the effbot's example,
this uses the time the event was generated. I didn't really feel like
examining all the possible places a wav file might exist on all
platforms, but here's a start:


import wx

SOUND = {
'__WXMSW__': 'c:/winnt/media/ding.wav',
'__WXGTK__': '/usr/share/sounds/generic.wav',
}[wx.Platform]

class Panel(wx.Panel):
def __init__(self, parent, id):
wx.Panel.__init__(self, parent, id)
self.ts = None
self.sound = wx.Sound(SOUND)
if not self.sound.IsOk():
self.sound = None
print "Your sound file is bad."
self.Bind(wx.EVT_KEY_DOWN, self.OnKeyDown)

def OnKeyDown(self, evt):
if evt.GetKeyCode() == wx.WXK_DELETE:
t = evt.GetTimestamp() / 1000.0
if self.ts is not None:
print "time between", t - self.ts
self.ts = t
if self.sound:
self.sound.Play(wx.SOUND_ASYNC)

class Frame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, -1, 'test')
p = Panel(self, -1)

if __name__ == '__main__':
app = wx.PySimpleApp()
frame = Frame()
frame.Show()
app.MainLoop()


Regards,
Cliff
 
J

Jeff Epler

also note that unlike Peter's example, the time attribute contains
the time when the event was generated, not when it reached your
program.

Beware! In at least some old versions of Tk, the Windows version used a
function TkpGetMS to find the timestamp for an event, and TkpGetMS calls
GetTickCount(), which as far as I can tell from the documentation is the
wall time now, not when the event was generated.

In fact, this code still seems to be in the HEAD revision of tkWinX.c.
Well, it's my own fault for only fixing the bug locally, not filing any
kind of bug report with the tcl/tk people.

Jeff

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (GNU/Linux)

iD8DBQFBRZUSJd01MZaTXX0RAtQnAJ43jHMPRgF5J1Wf2t/xMv7sdo4HFgCdH7lF
bxf+ZOX/W4LczGoCQLbD3Jk=
=qRUz
-----END PGP SIGNATURE-----
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top