Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 3 additions & 8 deletions packages/scratch-gui/src/lib/vm-listener-hoc.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,9 @@ const vmListenerHOC = function (WrappedComponent) {
}
}
handleKeyDown (e) {
// Don't capture keys intended for HTML inputs (e.g. project title).
// The Blockly workspace is rendered as SVG, so SVG-targeted events
// should always reach the VM for key-sensing — even when a block has
// Blockly focus — so that game controls are never silently dropped
// while the user is on the Code tab.
if (e.target !== document && e.target !== document.body) {
if (!(e.target instanceof SVGElement)) return;
}
// Don't capture keys intended for Blockly inputs.
if (e.target instanceof HTMLInputElement || e.target instanceof HTMLTextAreaElement ||
e.target.isContentEditable) return;

const key = (!e.key || e.key === 'Dead') ? e.keyCode : e.key;
this.props.vm.postIOData('keyboard', {
Expand Down
11 changes: 4 additions & 7 deletions packages/scratch-gui/test/unit/util/vm-listener-hoc.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -168,15 +168,12 @@ describe('VMListenerHOC', () => {
/>
);

// keydown with an HTML target (e.g. project title input) should not be forwarded to VM
const inputEl = document.createElement('input');
eventTriggers.keydown({key: 'A', target: inputEl});
// keyboard events in text inputs are ignored
eventTriggers.keydown({key: 'A', target: document.createElement('input')});
expect(vm.postIOData).not.toHaveBeenLastCalledWith('keyboard', {key: 'A', isDown: true});

// keydown with an SVG target (Blockly workspace) should always be forwarded to VM
// even when a block has Blockly focus, so game controls work from the Code tab
const svgEl = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
eventTriggers.keydown({key: 'A', target: svgEl});
// keydown with other non-input targets are sent to the vm via postIOData
eventTriggers.keydown({key: 'A', target: document.createElement('div')});
expect(vm.postIOData).toHaveBeenLastCalledWith('keyboard', {key: 'A', isDown: true});

// keydown/up with target as the document are sent to the vm via postIOData
Expand Down
Loading