Skip to content
Snippets Groups Projects
Commit def5cdc4 authored by Paul Kaplan's avatar Paul Kaplan
Browse files

Send keyCode if the key information is gone

parent 5c1b806f
No related branches found
No related tags found
No related merge requests found
......@@ -87,9 +87,9 @@ const vmListenerHOC = function (WrappedComponent) {
// Don't capture keys intended for Blockly inputs.
if (e.target !== document && e.target !== document.body) return;
const key = (!e.key || e.key === 'Dead') ? e.keyCode : e.key;
this.props.vm.postIOData('keyboard', {
keyCode: e.keyCode,
key: e.key,
key: key,
isDown: true
});
......@@ -102,9 +102,9 @@ const vmListenerHOC = function (WrappedComponent) {
handleKeyUp (e) {
// Always capture up events,
// even those that have switched to other targets.
const key = (!e.key || e.key === 'Dead') ? e.keyCode : e.key;
this.props.vm.postIOData('keyboard', {
keyCode: e.keyCode,
key: e.key,
key: key,
isDown: false
});
......
......@@ -133,4 +133,49 @@ describe('VMListenerHOC', () => {
const actions = store.getActions();
expect(actions.length).toEqual(0);
});
test('keypresses go to the vm', () => {
const Component = () => (<div />);
const WrappedComponent = vmListenerHOC(Component);
// Mock document.addEventListener so we can trigger keypresses manually
// Cannot use the enzyme simulate method because that only works on synthetic events
const eventTriggers = {};
document.addEventListener = jest.fn((event, cb) => {
eventTriggers[event] = cb;
});
vm.postIOData = jest.fn();
store = mockStore({
scratchGui: {
mode: {isFullScreen: true},
modals: {soundRecorder: true},
vm: vm
}
});
mount(
<WrappedComponent
attachKeyboardEvents
store={store}
vm={vm}
/>
);
// keyboard events that do not target the document or body are ignored
eventTriggers.keydown({key: 'A', target: null});
expect(vm.postIOData).not.toHaveBeenLastCalledWith('keyboard', {key: 'A', isDown: true});
// keydown/up with target as the document are sent to the vm via postIOData
eventTriggers.keydown({key: 'A', target: document});
expect(vm.postIOData).toHaveBeenLastCalledWith('keyboard', {key: 'A', isDown: true});
eventTriggers.keyup({key: 'A', target: document});
expect(vm.postIOData).toHaveBeenLastCalledWith('keyboard', {key: 'A', isDown: false});
// When key is 'Dead' e.g. bluetooth keyboards on iOS, it sends keyCode instead
// because the VM can process both named keys or keyCodes as the `key` property
eventTriggers.keyup({key: 'Dead', keyCode: 10, target: document});
expect(vm.postIOData).toHaveBeenLastCalledWith('keyboard', {key: 10, isDown: false});
});
});
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment