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

Improve issue where keyup is not fired when command key is pressed / 修复 command 键按下时 keyup 不触发的问题 #502

Merged

Conversation

foxhatleo
Copy link
Contributor

@foxhatleo foxhatleo commented Dec 6, 2024

中文见下方

Currently, if one were to register two hotkeys with command key like "Command + Shift + ArrowLeft" and "Command + Shift + ArrowRight", they will not be reliably triggered. To reproduce:

  1. Hold down Command and Shift key.
  2. Press and release left arrow. The hotkey should trigger.
  3. Now, while still holding command and shift keys, press and release right arrow. The hotkey will not trigger.

The reason of this issue is that when command key is pressed, keyup for a non-modifier key will not trigger. This is a known browser limitation. As a result, hotkeys never knew about the left arrow being let go. In its internal state, when the right arrow is pressed, the _downKeys include both arrow left and arrow right. This is incorrect.

This PR brings a solution where, if command key is pressed, any non-modifier key that is not the event key will be removed from _downKeys. Because keyup is never fired for them, we need to manually release them when command is pressed.


当前注册多个带有command的hotkeys(比如“Command + Shift + ArrowLeft”和“Command + Shift + ArrowRight”)的情况下,触发不稳定。重现步骤如下:

  1. 按住 Command 和 Shift 键。
  2. 按下并释放左箭头键,快捷键应该会触发。
  3. 继续按住 Command 和 Shift 键,然后按下并释放右箭头键,此时快捷键就不会触发。

出现此问题的原因是,当 command 键被按下时,非修饰键的 keyup 事件不会触发。这是已知的浏览器限制。结果就是快捷键内部状态无法得知左箭头键已被释放。在上面提到的例子中,当按下右箭头键时,内部状态中_downKeys包含了左箭头键和右箭头键。这就不对了。

此 PR 提供的解决方案是:当 command 键被按下时,任何不是当前事件触发的非修饰键都会从 _downKeys 中移除。由于它们的 keyup 事件不会被触发,我们需要在 command 被按下时手动释放它们。

致maintainers:可以用中文和我交流。

@jaywcjlove
Copy link
Owner

@foxhatleo

hotkeys('command+shift+left', {keyup: true}, function(event, handler) {
  if (event.type === 'keydown') {
    console.log('keydown:', event.type, handler, handler.key);
  }

  if (event.type === 'keyup') {
    console.log('keyup:', event.type, handler, handler.key);
  }
});

From the example above, the test did not resolve the issue where the keyup event is not triggered.


通过上面示例,测试并没有解决 keyup 不触发的问题

@foxhatleo
Copy link
Contributor Author

@jaywcjlove

按下 command 键不触发 keyup 的问题是无解的。按下command键后非修饰键的keyup会被浏览器或macOS给“吞掉”。问题在于说hotkeys目前依靠keyup来记录哪些按键被按下,而这个PR的目的正是修正由于keyup没有触发而导致hotkeys内部已按下按键的state不正确的问题。keyup还是不会被触发的,这个很难解决。


It's virtually impossible to find a solution for the issue of keyup not firing when command is pressed. The keyup event of non-modifier keys is "consumed" by the browser or macOS when command is pressed. The issue here is that hotkey relies on keyup to know which keys are currently pressed. The purpose of this PR is to fix the issue where hotkeys' internal state of which keys are held down become incorrect due to keyup not firing. The keyup event is still not being fired regardless. It's hard to find a solution for that.

@jaywcjlove
Copy link
Owner

@foxhatleo 了解,command 事件触发是无法解决的,只是解决内部记录的摁键内容

@foxhatleo
Copy link
Contributor Author

foxhatleo commented Dec 10, 2024

@jaywcjlove 对,那您看看这个PR代码还有什么问题吗?如果没有的话能帮忙merge一下吗?我这儿有个project因为这个bug没法继续。😅

@jaywcjlove jaywcjlove merged commit 0dd7438 into jaywcjlove:master Dec 11, 2024
github-actions bot pushed a commit that referenced this pull request Dec 11, 2024
jaywcjlove added a commit that referenced this pull request Dec 11, 2024
github-actions bot pushed a commit that referenced this pull request Dec 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants