Skip to content

Commit

Permalink
Make it harder to steal character keys from the outer app
Browse files Browse the repository at this point in the history
- require "Alt" key with numpad Plus/Minus,
- use `event.code` instead of deprecated `keyCode`,
- process editing keys only when editing.
  • Loading branch information
paulsmirnov committed Aug 16, 2024
1 parent 77973f9 commit e212930
Showing 1 changed file with 58 additions and 48 deletions.
106 changes: 58 additions & 48 deletions packages/miew/src/Miew.js
Original file line number Diff line number Diff line change
Expand Up @@ -2855,59 +2855,69 @@ Miew.prototype._onKeyDown = function (event) {
return;
}

switch (event.keyCode) {
case 'C'.charCodeAt(0):
if (settings.now.editing) {
// editing keys
if (settings.now.editing) {
switch (event.code) {
case 'KeyC':
this._enterComponentEditMode();
}
break;
case 'F'.charCodeAt(0):
if (settings.now.editing) {
break;
case 'KeyF':
this._enterFragmentEditMode();
break;
case 'KeyA':
switch (this._editMode) {
case EDIT_MODE.COMPONENT:
this._applyComponentEdit();
break;
case EDIT_MODE.FRAGMENT:
this._applyFragmentEdit();
break;
default:
break;
}
break;
case 'KeyD':
switch (this._editMode) {
case EDIT_MODE.COMPONENT:
this._discardComponentEdit();
break;
case EDIT_MODE.FRAGMENT:
this._discardFragmentEdit();
break;
default:
break;
}
break;
default:
}
}

// other keys
switch (event.code) {
case 'NumpadAdd':
if (event.altKey) {
event.preventDefault();
event.stopPropagation();
this._forEachComplexVisual((visual) => {
visual.expandSelection();
visual.rebuildSelectionGeometry();
});
this._updateInfoPanel();
this._needRender = true;
}
break;
case 'A'.charCodeAt(0):
switch (this._editMode) {
case EDIT_MODE.COMPONENT:
this._applyComponentEdit();
break;
case EDIT_MODE.FRAGMENT:
this._applyFragmentEdit();
break;
default: break;
}
break;
case 'D'.charCodeAt(0):
switch (this._editMode) {
case EDIT_MODE.COMPONENT:
this._discardComponentEdit();
break;
case EDIT_MODE.FRAGMENT:
this._discardFragmentEdit();
break;
default: break;
case 'NumpadSubtract':
if (event.altKey) {
event.preventDefault();
event.stopPropagation();
this._forEachComplexVisual((visual) => {
visual.shrinkSelection();
visual.rebuildSelectionGeometry();
});
this._updateInfoPanel();
this._needRender = true;
}
break;
case 107:
event.preventDefault();
event.stopPropagation();
this._forEachComplexVisual((visual) => {
visual.expandSelection();
visual.rebuildSelectionGeometry();
});
this._updateInfoPanel();
this._needRender = true;
break;
case 109:
event.preventDefault();
event.stopPropagation();
this._forEachComplexVisual((visual) => {
visual.shrinkSelection();
visual.rebuildSelectionGeometry();
});
this._updateInfoPanel();
this._needRender = true;
break;
default:
}
};
Expand All @@ -2917,7 +2927,7 @@ Miew.prototype._onKeyUp = function (event) {
return;
}

if (event.keyCode === 'X'.charCodeAt(0)) {
if (event.code === 'KeyX') {
this._extractRepresentation();
}
};
Expand Down

0 comments on commit e212930

Please sign in to comment.