Page 1 of 1

Trapping and using keypresses

Posted: Wed Jun 19, 2013 9:39 am
by rbeynon
Hello all,

I have a program that is used to allow my biologist colleagues to watch animal behaviour videos and record the time and nature of 'behaviours' by a simple app that allows them to start record/capture key presses/check for specific keys/end recording/save file

It has worked well for years, but need some tweaks. When I fire it up in LiveCode, it no longer works. I used to trap the key pressed and the time in a field, but now, the keypresses seem to go the first field of the app, not the one I name. Here's the relevant bits..

Code: Select all

on mouseUp
  put empty into field "data"
  put empty into field "lastLine"
  beep
  put the milliseconds into stTime
  put the seconds into disTime
  put false into stopNow
  put true into clockRunning
  put 0 into pauseStart
  put 0 into pauseStop
  put 0 into pauseInterval
end mouseUp

on keyDown theKey
  put the milliseconds into keyTime
  subtract stTime from keyTime
  subtract pauseInterval from keyTime
  
  put value(pauseInterval)/1000 into field "date"
  
  switch
  
  case theKey = "?" 
    put "Elapsed time: " && keyTime/1000 && "s elapsed" into field "lastLine"
    break
  
  case theKey="q"
    send mouseUp to button "Stop"
    break

default   
  put "Event:" && theKey && keyTime/1000 & return after field "Data"
    set the scroll of field "Data" to the formattedHeight of field "Data"
    put "Elapsed time:" && keyTime/1000 into field "lastLine"
  end switch

end keyDown
Advice on capturing and redirecting keypress would be most welcome. Thank you.

Re: Trapping and using keypresses

Posted: Wed Jun 19, 2013 3:07 pm
by dunbarx
Hi.

You make several references to variables in the keyDown handler that are not valid. (ex.:subtract stTime from keyTime) Is there more to the script? Are these handlers in a field script? If so, is it locked? You cannot trap a mouseUp handler in an unlocked field, and you cannot type (directly, at least) into a locked one.

Craig Newman

Re: Trapping and using keypresses

Posted: Wed Jun 19, 2013 6:09 pm
by rbeynon
Sorry Craig,
I was trying to be efficient. Yes there are some global variable sand other components surrounding this script.

The key line is

Code: Select all

put "Event:" && theKey && keyTime/1000 & return after field "Data"
this no longer directs keypresses at the relevant field (unlocked) but sends the presses to the 'first' field on the card.

Thanks
Rob

Re: Trapping and using keypresses

Posted: Wed Jun 19, 2013 7:04 pm
by dunbarx
What happens if you do this in the msg box? What happens if you change the name of the field, or use its ID instead?

Craig

Re: Trapping and using keypresses

Posted: Mon Jun 24, 2013 4:03 am
by astacus
I tried to redo the app from the beginning, and it works, sort of! It is all about having the focus in the right place (i.e nowhere). It works, but one set up keypress too many, and it fails. I'd like to ask some kind soul if they could help. I'd send them the .livecode file.

Actually, just realised it can be uploaded here.

I'd welcome any insight about directing the key presses to the field "data"

Thanks

rob

Re: Trapping and using keypresses

Posted: Mon Jun 24, 2013 6:28 am
by Simon
Hi Rob,
I downloaded your stack and it actually seems to be working here.

But there is something different:

Code: Select all

put "Event:" && theKey && keyTime/1000 & return after field "Data"
isn't in the file you posted.

Any steps I should follow to see it fail?
I just used "Start" and "q" and numbers were dumped into field Data.

There are some things I've never seen before:

Code: Select all

...return after card field "data"
don't think that "card" is needed.

Simon
EDIT:
LC 6.0.0 LC 5.5.5 win7

Re: Trapping and using keypresses

Posted: Mon Jun 24, 2013 7:44 am
by astacus
Simon, thanks so much for your time...

The key line is in the on keyDown switch, default handler...

Code: Select all

put theKey & tab & round(keyTime/1000,2) & return after card field "data"
Now try this. When recording, click on the field "data". Where are all my events going?

I think the expression 'card field' (which is valid) refers to the Hypercard legacy!

Thanks
rob

Re: Trapping and using keypresses

Posted: Mon Jun 24, 2013 7:57 am
by astacus
I forgot to say - the "Event:" text was removed because it was simpler for the downstream statisticians to handle the file without spurious text!

Re: Trapping and using keypresses

Posted: Mon Jun 24, 2013 8:14 am
by Simon
Hi Rob,
Still I'm not getting any large error (see pic).
Though I did just notice the "?" only works before I press other keys.

Simon

EDIT:
Clicking on the Data field does not show anything new.

Re: Trapping and using keypresses

Posted: Mon Jun 24, 2013 7:51 pm
by astacus
Curiouser and curiouser..

I go through the setup. OK
click start - OK
(btw ? works at any time all it does is update elapsed time in red)
keep recording keypresses...

then

click once over the data field.

For me, from now, all key presses disappear into the void. This is in interpreted mode. Also in compiled mode though.

Again, thanks
Rob

Re: Trapping and using keypresses

Posted: Mon Jun 24, 2013 8:04 pm
by mwieder
Rob- your keydown handler is in the script of the Start button. The only keypresses that will capture are those going to the button, i.e., while the button has focus. When you click in the field, the field gets the focus and so you are no longer trapping the keydown events.

If you move your keydown handler into the card script it will do what you want, although it would be better in that situation to have the start and stop buttons set a flag that would enable the trapping.

Re: Trapping and using keypresses

Posted: Mon Jun 24, 2013 9:56 pm
by astacus
Ahhhhh.... (lightbulb moment!)
Checking it now :-)
Thanks

Re: Trapping and using keypresses

Posted: Mon Jun 24, 2013 10:13 pm
by astacus
Update:

That was some simple. Hindsight is a wonderful thing. My application is now doing what is required. hence, a rare and self-indulgent :)

Thanks to all of you who were gracious enough to take the time to address what turned out to be a very silly problem. I really need to think about inheritance!

Rob