Trapping key presses outside fields

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
clatimer
Posts: 4
Joined: Sun Mar 14, 2010 9:56 am

Trapping key presses outside fields

Post by clatimer » Sun Mar 14, 2010 10:19 am

I am trying to capture a key press after a card opens. There is no field on the card - just an image - and I am interested in how long it takes for someone to press a key after the card opens. It's a psychological experiment. I've tried the various handlers for key presses, but they seem to function only for key presses within fields - I could be wrong. Any suggestions greatly appreciated.
Thanks
Cyril

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Re: Trapping key presses outside fields

Post by Mark » Sun Mar 14, 2010 10:25 am

Hi Cyril,

Probably, you want to use the rawKeyDown or rawKeyUp message.

Best,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

clatimer
Posts: 4
Joined: Sun Mar 14, 2010 9:56 am

Re: Trapping key presses outside fields

Post by clatimer » Sun Mar 14, 2010 11:00 am

Thanks Mark,

Yes, I've tried rawKeyDown without success. Even the key relevant sample scripts from the Manual don't work.

Cheers
Cyril

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Re: Trapping key presses outside fields

Post by Mark » Sun Mar 14, 2010 11:15 am

Cyril,

I assure you that those handlers work in fields, cards and stacks.

Code: Select all

on rawkeyup
     beep
     pass rawkeyup
end rawkeyup
Best,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

Regulae
Posts: 136
Joined: Tue Oct 20, 2009 6:05 am

Re: Trapping key presses outside fields

Post by Regulae » Sun Mar 14, 2010 11:33 am

I’ve found there’s a slight trick when dealing with images. rawKeyDown is sent to the control with the “focus”, i.e. its traversalOn property is set to true. You can get an image to respond to rawKeyDown/Up by:

Code: Select all

set the traversalOn of image “MyImageName” to true
... either in the message box or in a handler. The Property Inspector for images doesn’t let you set this directly, but I’ve found it can be set. What may work better for you is to put the rawKeyDown/Up handler in the card, or stack, script, especially if you are navigating from card to card. You could try the following in the stack script:

Code: Select all

global openTime, measureTime

on openCard
   put "Yes" into measureTime
   put the milliseconds into openTime
   pass opencard
end openCard

on rawKeyDown
   if measureTime = "Yes" then
      put the milliseconds into pressTime
      put pressTime - openTime
      put "No" into measureTime
   end if
   pass rawKeyDown
end rawKeyDown
... this should show the duration in milliseconds between the openCard and the next keypress in the message box. There is no need to set the traversalOn for the individual images in this case.

Regards,

Michael

clatimer
Posts: 4
Joined: Sun Mar 14, 2010 9:56 am

Re: Trapping key presses outside fields

Post by clatimer » Mon Mar 15, 2010 12:40 am

Mark wrote:Cyril,

I assure you that those handlers work in fields, cards and stacks.

Code: Select all

on rawkeyup
     beep
     pass rawkeyup
end rawkeyup
Best,

Mark
Thanks Mark, you are of course right.
Cyril

clatimer
Posts: 4
Joined: Sun Mar 14, 2010 9:56 am

Re: Trapping key presses outside fields

Post by clatimer » Mon Mar 15, 2010 12:57 am

Code: Select all

global openTime, measureTime

on openCard
   put "Yes" into measureTime
   put the milliseconds into openTime
   pass opencard
end openCard

on rawKeyDown
   if measureTime = "Yes" then
      put the milliseconds into pressTime
      put pressTime - openTime
      put "No" into measureTime
   end if
   pass rawKeyDown
end rawKeyDown
... this should show the duration in milliseconds between the openCard and the next keypress in the message box. There is no need to set the traversalOn for the individual images in this case.

Regards,

Michael[/quote]

Thanks Michael, Your code works a treat! Problem solved. Looking at yours and Mark's code, one of my problems may have been not passing rawKeyDown.
Cheers
Cyril

Post Reply