KeyUp Problem

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

xfratboy
Posts: 97
Joined: Fri Mar 05, 2010 9:09 pm

KeyUp Problem

Post by xfratboy » Wed May 12, 2010 4:37 am

I'm trying to understand why keyUp doesn't work properly. I've got a simple little script that is supposed to increase the size of a graphic when the "I" key is released and decreases the size when the "D" key is released. The problem is that holding down the key without releasing executes the handler. So, I'm assuming that holding down theKey must buffer several of theKeys, thus telling Rev to execute the handler for each instance of the key. I'm not even sure how to explain this properly. How can I just execute something when the key is actually released, e.g. KeyUp? Currently this code will grow/shrink the graphic "ball" just by holding down the "I" or "D" keys. I only want the size of the graphic to increase/decrease when the key is released. I've tried both keyUp and KeyDown but both perform identical. Is there a better way to do this?

Code: Select all


on keyUp theKey 
   if theKey is m then 
      muteSound
      Else if theKey is i then
         increaseObjSize
         Else if theKey is d then
         DecreaseObjSize
   else pass keyUp
   end keyUp

on mutesound
   put the playloudness into myPlayLoudness
      if myPlayLoudness is 0 then set the playloudness to 100
      else 
      set the playloudness to 0
      end if
end mutesound

on increaseObjSize
      put the height of graphic ball into tempH
      put the width of graphic ball into tempW
      set the height of graphic ball to tempH +2
      set the width of graphic ball to tempW +2
      
end increaseObjSize

on DecreaseObjSize
   put the height of graphic ball into tempH
      put the width of graphic ball into tempW
      set the height of graphic ball to tempH -2
      set the width of graphic ball to tempW -2
      
end DecreaseObjSize
Thanks for any assistance.

Curry
Posts: 111
Joined: Mon Oct 15, 2007 11:34 pm
Contact:

Re: KeyUp Problem

Post by Curry » Wed May 12, 2010 7:17 am

Are you on Windows or Mac? See the Rev Dictionary note about keyUp on Mac. And maybe try keysDown.
Best wishes,

Curry Kenworthy

LiveCode Development, Training & Consulting
http://livecodeconsulting.com/

WordLib: Conquer MS Word & OpenOffice
SpreadLib: "Excel-lent" spreadsheet import/export
http://livecodeaddons.com/

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2729
Joined: Sat Dec 22, 2007 5:35 pm
Contact:

Re: KeyUp Problem

Post by jmburnod » Wed May 12, 2010 9:43 am

I think you can use the keydown message and not the keysdown function

Best

Jean-Marc
https://alternatic.ch

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

Re: KeyUp Problem

Post by dunbarx » Wed May 12, 2010 2:28 pm

I do not get this behavior at all. If I place a keyUp handler (in the card script) I only get an action when the key is released.

Code: Select all

on keyup tKey
   if tKey = "K" then 
      beep 3
      set the height of btn "B4" to random(55) + 33
   end if
end keyup
Mac, OS 10.4, Rev 4.0

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2729
Joined: Sat Dec 22, 2007 5:35 pm
Contact:

Re: KeyUp Problem

Post by jmburnod » Wed May 12, 2010 2:42 pm

Hi dunbarx

You can try this :

Code: Select all

on keydown tKey
   if tKey = "K" then
      beep 3
      set the height of btn "B4" to random(55) + 33
   end if
end keydown
best

Jean-Marc
https://alternatic.ch

xfratboy
Posts: 97
Joined: Fri Mar 05, 2010 9:09 pm

Re: KeyUp Problem

Post by xfratboy » Wed May 12, 2010 3:14 pm

I copied your code exactly. It doesn't work for me. If I hold down the K key I end up getting a long string of beeps. The longer I hold down the key the more beeps and resizes get qued-up. So the KeyUp is happening even though there's not been an actual "Up" of the key "K."

Same behavior on WinXP and Vista.

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

Re: KeyUp Problem

Post by Regulae » Wed May 12, 2010 4:26 pm

A way to ensure that the keyUp handler only responds when the key is lifted is to use the keysDown function. Thus:

Code: Select all

on keyUp theKey
   if the keysDown is empty then
      if theKey is "k" then
         beep 1
      end if
   end if
end keyUp
... should only respond when you take your finger off the “k” key.
Regards,

Michael

xfratboy
Posts: 97
Joined: Fri Mar 05, 2010 9:09 pm

Re: KeyUp Problem

Post by xfratboy » Wed May 12, 2010 5:22 pm

Perfect! Exactly what I was looking for. Thanks!

EDIT:
Well, not exactly working. I noticed that after "I" or "D" is pressed and released, that any special keys like Arrow, shift, return, cause the first element in the IF statement to be run.

Code: Select all

on keyUp theKey
   if the keysDown is empty then
      if theKey is "i" then
         set the height of graphic ball to (the height of graphic ball) +10
         set the width of graphic ball to (the width of graphic ball) +10
      end if
      if theKey is "d" then
         set the height of graphic ball to (the height of graphic ball) -10
         set the width of graphic ball to (the width of graphic ball) -10
      end if
   end if
end keyUp
Pressing for example the Up Arrow key will execute the first IF statement (provided the I or D key is pressed first).

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

Re: KeyUp Problem

Post by dunbarx » Wed May 12, 2010 11:08 pm

The keysDown function is usually used to test whether a keypress occurs while a handler is running. I am not implying that it is not useful as an additional filter in the workaround that Regulae suggests.

I put my handler in the card script. Did yours go there also?

I just wonder why the message works in my system as advertised (that keyUp operates, well, when the key is up) and xFratBoy cannot seem to make his behave at all. When entering text, or performing operations while entering text, the difference between keyDown and keyUp are important. But this is just a message sent in a very ordinary way. Is there a possible issue here? A bug?

Craig Newman

xfratboy
Posts: 97
Joined: Fri Mar 05, 2010 9:09 pm

Re: KeyUp Problem

Post by xfratboy » Thu May 13, 2010 1:27 am

I placed in the script of the card. Also tried it in the script of the stack. Same thing. It just bufferes up a bunch of k's and beeps forever. 3 different computers. All do the same thing. If you hold a key down it will execute the keyUp handler multiple times.

Curry
Posts: 111
Joined: Mon Oct 15, 2007 11:34 pm
Contact:

Re: KeyUp Problem

Post by Curry » Thu May 13, 2010 6:43 am

Mac or Windows?
Best wishes,

Curry Kenworthy

LiveCode Development, Training & Consulting
http://livecodeconsulting.com/

WordLib: Conquer MS Word & OpenOffice
SpreadLib: "Excel-lent" spreadsheet import/export
http://livecodeaddons.com/

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

Re: KeyUp Problem

Post by Regulae » Thu May 13, 2010 8:06 am

Just to check we’re chasing the same problem. As I understand it, xfratboy was finding that, if a key is held down, it repeats, and this was causing problems for his implementation. Normally when people type they tap the keys briefly, so a single keyDown/keyUp pair is generated. When pressed and held long enough, multiple keyDown/keyUps are generated. I’m testing under Windows XP, and there are systems settings under Settings:Keyboard Properties: Repeat delay, Repeat rate, which set how long the key must be pressed before it starts to repeat (repeat delay) and how quickly it repeats thereafter (repeat rate). Note these are distinct from the Rev properties repeatDelay and repeatRate, which govern the way scrollbars behave. On a Mac the keyUp is sent immediately after any keyDown handlers are completed (regardless of whether the key is actually released), whereas under Unix and Windows the key must actually be released. I’m not sure this affects the current problem of blocking key repeats, which while normal behaviour, is a problem in some cases. It’s natural for a user to hold down a key until they see it have the desired effect. When typing, this virtually instant. If the keypress is resizing an object and redrawing it on the screen, users can tend to hold the keys down long enough for them to repeat, generating unwanted messages. In this case, checking that the keysDown returns empty ensures that only the last keyUp, after the key is actually released, is registered by the handler.

The problem with special keys like the arrow keys falsely triggering the handler is interesting- the documentation says that these keys don’t generate a keyUp message, but indeed they seem to. The problem is avoided by using rawKeyUp instead, thus:

Code: Select all

on rawKeyUp theKey
   if the keysDown is empty then
      if theKey is 107 then
         beep 1
      end if
   end if
end rawKeyUp
... for the “k” key test, “i” being 105 and “d” being 100. It’s as if special keys generate keyUps, with theKey equivalent to “any” key. I could have missed the point of the original question, but we've definitely uncovered an oddity about keyUp.

Regards,

Michael

xfratboy
Posts: 97
Joined: Fri Mar 05, 2010 9:09 pm

Re: KeyUp Problem

Post by xfratboy » Thu May 13, 2010 1:58 pm

I appreciate your thoughtful response. You are understanding correctly. This is the problem on Windows XP and Vista. Actually my 3 year old daughter found the problem. I recently made a little stack for my kids to play around with changing colors and shapes and also make the shapes bounce around. I used the Up and Down arrows to control the speed of the bounce. Within a minute of her playing with it she had found all the bugs. I've never been able to figure out a workaround. So, in the actual scenario, the "I" key increases the size of the object, and the Up arrow increases the speed of the bounce of the object. I thought Regulae's solution was the answer but then then realized that that first adjusting the size with the "I" key then going to the arrow key continued to cause the "I" key feature to fire-off. Weird.

It seems that using the RawKeyUp might be problematic. Is the RawKey code likely to be different on every computer? In other words, is "105" the universal RawKey for "i" ?

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

Re: KeyUp Problem

Post by dunbarx » Thu May 13, 2010 3:22 pm

I am on a Mac, and the problem does not exist. But I thought the only handler in question was "keyUp". On a Mac, if you have a "keyDown" handler, the message is sent repeatedly if the key is held down, as Regulae points out.

I do not get a keyUp message with any arrow keys.

Craig Newman

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

Re: KeyUp Problem

Post by Regulae » Thu May 13, 2010 5:12 pm

There’s nothing like small children to uncover the bugs and limitations of a program! I did wonder myself about the universality of rawKeyDown/Up’s keyCodes. As far as I can tell, the keyCode seems to be the ASCII value of the key i.e. identical to the value returned by charToNum(). There are some differences between character sets, which affects characters typed with special keys such as Option and Alt, which the Rev dictionary mentions in the charToNum entry. Other than that, the ASCII seems standard across computers, so rawKeyUp should be suitable in general use. However, having just read Craig’s post, I’ve done some tests on a Mac, admittedly an old iMac running OS 9, Rev 2.6.1, and have seen for myself the differences. What I’m getting is this: on both XP and Mac, putting:

Code: Select all

on rawKeyUp theKey
      if theKey is 107 then
         beep 1
      end if
end rawKeyUp
... in the card script, and holding down the “k” key for a while, produces a string of beeps. Modifying the scripts with keysDown:

Code: Select all

on rawKeyUp theKey
   if the keysDown is empty then
      if theKey is 107 then
         beep 1
      end if
   end if
end rawKeyUp
... worked correctly on XP, but not on Mac- I think because of the order messages are sent on Mac, keysDown is never empty when the rawKeyUp is sent, so folks asking about the operating system were indeed on the money. I did test keyUp on the Mac. As Craig said, the Mac does not exhibit the strange behaviour we saw in XP with arrow keys and the like. What would be nice is to be able to temporarily reset the delay between the initial key press and when it starts to repeat. I’m not aware of such a property in Rev. I can only speak for what I have seen on the operating systems I have access to- it's great having people testing on other ones.
Regards,

Michael

Post Reply