@@ -31,6 +31,8 @@ let canvasCurrentMousePos = { x: 0, y: 0 };
31
31
let canvasIsMouseDown = false ;
32
32
let canvasMouseClicked = false ;
33
33
34
+ const pressedKeys = new Set <string >();
35
+
34
36
const canvas = useTemplateRef (" canvas" );
35
37
const frameTime = ref (0 );
36
38
@@ -63,8 +65,21 @@ onMounted(() => {
63
65
// therefore, we have to set the resolution same as the container size.
64
66
const observer = new ResizeObserver ((entries ) => { resizeCanvasHandler (entries ); });
65
67
observer .observe (canvas .value );
68
+
69
+ window .addEventListener (' keydown' , handleKeyDown );
70
+ window .addEventListener (' keyup' , handleKeyUp );
66
71
})
67
72
73
+ function handleKeyDown(event : KeyboardEvent ) {
74
+ pressedKeys .add (event .key );
75
+ pressedKeys .add (event .code );
76
+ }
77
+
78
+ function handleKeyUp(event : KeyboardEvent ) {
79
+ pressedKeys .delete (event .key );
80
+ pressedKeys .delete (event .code );
81
+ }
82
+
68
83
function resizeCanvas(entries : ResizeObserverEntry []) {
69
84
const canvas = entries [0 ].target ;
70
85
@@ -318,6 +333,28 @@ async function execFrame(timeMS: number, currentDisplayMode: ShaderType, playgro
318
333
uniformBufferView .setFloat32 (offset + 4 , canvasCurrentMousePos .y , true );
319
334
uniformBufferView .setFloat32 (offset + 8 , canvasLastMouseDownPos .x * (canvasIsMouseDown ? - 1 : 1 ), true );
320
335
uniformBufferView .setFloat32 (offset + 12 , canvasLastMouseDownPos .y * (canvasMouseClicked ? - 1 : 1 ), true );
336
+ } else if (uniformComponent .type == " KEY" ) {
337
+ // Set 1 or 0 depending on key state, using correct type
338
+ const isPressed = pressedKeys .has (uniformComponent .key );
339
+ if (uniformComponent .scalarType == " float32" ) {
340
+ uniformBufferView .setFloat32 (offset , isPressed ? 1.0 : 0.0 , true );
341
+ } else if (uniformComponent .scalarType == " float64" ) {
342
+ uniformBufferView .setFloat64 (offset , isPressed ? 1.0 : 0.0 , true );
343
+ } else if (uniformComponent .scalarType == " int8" ) {
344
+ uniformBufferView .setInt8 (offset , isPressed ? 1 : 0 );
345
+ } else if (uniformComponent .scalarType == " int16" ) {
346
+ uniformBufferView .setInt16 (offset , isPressed ? 1 : 0 , true );
347
+ } else if (uniformComponent .scalarType == " int32" ) {
348
+ uniformBufferView .setInt32 (offset , isPressed ? 1 : 0 , true );
349
+ } else if (uniformComponent .scalarType == " uint8" ) {
350
+ uniformBufferView .setUint8 (offset , isPressed ? 1 : 0 );
351
+ } else if (uniformComponent .scalarType == " uint16" ) {
352
+ uniformBufferView .setUint16 (offset , isPressed ? 1 : 0 , true );
353
+ } else if (uniformComponent .scalarType == " uint32" ) {
354
+ uniformBufferView .setUint32 (offset , isPressed ? 1 : 0 , true );
355
+ } else {
356
+ throw new Error (" KEY_INPUT only scalar type not supported" );
357
+ }
321
358
} else {
322
359
let _: never = uniformComponent ;
323
360
throw new Error (" Invalid state" );
@@ -749,6 +786,12 @@ async function processResourceCommands(resourceBindings: Bindings, resourceComma
749
786
// Initialize the buffer with zeros.
750
787
let bufferDefault: BufferSource = new Float32Array ([0 , 0 , 0 , 0 ]);
751
788
device .queue .writeBuffer (buffer , parsedCommand .offset , bufferDefault );
789
+ } else if (parsedCommand .type == " KEY" ) {
790
+ const buffer = allocatedResources .get (" uniformInput" ) as GPUBuffer
791
+
792
+ // Initialize the buffer with zeros.
793
+ let bufferDefault: BufferSource = new Float32Array ([0 ]);
794
+ device .queue .writeBuffer (buffer , parsedCommand .offset , bufferDefault );
752
795
} else {
753
796
// exhaustiveness check
754
797
let x: never = parsedCommand ;
0 commit comments