Ignoring unwanted rawKeyUp messages

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
kaveh1000
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 508
Joined: Sun Dec 18, 2011 7:23 pm
Location: London
Contact:

Ignoring unwanted rawKeyUp messages

Post by kaveh1000 » Sat Dec 02, 2017 8:39 pm

I am attaching a minimal file. There is one field with the following script:

=================
on arrowKey theKey
if theKey is "up" then
put the text of the target + 1 into the target
end if -- up
if theKey is "down" then
put the text of the target - 1 into the target
end if -- down
pass arrowKey
end arrowKey

on rawkeyup
wait for .1 sec
beep
end rawkeyup

on textchanged
-- do something
end textchanged
=================

the up and down arrow keys change the value of the field. What I want is that when I release the up or down key, the value is used to perform some other actions. But I don't want those actions to take place until I have released the arrow key.

I thought I would use the rawkeyup handler that would be sent when I released the key, but actually it seems that if I have the arrow key pressed, every rawkeydown has a corresponding rawkeydown. You can see this by noting that there are multiple beeps when the key is released.

I tried using

flushEvents

e.g.

put flushEvents("all") into rubbish

and

get flushevents

but no combination works.

Then I tried using textchanged, but this is only sent when a number is manually entered into a field. The change using arrows does not result in textchanged.

Any help welcome. Thanks.

Regards
Kaveh
Attachments
rawkeyup.livecode.zip
(1.01 KiB) Downloaded 194 times
Kaveh

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9580
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Ignoring unwanted rawKeyUp messages

Post by dunbarx » Sun Dec 03, 2017 1:33 am

Hi.

I have seen this in the use-list.

I posted a simple handler there that indicated I could not reproduce your issue. I could not as well with the sample stack you posted. i get only one beep. I could add a line calling any handler I wished. What version of LC? What OS?

I am in 8.1.6 on Mac OS 10.9.

Craig Newman

kaveh1000
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 508
Joined: Sun Dec 18, 2011 7:23 pm
Location: London
Contact:

Re: Ignoring unwanted rawKeyUp messages

Post by kaveh1000 » Sun Dec 03, 2017 9:46 am

Hi Craig

Thank you for your help and that of others on the use-list. And thanks for not ignoring this one!!

I am using Community Edition 9.0.0 DP 10 (build 15013) on Mac Sierra 10.12.6

I am getting multiple rawKey messages on the stack I uploaded. (Only one is heard when I don't have the 0.1 sec delay.)

Please see a screenshot of the relevant parts of message watcher attached, with multiple rawKeyUp being sent all together when arrow key is kept pressed.
Attachments
single key.png
single key.png (19.2 KiB) Viewed 5504 times
keeping key pressed.png
Kaveh

bogs
Posts: 5435
Joined: Sat Feb 25, 2017 10:45 pm

Re: Ignoring unwanted rawKeyUp messages

Post by bogs » Sun Dec 03, 2017 7:19 pm

Mind you, I am not suggesting this is a good way to go about it, but may be workable in your situation. The main problem with keyUp and keyDown messages is as you noted, because of the keyboard repeat rates, they come in pairs and have little to signify what is going on physically with your finger and the key.

In this thread (about moving a button), the same issue was hit, i.e. the image would flash back and forth due to kUp/kDown messages. My post here shows one way to work around it, in this case by putting value from the keyDown state into a temp local to store them, then using the temp local in a repeat loop to empty the value. You can set the beep message (or whatever else) when the value hits a certain level. It would look something like this (psuedo code)

Code: Select all

local tmpKeyDown

on arrowKey // could just as easily be rawKeyUp/Down...
// we are going to add in blocks to overcome the repeat rate...
   add 5 to tmpKeyDown // arbitrary number, change to suit...
end arrowKey

repeat with x=1 to tmpKeyDown
   if tmpKeyDown > 20 then // arbitrary number, change to suit...
// do your code for the key being down here ...
   else // stop the tmpKeyDown from being completely empty...
      beep // or whatever you want to happen
//    this number controls rate of tmpKeyDown emptying out
// and should be less than what goes in to allow tmpKeyDown to fill up a cushion...
      subtract 3 from tmpKeyDown 
   end if
end repeat    
Like I say, not a perfect solution, but pretty fast and adjustable way to overcome repeat rates of keyboards.

Good luck with it.
Image

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7215
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: Ignoring unwanted rawKeyUp messages

Post by jacque » Sun Dec 03, 2017 9:41 pm

This seems to work:

Code: Select all

local sKeyCount
local sStartSecs

on rawKeyDown pKey
  if the seconds - sStartSecs > 2 then -- reset after 2 seconds; adjust as desired
    put 0 into sStartSecs
    put 0 into sKeyCount
  end if
  if pKey = 65362 then -- up
    put the text of the target + 1 into the target
  else if pKey = 65364 then -- down
    put the text of the target - 1 into the target
  end if
  add 1 to sKeyCount
  wait 10 milliseconds -- just to slow it down; can remove or adjust
end rawKeyDown

on rawKeyUp pKey
  if sKeyCount = 1 then
    put "ACTION HERE"
  end if
end rawKeyUp
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

bogs
Posts: 5435
Joined: Sat Feb 25, 2017 10:45 pm

Re: Ignoring unwanted rawKeyUp messages

Post by bogs » Mon Dec 04, 2017 12:55 am

Nice, and I like how you made taking the key count into account. Key down overflow is bad, really really bad :D
Image

kaveh1000
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 508
Joined: Sun Dec 18, 2011 7:23 pm
Location: London
Contact:

Re: Ignoring unwanted rawKeyUp messages

Post by kaveh1000 » Mon Dec 04, 2017 9:53 am

Have not gone thro code to understand, but works. Thank you so much J. :-)
Kaveh

kaveh1000
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 508
Joined: Sun Dec 18, 2011 7:23 pm
Location: London
Contact:

Re: Ignoring unwanted rawKeyUp messages

Post by kaveh1000 » Mon Dec 04, 2017 8:21 pm

Wait. I might have spoken too soon. Please replace

Code: Select all

on rawKeyUp pKey
  if sKeyCount = 1 then
    put "ACTION HERE"
  end if
end rawKeyUp
by

Code: Select all

on rawKeyUp pKey
   if sKeyCount = 1 then
      wait .5 seconds
      beep
   end if
end rawKeyUp
I hear multiple beeps when I release. Should be one beep, no?
Kaveh

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7215
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: Ignoring unwanted rawKeyUp messages

Post by jacque » Mon Dec 04, 2017 11:00 pm

If there are many rawKeyUp messages in the queue, and you wait half a second between each, then there will be more than 2 seconds worth and the rawKeyDown handler will reset. If you want to test that way, increase the reset time to 5 or 10 seconds or even more, to accomodate any amount of queued rawKeyUp messages.

In my own tests, I used "put sKeyCount" to keep track instead of using beeps, because you need a wait period to hear all the beeps. I didn't want to add any waits because of the above.

Even better is probably to add flushEvents() into the rawKeyUp conditional. That should help with the queued message backup without relying on a time reset. Try this:

Code: Select all

on rawKeyUp pKey
  if sKeyCount = 1 then
    put "ACTION HERE" && sKeyCount
    get flushEvents("autoKey")
  else -- should never see this happen:
    put sKeyCount
  end if
end rawKeyUp
If that works, you can probably remove the time check at the top of the rawKeyDown handler.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

Post Reply

Return to “Getting Started with LiveCode - Experienced Developers”