diff options
author | Mitya Selivanov <automainint@guattari.tech> | 2025-01-08 02:18:43 +0100 |
---|---|---|
committer | Mitya Selivanov <automainint@guattari.tech> | 2025-01-08 02:18:43 +0100 |
commit | 917f5beee05a994852949b880ea47d7fab6f8cca (patch) | |
tree | 8f55c01abe71626b79dc96126f914bdf0f12163d /index.htm | |
parent | 91646cbafee3ff67baff52499529ac9077675239 (diff) | |
download | reduced_system_layer-917f5beee05a994852949b880ea47d7fab6f8cca.zip |
Impl clipboard
Diffstat (limited to 'index.htm')
-rw-r--r-- | index.htm | 73 |
1 files changed, 56 insertions, 17 deletions
@@ -10,8 +10,8 @@ <canvas style="margin: 0; width: 100%; height: 100%;" id="frame" oncontextmenu="event.preventDefault()"></canvas> <script type="worklet"> registerProcessor( - 'Sound_Node', - class Sound_Ring extends AudioWorkletProcessor { + "Sound_Node", + class Sound_Node extends AudioWorkletProcessor { constructor(options) { super(); @@ -33,7 +33,7 @@ fd_seek : () => { throw new Error("Unexpected fd_seek call"); }, }, env : { - p_clipboard_write : (size, text) => { + p_clipboard_write_impl : (size, text) => { let text_buffer = new ArrayBuffer(size); new Uint8Array(text_buffer).set(new Uint8Array(this.memory_buffer, text, size)); @@ -116,7 +116,7 @@ if (this.program === undefined) return; - this.program.exports.js_keydown(ev.key, ev.mod); + this.program.exports.js_keydown(ev.key, ev.mod, ev.ch); }, keyup : (ev) => { @@ -125,6 +125,18 @@ this.program.exports.js_keyup(ev.key, ev.mod); }, + + paste : (ev) => { + if (this.program === undefined) + return; + + let address = this.program.exports.js_clipboard_buffer(ev.bytes.length); + let len = this.program.exports.js_clipboard_size(); + + new Uint8Array(this.memory_buffer, address, len).set(ev.bytes.subarray(0, len)); + + this.program.exports.js_keydown(ev.key, ev.mod, ev.ch); + }, }; this.port.onmessage = (ev) => { @@ -162,6 +174,8 @@ ); </script> <script type="text/javascript"> + const KEY_PASTE = 118; + let key_map = { "Backspace" : 8, "Tab" : 9, @@ -229,7 +243,7 @@ "KeyS" : 115, "KeyT" : 116, "KeyU" : 117, - "KeyV" : 118, + "KeyV" : KEY_PASTE, "KeyW" : 119, "KeyX" : 120, "KeyY" : 121, @@ -276,6 +290,12 @@ return mod; }; + function char_from_event(ev) { + if (ev.key.length != 1) + return 0; + return ev.key.charCodeAt(0); + } + async function run(attrs) { let sound_sample_rate = 44100; // TODO let sound_num_channels = 2; // TODO @@ -293,6 +313,9 @@ let sound_context; let sound_node; + let paste_mod = 0; + let paste_ch = 0; + canvas = attrs.canvas; context = canvas.getContext("2d"); @@ -378,7 +401,6 @@ frame : (ev) => { pixels_buffer = ev.pixels_buffer; - let pixels = new Uint8Array(pixels_buffer); } }; @@ -428,23 +450,26 @@ }); window.addEventListener("keydown", (ev) => { - ev.preventDefault(); if (!sound_ready) return; if (ev.repeat) return; - let mod = mod_from_event(ev); let key = key_from_code(ev.code); if (key == 0) return; + let mod = mod_from_event(ev); + let ch = char_from_event(ev); - console.log(ev); - - sound_node.port.postMessage({ - id : "keydown", - mod : mod, - key : key, - }); + if (ev.ctrlKey && key == KEY_PASTE) { + paste_mod = mod; + paste_ch = ch; + } else + sound_node.port.postMessage({ + id : "keydown", + mod : mod, + key : key, + ch : ch, + }); }); window.addEventListener("keyup", (ev) => { @@ -453,15 +478,29 @@ return; if (ev.repeat) return; - let mod = mod_from_event(ev); let key = key_from_code(ev.code); if (key == 0) return; + let mod = mod_from_event(ev); sound_node.port.postMessage({ id : "keyup", - mod : mod, key : key, + mod : mod, + }); + }); + + window.addEventListener("paste", (ev) => { + ev.preventDefault(); + if (!sound_ready) + return; + + sound_node.port.postMessage({ + id : "paste", + bytes : new TextEncoder("utf-8").encode(ev.clipboardData.getData("text")), + mod : paste_mod, + key : KEY_PASTE, + ch : paste_ch, }); }); } |