PDA

View Full Version : Function: Filter Key Press for On Key Event


Worm
09-27-2006, 03:34 PM
Just a little function I threw together to filter allowed characters in a input box.


function FilterKeyPress(InputObject, e_Key, sAllowed)
sOrig = Input.GetText(InputObject)
sFilter = String.TrimLeft(sOrig, sAllowed)
sFilteredLeft = String.TrimLeft(sOrig, sFilter)
sFilteredRight = String.TrimRight(sFilteredLeft, sFilter)
Input.SetText(InputObject, sFilteredRight)

Input.SetSelection(InputObject, String.Length(Input.GetText(InputObject)) + 1, String.Length(Input.GetText(InputObject)))
end


Example calls (Should be in the On Key Event)

-- allow only numerics
FilterKeyPress(this, e_Key, "1234567890")



-- allow only Capital Letters
FilterKeyPress(this, e_Key, "ABCDEFGHIJKLMNOPQRSTUVWXYZ")

JimS
09-28-2006, 04:07 AM
Good one, thanks Worm. This looks like one that will probably come in handy some time. :yes

I don’t want to brag, but given enough time, I probably could have written a 30+ line function, that could do nearly the same thing. Additionally, mine would only have a couple of small, and rarely encountered bugs.

Maybe if I keep with this programming stuff long enough, I too can be lazy like that, and cram 30 + lines of code down to 8 or so. :lol

Then again, customers would probably put a higher value on my 8.4MB program, than they would on your 1.3MB program. And they should, because I can guarantee that it would take me longer to write my 8.4MB program, than you’d spend writing a 1.3MB program, even if your program had more features, and didn’t crash like mine does, on months with 31 days. :rolleyes

Thanks again Man!:yes

Intrigued
09-28-2006, 08:20 AM
This is why I also request for the addition into of AMS for checking patterns. (aka. 0-9, a-z, A-Z). Lua support this, we just need AMS to catch up via an Action for this basic but important, useful activity.

:yes

Lorne
09-28-2006, 10:28 AM
You mean like supporting regexp? It's built into lua, so it's already there in autoplay.

See string.find(), string.gfind(), etc. (http://www.lua.org/manual/5.0/manual.html#5.3)

Intrigued
09-28-2006, 12:55 PM
I did not have ultimate success with Patterns, it may have been my fault or?:

Patterns

A character class is used to represent a set of characters. The following combinations are allowed in describing a character class:

* x (where x is not one of the magic characters ^$()%.[]*+-?) --- represents the character x itself.
* . --- (a dot) represents all characters.
* %a --- represents all letters.
* %c --- represents all control characters.
* %d --- represents all digits.
* %l --- represents all lowercase letters.
* %p --- represents all punctuation characters.
* %s --- represents all space characters.
* %u --- represents all uppercase letters.
* %w --- represents all alphanumeric characters.
* %x --- represents all hexadecimal digits.
* %z --- represents the character with representation 0.
* %x (where x is any non-alphanumeric character) --- represents the character x. This is the standard way to escape the magic characters. Any punctuation character (even the non magic) can be preceded by a `%´ when used to represent itself in a pattern.

* [set] --- represents the class which is the union of all characters in set. A range of characters may be specified by separating the end characters of the range with a `-´. All classes %x described above may also be used as components in set. All other characters in set represent themselves. For example, [%w_] (or [_%w]) represents all alphanumeric characters plus the underscore, [0-7] represents the octal digits, and [0-7%l%-] represents the octal digits plus the lowercase letters plus the `-´ character.

The interaction between ranges and classes is not defined. Therefore, patterns like [%a-z] or [a-%%] have no meaning.

* [^set] --- represents the complement of set, where set is interpreted as above.

For all classes represented by single letters (%a, %c, etc.), the corresponding uppercase letter represents the complement of the class. For instance, %S represents all non-space characters.

The definitions of letter, space, and other character groups depend on the current locale. In particular, the class [a-z] may not be equivalent to %l. The second form should be preferred for portability.

A pattern item may be

* a single character class, which matches any single character in the class;
* a single character class followed by `*´, which matches 0 or more repetitions of characters in the class. These repetition items will always match the longest possible sequence;
* a single character class followed by `+´, which matches 1 or more repetitions of characters in the class. These repetition items will always match the longest possible sequence;
* a single character class followed by `-´, which also matches 0 or more repetitions of characters in the class. Unlike `*´, these repetition items will always match the shortest possible sequence;
* a single character class followed by `?´, which matches 0 or 1 occurrence of a character in the class;
* %n, for n between 1 and 9; such item matches a substring equal to the n-th captured string (see below);
* %bxy, where x and y are two distinct characters; such item matches strings that start with x, end with y, and where the x and y are balanced. This means that, if one reads the string from left to right, counting +1 for an x and -1 for a y, the ending y is the first y where the count reaches 0. For instance, the item %b() matches expressions with balanced parentheses.

A pattern is a sequence of pattern items. A `^´ at the beginning of a pattern anchors the match at the beginning of the subject string. A `$´ at the end of a pattern anchors the match at the end of the subject string. At other positions, `^´ and `$´ have no special meaning and represent themselves.

A pattern may contain sub-patterns enclosed in parentheses; they describe captures. When a match succeeds, the substrings of the subject string that match captures are stored (captured) for future use. Captures are numbered according to their left parentheses. For instance, in the pattern "(a*(.)%w(%s*))", the part of the string matching "a*(.)%w(%s*)" is stored as the first capture (and therefore has number 1); the character matching . is captured with number 2, and the part matching %s* has number 3.

As a special case, the empty capture () captures the current string position (a number). For instance, if we apply the pattern "()aa()" on the string "flaaap", there will be two captures: 3 and 5.

A pattern cannot contain embedded zeros. Use %z instead.

Lorne
09-28-2006, 03:39 PM
regexp patterns can be tricky to learn. That's why there isn't an IR action for it. It's built in, and it works, but if you don't already know how to do regexp searches...it's way beyond what we can reasonably support. :)

TJ_Tigger
09-28-2006, 03:47 PM
Find a good UNIX book. They usually have a chapter on how to use regular expressions. They come in quite handy.

Tigg

Intrigued
09-28-2006, 03:50 PM
regexp patterns can be tricky to learn. That's why there isn't an IR action for it. It's built in, and it works, but if you don't already know how to do regexp searches...it's way beyond what we can reasonably support. :)

Ah ha. We have stumped you guys/gals. I knew with some elbow grease, tissue paper, and three widgets it could be done.

;)

FoxLeader
08-02-2007, 09:39 AM
Hey! Thanks a lot Worm! I worked a long time to have something different, but with the same goal, to work, so I'll probably learn a lot from this!

Thanks again ;)
FoxLeader

crz
08-02-2007, 11:21 AM
does anybody knows how i can get a pagejump when
3 images are visable on a page

can someone please help me with this it's like i can't sleep untill i get it done
and i tried untill i fell in sleep on my keyboard
so please help me.......

thanx

mustafa06
08-02-2007, 11:37 AM
.apz pls :huh

Intrigued
08-02-2007, 06:47 PM
does anybody knows how i can get a pagejump when
3 images are visable on a page

can someone please help me with this it's like i can't sleep untill i get it done
and i tried untill i fell in sleep on my keyboard
so please help me.......

thanx

For this example do the following:

1. create a new empty project

2. add the following code to the "On Preload" of Page1:

Page.StartTimer(100)

3. add the following code to the "On Timer" of Page1:

if Image.IsVisible("Image1") and Image.IsVisible("Image2") and Image.IsVisible("Image3") then
Page.Jump("Page2")
end

4. add three Image objects to the canvas and make sure each have an image loaded into them.

5. add a second page to the project (will be named Page2 by default)

6. add a Lable object and set it's text to "Page 2"

7. click the F5 (Preview) toolbar button to view the project. It should page jump to Page2, right away.

crz
08-02-2007, 09:14 PM
thanx intrigued
i already thought that i asked for something
that was imposseble
i just got home so i'm going to try it out right away...

you really helped me out....

crz
08-02-2007, 09:25 PM
**** your good
i just tried it and it works perfectly
took me a minute with your help

rybosom
01-23-2008, 12:51 PM
this function is not working on AMS ver. 7.0 :huh
onKey I have FilterKeyPress(this, e_Key, "0123456789,-") and I can type only one digit in input, why?

holtgrewe
01-24-2008, 09:11 AM
rybosom

I had the same problem.
I placed the following code at the end of the function, and it works okay for me now.

function FilterKeyPress(InputObject, e_Key, sAllowed)
sOrig = Input.GetText(InputObject)
sFilter = String.TrimLeft(sOrig, sAllowed)
sFilteredLeft = String.TrimLeft(sOrig, sFilter)
sFilteredRight = String.TrimRight(sFilteredLeft, sFilter)
Input.SetText(InputObject, sFilteredRight)

Input.SetSelection(InputObject, String.Length(Input.GetText(InputObject)) + 1, String.Length(Input.GetText(InputObject)))
Input.SetSelection(InputObject, -1, 1)
end

Nice find, I use WORMS function a lot.

rybosom
01-24-2008, 09:52 AM
it works, thank you :yes

RizlaUK
01-25-2008, 05:54 AM
Nice find, I use WORMS function a lot.

Amen to that, i use this function a lot as well, although i havent run into this problem yet at least i'll know what to do when/if i do :yes