Simplest way to add an event to a control

Hello,

I’m trying to add a “onkeypress” event to a text field.
I have this in the Shown event:
s=“document.getElementById(’”+me.ControlID+"’).onkeypress="“return FilterKey(event)”""+EndOfLine+_
“function FilterKey(event){”+_
“var a=(‘charCode’ in event) ? event.charCode:event.keyCode;”+_
“alert(a);”+_
“return (a!=58);”+_
“}”
ExecuteJavaScript(s)

This just does nothing (no error, not showing an alert, not executing). I’ve looked at examples in the WebSDK folder. The only examples that might work use a custom class; I don’t want this as the TextField is already an existing class (and it’s not written this is the only way).

I also tried with something like that:
ExecuteJavaScript("$(’#" + me.ControlID + “’).” …
and get “Can’t find variable: $”. For this, I read I should include jquery. Again, examples I found use a WebControlWrapper class while I have a TextField; the used RegisterLibrary function isn’t available outside of a WebControlWrapper, so I tried two things:
•In session.PrepareSession: me.ExecuteJavaScript("<script src="“http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js”">")
This gives a runtime error.
•In the HTMLHeader property of the app class: http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js
This shows the URL on the web page before the app loads but doesn’t resolve my problem.

I also tried keypress/keydown events, as some older pages in this forum suggest, instead of “onkeypress”, but this didn’t change anything (nothing gets executed). onkeypress seems to be the wanted way, however.

That’s it… I’ve found lots of confusions I’d like the Language Reference to explain, because the path is not clear (to me, at least)…

Am I close to the solution, or not at all?

[quote=466272:@Arnaud Nicolet]Hello,

I’m trying to add a “onkeypress” event to a text field.
I have this in the Shown event:
s=“document.getElementById(’”+me.ControlID+"’).onkeypress="“return FilterKey(event)”""+EndOfLine+_
“function FilterKey(event){”+_
“var a=(‘charCode’ in event) ? event.charCode:event.keyCode;”+_
“alert(a);”+_
“return (a!=58);”+_
“}”
ExecuteJavaScript(s)[/quote]

Arnaud, please, when you post code, select it and click the code icon on top of the editor. That will make it legible. As it stands, it is very difficult to read.

About onkeypress, you should know that the Xojo framework makes use of it, so it is not as simple as you may think.

What is wrong with the keypressed event ?

Sorry. Thanks for pointing this out. If I was able to edit my post (which I’m not…), I’d change it right away. Perhaps I should re-post it?

Ah, ok… So, when “nothing happens” in my tests, it’s actually because the event is already registered… Argh…

I can’t prevent keys from being entered. I want a file name to be entered, so chars like “:” should be prevented. Currently, I show an error label when invalid characters are detected, but I’d like to prevent it in the first place.
Thinking again about this, I now realise I won’t be able to catch EditPaste and text drops to really prevent invalid characters, so I start to understand why the KeyPressed event can’t be cancelled (it’s pointless to prevent typing a given character if other means can’t be cancelled as well).
Web programming is really different than desktop programming… Confusing…

But, for the record, could this be done at all? Does Xojo register events (TextChanged, Keypressed) if they aren’t used in code?

Thank you.

Arnaud,

I believe I posted precisely what you are after here:
https://forum.xojo.com/39730-restrict-entry-in-webtextfield-to-numbers/0

You will have to adapt my JS code, but it is relatively easy.

[quote=466359:@Michel Bujardet]Arnaud,

I believe I posted precisely what you are after here:
https://forum.xojo.com/39730-restrict-entry-in-webtextfield-to-numbers/0

You will have to adapt my JS code, but it is relatively easy.[/quote]
SetAttribute was the missing key. Thank you!