IntroductionInstallationLimitations of the "file" protocolFrequently asked questionsSyntax, keywords and built-in functionsStandard distributionimport implementationBrython packagesBrowser interface
Introduction - DOM API
Creating a document Accessing elements Attributes, properties and methods Events Mouse events Keyboard events Focus events Drag events Query string Using Javascript objects and libraries Brython-specific built-in modules
browser
browser.aio browser.ajax browser.html browser.local_storage browser.markdown browser.object_storage browser.session_storage browser.svg browser.template browser.timer browser.webcomponent browser.websocket browser.worker Widgets browser.widgets.dialog browser.widgets.menu interpreter javascript Working with BrythonCookbook |
Keyboard eventsKeyboard events are
For keyboard events, the |
altKey
This attribute is not set for the input event It is usually used with keypress, to be able to test if Alt+<key> was entered, or just <key> |
ExampleEnter text in the field below, with or without pressing the Alt key
Codefrom browser import document # the entry field has the id "altKey" def keypress(ev): document["traceAltKey"].text = f"altKey: {ev.altKey}" document["altKey"].bind("keypress", keypress) |
charCode
The Unicode reference number of the key This attribute is used only by the keypress event It is set to a different value if the Shift key is pressed or not |
ExampleEnter text in the entry below. Note that the character can be read bych(ev.charCode)
Codefrom browser import document def charCode(ev): trace = document["traceCharCode"] char = chr(ev.charCode) trace.text = f"charCode: {ev.charCode}, character: {char}" document["charCode"].bind("keypress", charCode) |
||
ctrlKey
This attribute is not set for the input event It is usually used with keypress, to be able to test if Ctrl+<key> was entered, or just <key> |
ExampleEnter text in the field below, with or without pressing the Ctrl keyCodefrom browser import document def keypress(ev): trace = document["traceCtrlKey"] trace.text = f"ctrlKey : {ev.ctrlKey}" ev.preventDefault() document["ctrlKey"].bind("keypress", keypress)Note that ev.preventDefault() is used to avoid the default behaviour of
some keyboard shortcuts using the Ctrl key.
|
||||
keyCode
A system and implementation dependent numerical code identifying the unmodified value of the pressed key The value doesn't change if the keys Alt, Ctrl or Shift are pressed Note that the result is not the same depending on the handled events keydown, keyup or keypress |
ExampleEnter text in the entry fields below. Note that the character can be read bych(ev.charCode) with the keypress event
with keydown
with keypress with keyup Codefrom browser import document def keyCode(ev): trace = document["traceKeyCode"] trace.text = f"event {ev.type}, keyCode : {ev.keyCode}" ev.stopPropagation() document["keyCodeKeydown"].bind("keydown", keyCode) document["keyCodeKeypress"].bind("keypress", keyCode) document["keyCodeKeyup"].bind("keyup", keyCode) |
||||
shiftKey
This attribute is not set for the input event It is usually used with keypress, to be able to test if Shift+<key> was entered, or just <key> |
ExampleEnter text in the field below, with or without pressing the Shift keyCodefrom browser import document def keypress(ev): trace = document["traceShiftKey"] trace.text = f"shiftKey : {ev.shiftKey}" document["shiftKey"].bind("keypress", keypress) |
||||
which
A system and implementation dependent numeric code identifying the unmodified value of the pressed key Note that the result is not the same depending on the handled events keydown, keyup or keypress |
ExampleEnter text in the entry below. Note that the character can be read bychr(ev.which) for the keypress event
|