Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: better robustness for triple control key press detect and disable preference #1779

Merged
merged 2 commits into from
Jul 26, 2024
Merged
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
37 changes: 24 additions & 13 deletions src/command/KeyBindingManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ define(function (require, exports, module) {
const _registeredCustomKeyMaps = {};

const STATE_CUSTOM_KEY_MAP_ID = "customKeyMapID";
const PREF_TRIPLE_CTRL_KEY_PRESS_ENABLED = "tripleCtrlPalette";

/**
* @private
Expand Down Expand Up @@ -1101,9 +1102,10 @@ define(function (require, exports, module) {
}
}

let lastKeyPressTime = 0; // Store the time of the last key press
let lastCtrlKeyPressTime = 0; // Store the time of the last key press
let pressCount = 0; // Counter for consecutive Control key presses
const doublePressInterval = 500; // Maximum time interval between presses, in milliseconds, to consider it a double press
const doublePressInterval = 250; // Maximum time interval between presses, in milliseconds, to consider it a double press
const PRESS_ACTIVATE_COUNT = 3;
const ctrlKeyCodes = {
ControlLeft: true,
ControlRight: true,
Expand All @@ -1113,22 +1115,28 @@ define(function (require, exports, module) {
Meta: true
};
function _detectTripleCtrlKeyPress(event) {
if(PreferencesManager && !PreferencesManager.get(PREF_TRIPLE_CTRL_KEY_PRESS_ENABLED)){
return false;
}
const currentTime = new Date().getTime(); // Get the current time
if (ctrlKeyCodes[event.code] && ctrlKeyCodes[event.key] && !event.shiftKey && !event.altKey) {
const currentTime = new Date().getTime(); // Get the current time
pressCount++;
if (currentTime - lastKeyPressTime <= doublePressInterval) {
if(pressCount === 3) {
KeyboardOverlayMode.startOverlayMode();
event.stopPropagation();
event.preventDefault();
return true;
}
// ignore all higher order press events like triple/quadruple press
} else {
if(pressCount === PRESS_ACTIVATE_COUNT && (currentTime - lastCtrlKeyPressTime) <= doublePressInterval) {
KeyboardOverlayMode.startOverlayMode();
event.stopPropagation();
event.preventDefault();
lastCtrlKeyPressTime = currentTime;
Metrics.countEvent(Metrics.EVENT_TYPE.KEYBOARD, 'ctrlx'+PRESS_ACTIVATE_COUNT, "showOverlay");
return true;
}
if((currentTime - lastCtrlKeyPressTime) > doublePressInterval){
pressCount = 1;
}
lastKeyPressTime = currentTime;
lastCtrlKeyPressTime = currentTime;
} else {
pressCount = 0;
}
return false;
}

const dontHideMouseOnKeys = {
Expand Down Expand Up @@ -1862,6 +1870,9 @@ define(function (require, exports, module) {
_loadUserKeyMap();
exports.constructor(EVENT_PRESET_CHANGED, _customKeymapIDInUse);
});
PreferencesManager.definePreference(PREF_TRIPLE_CTRL_KEY_PRESS_ENABLED, "boolean", true, {
description: Strings.DESCRIPTION_TRIPLE_CTRL_PALETTE
});
_customKeymapIDInUse = PreferencesManager.stateManager.get(STATE_CUSTOM_KEY_MAP_ID);
_loadUserKeyMap();
});
Expand Down
2 changes: 1 addition & 1 deletion src/command/KeyboardOverlayMode.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ define(function (require, exports, module) {

function showOverlay(targetId) {
// Find the target div and the overlay div
Metrics.countEvent(Metrics.EVENT_TYPE.KEYBOARD, "ctrlx3", "showOverlay");
Metrics.countEvent(Metrics.EVENT_TYPE.UI, "palette", "showOverlay");
if(!targetId){
console.error("No target ID for selecting overlay. Ignoring");
return;
Expand Down
1 change: 1 addition & 0 deletions src/nls/root/strings.js
Original file line number Diff line number Diff line change
Expand Up @@ -1019,6 +1019,7 @@ define({
"DESCRIPTION_RULERS_ENABLED": "true to enable editor rulers, else false",
"DESCRIPTION_AUTO_RENAME_TAGS": "true to automatically rename paired HTML/XML tag, else false",
"DESCRIPTION_DRAG_AND_DROP_ENABLED": "true to enable drag and drop functionality for files and folders from the file explorer or Finder",
"DESCRIPTION_TRIPLE_CTRL_PALETTE": "true to enable showing the visual command overlay on pressing Ctrl/Cmd key 3 consecutive times",
"DESCRIPTION_SMART_INDENT": "Automatically indent when creating a new block",
"DESCRIPTION_SOFT_TABS": "false to turn off soft tabs behavior",
"DESCRIPTION_SORT_DIRECTORIES_FIRST": "true to sort the directories first in the project tree",
Expand Down
Loading