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

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

Re: KeyUp Problem

Post by Curry » Thu May 13, 2010 9:13 pm

I think you can still do this for Mac by sending a message to check on the key state a bit afterwards. There's no rule that says you have to check only at the moment of the event you're handling! :)
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 » Fri May 14, 2010 7:43 am

I did try this on the Mac:

Code: Select all

global messageID

on rawKeyUp theKey
   if theKey = 107 then
      cancel messageID
      send "KKey" to me in 150 milliseconds
      put the result into messageID
   end if
end rawKeyUp

on KKey
   if the keysDown is empty then
      beep 1
   end if
end KKey
... which works on both Mac and Windows, but I was hoping to avoid the delay. I found on my old Mac the delay took some trial and error- obviously we want to minimise it, but less than about 150 ms and brief single taps of the key could be missed (keysDown was still returning a value). Without cancelling messageID, an extended keypress can leave two messages “in the pipeline”, thus two beeps are produced. The problem the delay introduces is that it can “screen out” quickly repeated key taps, which might prove problematic for xfratboy’s implementation. Perhaps there is a better approach?

Regards,

Michael

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

Re: KeyUp Problem

Post by xfratboy » Fri May 14, 2010 2:41 pm

Thanks for the helpful suggestions and trials. Not sure I've found a workable cross-platform solution for the problem of my 3 year old's tendency to hold a key down too long.

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

Re: KeyUp Problem

Post by Regulae » Fri May 14, 2010 4:22 pm

The following may be a cross-platform solution for you. It uses rawKeyUp, which passes the ASCII code of the key, and is thus the same, for our purposes, on each platform. I’ve tested the following on Windows XP and Mac OS 9, and on both it blocks key repeats due to the key being held down, but also detects repeated rapid key presses (which was a problem with my earlier effort):

Code: Select all

global iKeyPressed, dKeyPressed, displayCount

on rawKeyUp theKey
   if displayCount is empty then
      put 0 into displayCount
   end if
   
   if theKey = 105 then
      if iKeyPressed <> "yes" then
         put "yes" into iKeyPressed
         send "IKey" to me in 2 milliseconds
      end if
   end if
   if theKey = 100 then
      if dKeyPressed <> "yes" then
         put "yes" into dKeyPressed
         send "DKey" to me in 2 milliseconds
      end if
   end if
end rawKeyUp

on IKey
   if the keysDown is empty then
      put "no" into iKeyPressed
      put displayCount + 1 into displayCount
      put displayCOunt into card field "Test"
      exit IKey
   end if
   send "IKey" to me in 2 milliseconds
end IKey

on DKey
   if the keysDown is empty then
      put "no" into dKeyPressed
      put displayCount - 1 into displayCount
      put displayCOunt into card field "Test"
      exit DKey
   end if
   send "DKey" to me in 2 milliseconds
end DKey
The script was tested in the card script, and requires a card field “Test” to be present on the card, so you can see the output. When, for example, the “i” key is pressed, the variable iKeyPressed is set to “yes”, which blocks repeats of the key until reset. This is done by the handler IKey, which you will see is called, and then calls itself repeatedly at 2 ms intervals until keysDown returns empty. In both Windows and Mac, this will be when the key is actually released. The periodic testing of keysDown accounts for the different way Windows and Mac generate rawKeyDown/Up pairs. The short interval of 2 ms means that the timing is virtually the same on both platforms. Of course, the timing can be adjusted. What you should see is, in card field “Test”, the key presses of the “i” key counting up, and the “d” key counting down.

Edit: Instead of rawKeyUp, the script works equally well with keyDown. keyDown does not exhibit the curious behaviour of keyUp in Windows, concerning keys like the arrow keys. I've tested keyDown on both platforms without problems. If making the change, remember to replace 105 with "i" and 100 with "d" in the script. A more streamlined version is thus:

Code: Select all

global keyPressed, displayCount

on keyDown theKey
   if displayCount is empty then
      put 0 into displayCount
   end if
   
   if keyPressed <> "yes" then
      if theKey = "i" then
         put "yes" into keyPressed
         send "IKey" to me in 2 milliseconds
      end if
      if theKey = "d" then
         put "yes" into keyPressed
         send "DKey" to me in 2 milliseconds
      end if
   end if 
end keyDown

on IKey
   if the keysDown is empty then
      put "no" into keyPressed
      put displayCount + 1 into displayCount
      put displayCOunt into card field "Test"
      exit IKey
   end if
   send "IKey" to me in 2 milliseconds
end IKey

on DKey
   if the keysDown is empty then
      put "no" into keyPressed
      put displayCount - 1 into displayCount
      put displayCOunt into card field "Test"
      exit DKey
   end if
   send "DKey" to me in 2 milliseconds
end DKey
Regards,

Michael

Post Reply