[Greasemonkey] Filtering keypress w/ event handler DNW (for me)

Vectorspace vectorspace at ntlworld.com
Wed Apr 19 22:51:31 EDT 2006


keyPress(evnt) is called on a  keypress event in the text box, but AFAIK 
it does not block or in any way effect the character from being 
registered by the text box - it is just code that is executed whenever 
you type a character.
A new idea: If the new character is outside the desired range, then 
actively remove the last character from the text box.

I don't know that much about events, so try Anthony Lieuallen's 
suggestion first.

Philip Friedman - Auto Europe wrote:
> I tried adding key press filtering to an inserted input field, but returning
> false seems to be ignored, the character is added to the input field anyway.
> Here's what I tried:
>
>   function keyPress(evnt) {
>     var charCode = evnt.charCode;
>     var rslt = false;
>     if (0==charCode || 46==charCode || (47<charCode && charCode<58)) {
>       rslt = true;
>     }
>     GM_log('charCode: '+charCode+' -> '+rslt);
>     return rslt;
>   }
>
>   var inp = document.createElement('input');
>   inp.size = '22';
>   inp.type = 'text';  
>   inp.addEventListener('keypress', keyPress, true);
>   document.getElementsByTagName('body')[0].appendChild(inp);
>
> Any suggestions on how to filter the characters that can be entered into an
> input field?
> I tried with the addEventListner capture argument as both true and false.
> Thanks.
>
> --
> Regards, Phil Friedman
> _______________________________________________
> Greasemonkey mailing list
> Greasemonkey at mozdev.org
> http://mozdev.org/mailman/listinfo/greasemonkey
>
>   


More information about the Greasemonkey mailing list