16 Replies Latest reply on Nov 28, 2010 3:47 PM by Hosseinrostamzadeh

    How to set input-filter to a text field?

    Hosseinrostamzadeh

      I use a  text input(a flash-component ) which the user will enter  only numbers in it (both positiv and negative integers and floating point numbers) but not  any other symbols.

      This code


        on keyDown me

      if ("1234567890.-") contains the key or the key = backSpace or the key = Tab or the keyCode =123 or the keyCode = 124 then
          pass
        else
          stopEvent
        end if
       
      end

       

      This script works  properly when I use it with old style text Fields in director.It does  not however work at all with new style flash component(text input).

       

      How can this be fixed?

      Thanks  in advance for any help.

      HR


        • 1. Re: How to set input-filter to a text field?
          Production Monkey Level 3

          Is there some reason you do not want to use a Text Member?  Your code will work on text members.

          • 2. Re: How to set input-filter to a text field?
            Hosseinrostamzadeh Level 1

            Yes it works fine on text member(old style director text).

            The only reason is the better "look" of the new input-text(flash component) and because I have hundreds of them in an exam-generating application.And I wanted to fix this property to them rather than replacing them all with old-style text members.

             

            I have used these input fields in flash CS5 and there everything is fixed by putting something like

            TEXTINPUT.restrict("0,1,2,3,4,5,6,7,8,9\\.\\+\\-") written in Asction Scpript 2

             

            When these elements now  are integrated in director, I think there must be a lingo version of the same code.

            So I am hopefull of finding a solution, although I have searched many resources without a result.

            Thanks for help/sharing your idea.

            Best regards!
            HR

            • 3. Re: How to set input-filter to a text field?
              Production Monkey Level 3

               

              You can set the "restrict" property in the "Flash Component" tab of the Property inspector.  I just typed in "-0123456789.+" and it seems to work fine. Also if you select all the Flash TextInput members in your library at one time, then when you set the property they will all be set with the same value. No need to do each one individually.

              1 person found this helpful
              • 4. Re: How to set input-filter to a text field?
                Hosseinrostamzadeh Level 1

                Thanks for great help!

                But I checked it it restricts all other symbols the exception of "," comma

                It is strange that it does not stops entering comma. I want the users not be able to enter comma" , " at all!

                 

                This is important for my application because it makes reading decimal numbers confusing.

                In Norway(where I live) students use comma for desimal numbers rather than period . when they write numbers, while lingo-based math operations understand decimal numbers by period . for example 2.3 and  not 2,3

                I don't know if you checked in director 11.5 or older versions?

                Dose your restrict(..........) works for filtering out ","

                Best regards!

                HR

                • 5. Re: How to set input-filter to a text field?
                  Production Monkey Level 3

                  I just tried it on D11.5.8 and D10.1.1 and you are correct. The comma is not restricted.

                   

                  The only hack I can think of is to catch keyDown events globally and stop the input of commas or other keys at that point.

                   

                  I tested the following in D10 and it works.

                   

                  on preparemovie
                    the keyDownScript = "chatchKeys()"
                  end

                  on stopMovie
                    the keyDownScript = ""
                  end

                  on chatchKeys
                    if _key.key = "," then
                      stopEvent()
                    else
                      pass
                    end if
                  end chatchKey

                  1 person found this helpful
                  • 6. Re: How to set input-filter to a text field?
                    Hosseinrostamzadeh Level 1

                    Thanks again! It works fine now!

                     

                    But it restricts comma in all text fields in the whole movie.

                    I need to let user write comma in some of text fileds(where they just enter explanation for exercises they solve, or where they have to  write simply free text)

                    Is it possible to specify which field to restrict the wanted symbol rather than any filed in the movie?

                    Best regards!

                    HR

                    • 7. Re: How to set input-filter to a text field?
                      Production Monkey Level 3

                      First I should mention that I avoid using Flash inside Director, so there may be a better way.

                       

                      As you noted, catching keyDown events globally affects all keyboard input. If you want to specify a subset of sprites then you need a system to manage that. The simplest would be a global list that you populate with restricted sprite numbers when going to a new frame.

                       

                      global gRestrictedInputSprites

                       

                      on preparemovie
                        gRestrictedInputSprites = [1, 5, 6, 23] -- sample data
                        the keyDownScript = "chatchKeys()"
                      end

                       


                      on stopMovie
                        the keyDownScript = ""
                      end

                       


                      on chatchKeys
                        if gRestrictedInputSprites.getPos(_movie.keyboardFocusSprite) then
                          if _key.key = "," then
                            stopEvent()
                          else
                            pass
                          end if
                        else  -- no global restrictions on input
                          pass
                        end if
                      end chatchKey

                       


                      And then add a beginsprite to your Framescript to set the values of gRestrictedInputSprites for each frame label you loop on - assuming you pause the frame head with "go the frame".


                      global gRestrictedInputSprites

                       

                      on beginSprite me
                        gRestrictedInputSprites = []  -- list all input sprites that need input restrictions for this frame.
                      end

                       

                      on exitFrame me
                        go the frame
                      end

                       

                      If you go this route then I suggest using a GPDL to define the restricted sprite lists. No need to write more than one behavior.

                      • 8. Re: How to set input-filter to a text field?
                        Hosseinrostamzadeh Level 1

                        Thanks for your time, and for quick and effective responses!

                        I will try the last suggestion after a while!

                        Best regards!

                        HR

                        • 9. Re: How to set input-filter to a text field?
                          Production Monkey Level 3

                          For fun I wrote a generalized solution. It consists of a global object - script instance - that manages keyboard input, and a behavior that you attach to the Flash TextInput sprites that require additional filtering. The advantage of this approach is that each sprite can have a unique set of filtered input, a single behavior, and you can monitor the global object in the Object inspector which is helpful for debugging.

                           

                          Attach the following behavior to any Flash TextInput sprites that require filtering. The default filter is just a comma, but you can type in anything you want.

                           

                          -- Flash TextInput Component Keyboard Filter Behavior

                           

                          --  Filters keyboard input in addition to what the "restrict" property of the flash component does.
                          --  Used in conjunction with the "Keyboard Filter" Parent script.

                           

                          property  pMe  -- this sprite
                          property  pKeys  -- String:  list of characters you want blocked
                          property  pInputFilter  -- internal reference to instance of the "Keyboard Filter" script.

                           


                          On GetBehaviorDescription me
                            return "Filters keyboard input."
                          end GetBehaviorDescription

                           


                          On GetPropertyDescriptionList me
                            Props = [:]
                            Props[#pKeys] = [#default:",", #format:#string,  #comment:"List of key(s) to filter out."]
                            return Props
                          end GetPropertyDescriptionList

                           


                          on beginSprite me
                            pMe = sprite(me.spriteNum)
                            pInputFilter = script("Keyboard Filter").new()  -- even though we call "new" only one instance is ever created.
                            pInputFilter.filterInput(pMe, pKeys)  -- tell pInputFilter to filter input for this sprite
                          end beginSprite

                           


                          on endSprite me
                            pInputFilter.removeFilter(pMe)  -- this sprite is going away, so tell pInputFilter to stop filter it's input
                          end endSprite

                           

                           

                          The following is a parent script that does all the work. Paste the code into a Parent script named "Keyboard Filter".

                           

                          -- Keyboard Filter
                          -- Name this Parent script "Keyboard Filter"

                           

                          global gKeyFilter  -- singleton reference to an instance of this script

                           

                          property  pFilteredSprites  -- Property list of sprites and their associated list of keys to filter

                           

                          ------------------------------------------------------------------------------------------ --------
                          -- PUBLIC METHODS
                          ------------------------------------------------------------------------------------------ --------

                           

                          on new me
                            -- force singleton pattern of only allowing one instance of this script.
                            if gKeyFilter.voidP then
                              gKeyFilter = me
                              the keyDownScript = "gKeyFilter.keyDown()"
                              pFilteredSprites = [:]
                            end if
                           
                            return gKeyFilter  -- force singleton pattern
                          end new

                           


                          on destruct me
                            the keyDownScript = ""
                            gKeyFilter = void
                          end destruct

                           


                          on filterInput me, aSprite, KeyList 
                            pFilteredSprites[String(aSprite)] = KeyList
                          end filterInput

                           


                          on removeFilter me, aSprite
                            pFilteredSprites.deleteProp(String(aSprite))
                          end allowInput

                           


                          ------------------------------------------------------------------------------------------ --------
                          -- PRIVATE METHODS
                          ------------------------------------------------------------------------------------------ --------

                           

                          on keyDown me
                            InputSprite = "(sprite " & _movie.keyboardFocusSprite & ")"  -- need string version of current sprite that has keyboard focus.
                            Keys = pFilteredSprites[InputSprite]  -- get list of keys to filter for this sprite. May be void.
                           
                            -- if key list is void then there is no filtering for this sprite.
                            if Keys.voidP then
                              pass
                              exit
                            end if
                           
                            -- filter input for this sprite
                            if Keys contains _key.key then 
                              stopEvent()
                            else
                              pass
                            end if  
                          end keyDown

                           

                           

                          There are a couple caveats:  If you want to filter input such as the "Home" key or "Return" etc. then the whole thing needs a rewrite to work with key codes. Second, you should add the following "stopMovie" code to clean up memory.

                           

                          on stopMovie
                            script("Keyboard Filter").destruct()  -- removes this object from memory and releases keyboard control
                          end

                          • 10. Re: How to set input-filter to a text field?
                            Hosseinrostamzadeh Level 1

                            I tried  the whole code you gave.

                            I attached the behavior to one, two flah-text input field and put  the parent script as a separate script and every thing went seemingly without any problem.

                             

                            When I thouch a flash-member text input,before I can write anything I get this  error saying:

                            script error

                            OBJECT EXPECTED

                            gKeyFilter.keyDown()

                             

                             

                            and then the movie stops. I don't know where to fix, nor how to fix it.

                            Have you tried it inside a movie?

                            thanks in advance!

                            Best regards!

                            HR

                            • 11. Re: How to set input-filter to a text field?
                              Production Monkey Level 3

                              I tested the code before posting. Select the Parent script and then the "Script" tab in the property inspector. Make sure the "Type" property is set to "Parent".

                              • 12. Re: How to set input-filter to a text field?
                                Hosseinrostamzadeh Level 1

                                I see how an elegant solution you have created!

                                I tested it in a new Movie it works fine and filter out comma, which is default. I tested for other keys(symbols) worked fine too.

                                But in my existing movie it does not work,with same error as previous post.

                                I checked every thing and all seems correct( parent-type etc.)

                                Why should this error happen?

                                Worse is that when I script error-dialog box SCRIPT it does not even go to the script such that I can debug from there its says it expects object!

                                As I Told the solution is excellent. And if I get it works inside an existing movie then the problem is solved.

                                Best regards!

                                HR

                                • 13. Re: How to set input-filter to a text field?
                                  Production Monkey Level 3

                                  The object "gKeyFilter" is being deleted somehow. Do a search for "clearGlobals" in your scripts. Delete any clearGlobals statements. In my opinion that statement should never be used in code. I only use it in the Message window.

                                  • 14. Re: How to set input-filter to a text field?
                                    Hosseinrostamzadeh Level 1

                                    I had a clearGlobals statement that I have removed now.

                                    The filtering works now. BUT it seems that it causes a strange "side effect".

                                    When I point with the mouse to a flash-input field (which has filtering drop-down scripit from you), then the field is being blocked for further entering of every thing.

                                    I mean I can not enter anything at all. It seems that the editable property of the fields blocks when I point with mouse in order to write something in it.

                                    When I user tab-key then it suddenly accepts  input from keyboard.

                                    conclusion: the filtering works but the field being blocked if I use mouse to point to it.

                                    Any idea?

                                    I can send you the movie itself if I had your e-mail.

                                    Best regards!

                                    HR

                                    • 15. Re: How to set input-filter to a text field?
                                      Production Monkey Level 3

                                      Clicking on a field with the mouse works in my test program. There is something going on that is particular to your program. Are there other behaviors attached to the sprites?  A global mouseDownScript?  I'm not sure what would cause that problem.

                                       

                                      Sorry, I don't give out my email address.

                                      • 16. Re: How to set input-filter to a text field?
                                        Hosseinrostamzadeh Level 1

                                        I checked everything,seemingly nor problems. No behaviors attached to these flash fields.

                                        I checked other versions of my last movie(where there is no sign of your suggested code), Strangely enough I find the same blocking problem again for flash-input text fields! clicking with mouse cause them to be no longer editable!

                                         

                                        Maybe it is the time to give these buggy flash-members in the whole movie( I should listen to you far earlier! )

                                        As a rule of own, I will give these problem a last try  tomorrow.( too late here in Norway, time is 00.46) Then I have to leave the whole flash-members if the problem still exists.

                                        I understand private-rules with regards to your email.

                                        I have already used so much of your time, which I greatly appreciate!

                                        Thanks for alle your time used on my problems.

                                        Best regards!
                                        HR