Trapping and using keypresses

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
rbeynon
Posts: 15
Joined: Fri Sep 21, 2007 7:07 am

Trapping and using keypresses

Post by rbeynon » Wed Jun 19, 2013 9:39 am

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.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10305
Joined: Wed May 06, 2009 2:28 pm

Re: Trapping and using keypresses

Post by dunbarx » Wed Jun 19, 2013 3:07 pm

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

rbeynon
Posts: 15
Joined: Fri Sep 21, 2007 7:07 am

Re: Trapping and using keypresses

Post by rbeynon » Wed Jun 19, 2013 6:09 pm

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

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10305
Joined: Wed May 06, 2009 2:28 pm

Re: Trapping and using keypresses

Post by dunbarx » Wed Jun 19, 2013 7:04 pm

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

astacus
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 7
Joined: Sat Jan 09, 2010 9:11 pm

Re: Trapping and using keypresses

Post by astacus » Mon Jun 24, 2013 4:03 am

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
Attachments
Timer June 2013.livecode.zip
Timer programme
(3.58 KiB) Downloaded 275 times

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: Trapping and using keypresses

Post by Simon » Mon Jun 24, 2013 6:28 am

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
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

astacus
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 7
Joined: Sat Jan 09, 2010 9:11 pm

Re: Trapping and using keypresses

Post by astacus » Mon Jun 24, 2013 7:44 am

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

astacus
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 7
Joined: Sat Jan 09, 2010 9:11 pm

Re: Trapping and using keypresses

Post by astacus » Mon Jun 24, 2013 7:57 am

I forgot to say - the "Event:" text was removed because it was simpler for the downstream statisticians to handle the file without spurious text!

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: Trapping and using keypresses

Post by Simon » Mon Jun 24, 2013 8:14 am

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.
Attachments
2013-06-24_0010.png
This is what I'm seeing
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

astacus
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 7
Joined: Sat Jan 09, 2010 9:11 pm

Re: Trapping and using keypresses

Post by astacus » Mon Jun 24, 2013 7:51 pm

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

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Contact:

Re: Trapping and using keypresses

Post by mwieder » Mon Jun 24, 2013 8:04 pm

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.

astacus
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 7
Joined: Sat Jan 09, 2010 9:11 pm

Re: Trapping and using keypresses

Post by astacus » Mon Jun 24, 2013 9:56 pm

Ahhhhh.... (lightbulb moment!)
Checking it now :-)
Thanks

astacus
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 7
Joined: Sat Jan 09, 2010 9:11 pm

Re: Trapping and using keypresses

Post by astacus » Mon Jun 24, 2013 10:13 pm

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

Post Reply