key events not working

119 views
Skip to first unread message

Brandon Keith Biggs

unread,
Apr 4, 2015, 11:09:12 AM4/4/15
to bry...@googlegroups.com
Hello,
I can't get the keydown event to work. I don't get any errors and I don't see anything wrong with the code...
I also am wondering if there is an API for the event argument that is passed into the function for keydown?
How do I get the name of the key like 'f', or '3'?
Also, why not put keys into a key, mod structure like every other library with a keyboard event handler?
Here is my code:

from browser import document, html, alert

def presser(event):
    alert("Hit it")

document <= html.H1("This is text with the event bound to it", Id="h")

document['h'].bind('keydown',presser)

Pierre Quentel

unread,
Apr 6, 2015, 3:55:29 PM4/6/15
to bry...@googlegroups.com

keydown events usually make sense in an INPUT or TEXTAREA field, where you can get focus and type something. It can also be sent to the whole document, as you will see if you replace document['h'].bind(...) by document.bind(...). I'm not saying that it's impossible to bind keydown to an H1 element - I'm not sure - but I can't think of example where it makes sense in a web application. 

Joao S. O. Bueno

unread,
Apr 6, 2015, 3:58:08 PM4/6/15
to bry...@googlegroups.com
It makes all sense in the Universe if you are programing your own widgets.
HTML and browsers do allow it, and there is no reason whatsoever this
should be artifially limited in Brython.

Form the browser + javascript point of view, it is just an event on an
html element, just as
a text entry is an html entry. The later just happen to have a native handler.






>
> --
> You received this message because you are subscribed to the Google Groups
> "brython" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to brython+u...@googlegroups.com.
> To post to this group, send email to bry...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/brython/a94d7ad3-5603-4bfc-8912-6da59d45c87f%40googlegroups.com.
>
> For more options, visit https://groups.google.com/d/optout.

Pierre Quentel

unread,
Apr 7, 2015, 5:55:57 AM4/7/15
to bry...@googlegroups.com, gwi...@gmail.com

Can you give an example of binding "keydown" on a H1 element with Javascript  ?

, and there is no reason whatsoever this
should be artifially limited in Brython.

Form the browser + javascript point of view, it is just an event on an
html element, just as
a text entry is an html entry. The later just happen to have a native handler.






>
> --
> You received this message because you are subscribed to the Google Groups
> "brython" group.
> To unsubscribe from this group and stop receiving emails from it, send an

Joao S. O. Bueno

unread,
Apr 7, 2015, 9:04:45 AM4/7/15
to Pierre Quentel, bry...@googlegroups.com
>
> Can you give an example of binding "keydown" on a H1 element with Javascript
> ?
>

Ok - it does not work (as in: it really does nothing, as the browser
don't know an arbitrary element has focus
in order to dispatch keyboard events to it) - still, my point is that
there is no sense in
artificially restraining it. (And I don't think Brython does that -
but it should not anyway)

(I've used the "addEventListener" javascript method.)

Pierre Quentel

unread,
Apr 7, 2015, 9:54:43 AM4/7/15
to bry...@googlegroups.com, pierre....@gmail.com, gwi...@gmail.com

"bind" is an alias for "addEventListener" : Brython actually doesn't add any restriction on it

My point was just that it's normal that binding a keydown event to an element such as H1 does nothing, because "typing in a H1" doesn't mean anything, as opposed to typing in a TEXTAREA or an INPUT, or typing on the whole document, or to "clicking on a H1"

Reply all
Reply to author
Forward
0 new messages