Skip to content

Resolve conflicts between custom ⌥⇧→/⌥⇧← and native shortcut keys #14638

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

Closed
Ember-Sky opened this issue Apr 17, 2025 · 8 comments
Closed
Assignees
Milestone

Comments

@Ember-Sky
Copy link

Ember-Sky commented Apr 17, 2025

解决自定义 ⌥⇧→/⌥⇧← 和原生快捷键冲突

In what scenarios do you need this feature?

这里的代码看起来像是处理 shift+左右键 选择内容 的功能

Image

如果 光标在开头 或者 在结尾, 将调用stopPropagation 取消事件传播, 导致 alt + shift + 左右键 无法触发

导致
通过思源本身提供的快捷键 无法触发对应的功能
自己js代码也无法监听到这个快捷键的事件

@TCOTC
Copy link
Contributor

TCOTC commented Apr 17, 2025

需要在捕获阶段监听。

用这个 JS:

// 监听键盘按下事件
document.addEventListener('keydown', (event) => {
    // 检查是否按下了 Alt、Shift 和 ArrowLeft 键
    if (event.altKey && event.shiftKey && event.key === 'ArrowLeft') {
        // 阻止默认行为(如果需要)
        event.preventDefault();

        // 执行自定义逻辑
        console.log('Alt + Shift + ArrowLeft 被按下');
    }
}, true);

或者这个:

// 监听键盘按下事件
document.addEventListener('keydown', (event) => {
    // 检查是否按下了 Alt、Shift 和 ArrowLeft 键
    const isAltPressed = event.altKey; // Alt 键是否按下
    const isShiftPressed = event.shiftKey; // Shift 键是否按下
    const isArrowLeftPressed = event.key === 'ArrowLeft'; // 左箭头键是否按下

    // 确保没有其他按键被按下
    const noOtherKeysPressed = event.ctrlKey === false && 
                               event.metaKey === false && 
                               event.key === 'ArrowLeft';

    if (isAltPressed && isShiftPressed && isArrowLeftPressed && noOtherKeysPressed) {
        // 阻止默认行为(如果需要)
        event.preventDefault();

        // 执行自定义逻辑
        console.log('仅 Alt + Shift + ArrowLeft 被按下');
    }
}, true);

Vanessa219 added a commit that referenced this issue Apr 17, 2025
@Vanessa219 Vanessa219 added this to the 3.1.28 milestone Apr 17, 2025
@Vanessa219 Vanessa219 removed this from the 3.1.28 milestone Apr 17, 2025
@Vanessa219
Copy link
Member

Vanessa219 commented Apr 17, 2025

alt+shift+左/右 在开头或结尾会导致 #13980 这个问题(光标丢失),因此进行了屏蔽。

@Vanessa219 Vanessa219 closed this as not planned Won't fix, can't repro, duplicate, stale Apr 17, 2025
Vanessa219 added a commit that referenced this issue Apr 17, 2025
@TCOTC
Copy link
Contributor

TCOTC commented Apr 17, 2025

实测把快捷键设置为这个之后确实没法在段落开头使用

@Vanessa219 能做到让快捷键优先吗?

Image

Vanessa219 added a commit that referenced this issue Apr 18, 2025
@Vanessa219 Vanessa219 added this to the 3.1.28 milestone Apr 18, 2025
@Vanessa219 Vanessa219 reopened this Apr 18, 2025
Vanessa219 added a commit that referenced this issue Apr 18, 2025
@Vanessa219 Vanessa219 changed the title bug: alt+shift+ArrowLeft 或者 ArrowRight 会在某种情况下失效 解决自定义 ⌥⇧→/⌥⇧← 和原生快捷键冲突 Apr 18, 2025
@Ember-Sky
Copy link
Author

@Vanessa219 好奇问一句, 改成: 如果是 alt+shift+左右, 就跳过
这样也会有 #13980 这个问题么

@88250 88250 changed the title 解决自定义 ⌥⇧→/⌥⇧← 和原生快捷键冲突 Resolve conflicts between custom ⌥⇧→/⌥⇧← and native shortcut keys Apr 18, 2025
@Vanessa219
Copy link
Member

Vanessa219 commented Apr 18, 2025

不会,要判断⌥⇧→/⌥⇧← 是否被自定义过

@Ember-Sky
Copy link
Author

Ember-Sky commented Apr 18, 2025

不会,要判断⌥⇧→/⌥⇧← 是否被自定义过

那为什么不改成我说的这种方式呢:

Image

这里改成
if (!event.altKey && event.shiftKey && (event.key === "ArrowLeft" || event.key === "ArrowRight")) {

我看现在的处理, 只排除了思源的快捷键
这样的话, js片段还是监听不到 ⌥⇧→/⌥⇧←

@TCOTC
Copy link
Contributor

TCOTC commented Apr 18, 2025

js片段还是监听不到 ⌥⇧→/⌥⇧←

用 2 楼的方法

@Vanessa219
Copy link
Member

改成你的方法,如果用户没有自定义 ⌥⇧→/⌥⇧← 会导致 #13980 这个问题。

后面有限制的,我测试了是可以自定义的,如果有问题麻烦发一下重现步骤。

Image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants