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 Interactions with Javascript 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 eventsFull documentation on the MDN site Keyboard events are
For keyboard events, the |
altKey
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) |
key
A string for the key pressed: - the character if the key produces a character - a string that describes the key for special keys (eg "Control" for the Ctrl key) |
ExamplePut the cursor in the input boxes below and hit keys on the keyboard
Codefrom browser import bind, document def keyevent(ev): trace = document["traceKey"] trace.text = f"type: {ev.type}, key: {ev.key}" ev.stopPropagation() document["key_keydown"].bind("keydown", keyevent) document["key_keypress"].bind("keypress", keyevent) document["key_keyup"].bind("keyup", keyevent) |
ctrlKey
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.
|
|
code
a string that describes the physical keyboard touch that is hit this value is the same regardless of the character produced when hitting the key: for instance on an AZERTY keyboard, hitting key A will produce the code "KeyQ" |
ExamplePut the cursor in the entry fields below and hit random keys. with keydownwith keypress with keyup Codefrom browser import document def keyCode(ev): trace = document["traceKeyCode"] trace.text = f"event: {ev.type}, code: {ev.code}" ev.stopPropagation() document["codeKeydown"].bind("keydown", keyCode) document["codeKeypress"].bind("keypress", keyCode) document["codeKeyup"].bind("keyup", keyCode) |
|
shiftKey
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) |