KeyDown only firing once

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
richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9359
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

KeyDown only firing once

Post by richmond62 » Thu Jul 15, 2021 11:53 am

What code can I use to stop multiple firing of a key when my finger gets stuck on it?

Klaus
Posts: 13824
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: KeyDown only firing once

Post by Klaus » Thu Jul 15, 2021 11:58 am

Maybe checking the last character in your field or wherever you type.
If it is the same as the currently pressed char...

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9359
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: KeyDown only firing once

Post by richmond62 » Thu Jul 15, 2021 12:03 pm

Not possible when the keyDown is being used for moving a graphic object round a stack in a game.

Klaus
Posts: 13824
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: KeyDown only firing once

Post by Klaus » Thu Jul 15, 2021 12:15 pm

True! :-)

Maybe you can post your script?

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2718
Joined: Sat Dec 22, 2007 5:35 pm
Location: Genève
Contact:

Re: KeyDown only firing once

Post by jmburnod » Thu Jul 15, 2021 12:20 pm

Hi friends,

If i understand correctly the goal, this script does the job

Code: Select all

local sStarttime,sPausetime
on opencard
   put the milliseconds into sStarttime
   put 500 into sPausetime
end opencard


on keydown pKey
   if tTime > (sStarttime + sPausetime) then
      put pkey after fld "fText"
   end if
   put the milliseconds into sStarttime
end keydown
Kind regards
Jean-Marc
https://alternatic.ch

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9359
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: KeyDown only firing once

Post by richmond62 » Thu Jul 15, 2021 1:10 pm

No, I am afraid that does not work:
-
Screen Shot 2021-07-15 at 3.07.59 PM.png
-
cardScript:

Code: Select all

on arrowKey AK
   put item 1 of the loc of grc "ANT" into LR
   put item 2 of the loc of grc "ANT" into UD
   switch AK
      case "up"
          set the backgroundpattern of grc "ANT" to the ID of img "A1.png"
         move grc "ANT" to LR,(UD-12)
         break
         case "down"
          set the backgroundpattern of grc "ANT" to the ID of img "A3.png"
         move grc "ANT" to LR,(UD+12)
         break
         case "left"
          set the backgroundpattern of grc "ANT" to the ID of img "A4.png"
         move grc "ANT" to (LR-12),UD
         break
         case "right"
          set the backgroundpattern of grc "ANT" to the ID of img "A2.png"
         move grc "ANT" to (LR+12),UD
         break
   end switch
   if intersect(grc"ANT",img"maze.png",4) then
      send "mouseUp" to btn "RESTART"
   end if
   if intersect(grc"ANT",img"block.png",4) then
      send "mouseUp" to btn "RESTART"
   end if
   --------
   if intersect(grc"ANT",img"f1",4) then
      add 1 to fld "SCORE"
      set the vis of img "f1" to false
      set the loc of img "f1" to -100,-100
   end if
   if intersect(grc"ANT",img"f2",4) then
      add 1 to fld "SCORE"
      set the vis of img "f2" to false
      set the loc of img "f2" to -100,-100
   end if
   if intersect(grc"ANT",img"f3",4) then
      add 1 to fld "SCORE"
      set the vis of img "f3" to false
      set the loc of img "f3" to -100,-100
   end if
   if intersect(grc"ANT",img"f4",4) then
      add 1 to fld "SCORE"
      set the vis of img "f4" to false
      set the loc of img "f4" to -100,-100
   end if
   if intersect(grc"ANT",img"f5",4) then
      add 1 to fld "SCORE"
      set the vis of img "f5" to false
      set the loc of img "f5" to -100,-100
   end if
   if intersect(grc"ANT",img"f6",4) then
      add 1 to fld "SCORE"
      set the vis of img "f6" to false
      set the loc of img "f6" to -100,-100
   end if
   if intersect(grc"ANT",img"f7",4) then
      add 1 to fld "SCORE"
      set the vis of img "f7" to false
      set the loc of img "f7" to -100,-100
   end if
   if intersect(grc"ANT",img"FINISH",4) then
      set the vis of img "bravo" to true
   end if
end arrowKey
A lot of that 'guff' after the switch statement is for the 'ant' to 'eat' things.
Attachments
Ant Maze END.livecode 2.zip
Here's the stack.
(197.97 KiB) Downloaded 97 times

Klaus
Posts: 13824
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: KeyDown only firing once

Post by Klaus » Thu Jul 15, 2021 1:12 pm

AHA, and where is the KEYDOWN handler in question?

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

Re: KeyDown only firing once

Post by dunbarx » Thu Jul 15, 2021 1:34 pm

Can you use "keyUp" to do what you want? That method cannot get "stuck"

Craig

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9359
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: KeyDown only firing once

Post by richmond62 » Thu Jul 15, 2021 1:46 pm

Ouch! That's simple.

BUT, better to use rawKeyUp as one can trap the arrowKeys that way.

UP = 65362
LEFT = 65361
RIGHT = 65363
DOWN = 65364

Thunder
Posts: 20
Joined: Thu Mar 12, 2015 1:52 pm

Re: KeyDown only firing once

Post by Thunder » Thu Jul 15, 2021 4:44 pm

Hi !

Maybe the flushEvents can be of some help too..?

flushEvents : "Clears pending events from the event queue so they will not trigger handlers.!

Sincere
Thunder.

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9359
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: KeyDown only firing once

Post by richmond62 » Thu Jul 15, 2021 7:32 pm

flushEvents

Quite possibly, but not for 9-14 year olds who have been programming for less than 2 weeks.

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”