英文:
Call hotkey only once with react-hotkeys-hook
问题
我在我的应用程序中有两个快捷键,1
和 2
。如果我按下 1
然后释放,再按下 2
,那没问题。但如果我同时按下它们,当我同时释放它们时,它不会按预期递增。
我的期望是,如果我按下 1 和 2,然后同时释放它们,它们应该都递增 1。但它没有。我查看了 API 并尝试了一些不同的选项,但似乎总是在某些情况下递增 2。
import { useHotkeys } from 'react-hotkeys-hook';
export default function App() {
const [cnt1, set1] = React.useState(0);
const [cnt2, set2] = React.useState(0);
useHotkeys('1', () => set1((v) => v + 1), { keyup: true });
useHotkeys('2', () => set2((v) => v + 1), { keyup: true });
return (
<div>
<div>{cnt1}</div>
<div>{cnt2}</div>
</div>
);
}
一个运行示例:
https://stackblitz.com/edit/stackblitz-starters-6mbnsa?file=src%2FApp.tsx
英文:
I have 2 hotkeys in my app, 1
and 2
. If I press 1
release and then 2
, it's fine. But if I press both at the same time, it doesn't increment as expected when I release both at the same time.
My expectation is that if I press 1 and 2, and then release both, I should have both incrementing by 1. But it doesn't. I checked the API and tried some different options but it always seems to increment 2 in one of the cases.
import { useHotkeys } from 'react-hotkeys-hook';
export default function App() {
const [cnt1, set1] = React.useState(0);
const [cnt2, set2] = React.useState(0);
useHotkeys('1', () => set1((v) => v + 1), { keyup: true });
useHotkeys('2', () => set2((v) => v + 1), { keyup: true });
return (
<div>
<div>{cnt1}</div>
<div>{cnt2}</div>
</div>
);
}
A running example:
https://stackblitz.com/edit/stackblitz-starters-6mbnsa?file=src%2FApp.tsx
答案1
得分: 1
我已经尝试了很多方法来防止这种情况发生,找到了一个不太好的解决方案,我想分享一下。
有一个isHotkeyPressed
钩子,可以在第二个回调中使用,来检查另一个热键是否处于活动状态:
useHotkeys('2', () => {
if (!isHotkeyPressed('1')) {
set2((v) => v + 1);
}
}, { keyup: true });
你可以将这两个热键钩子合并成一个(可以用逗号分隔多个键),以使另一个键上的isHotkeyPressed
更加动态。
英文:
I've tried quite some ways to prevent this, found a single, not so great solution I thought I'd share.
There seems to be a isHotkeyPressed
hook that can be used in the second callback to check if the other hotkey is active:
useHotkeys('2', () => {
if (!isHotkeyPressed('1')) {
set2((v) => v + 1);
}
}, { keyup: true });
You could combine both hotkeys hooks into a single one (you can pass multiple keys comma separated) to make the isHotkeyPressed
on the other key more dynamic.
答案2
得分: 0
我认为我在复制 @0stone0 的回答时犯了一个错误,并无意中使它起作用。我仍然困惑于这是如何工作的,但即使我有4个数字,它也可以工作。
请注意,只有在我放开的按键不被按下时它才会递增。
useHotkeys(
'1',
() => {
if (!isHotkeyPressed('1')) {
set1((v) => v + 1);
}
},
{
keyup: true,
}
);
useHotkeys(
'2',
() => {
if (!isHotkeyPressed('2')) {
set2((v) => v + 1);
}
},
{
keyup: true,
}
);
英文:
I think I made a mistake copying @0stone0 answer and inadvertently made it work. I'm still confused how this is working, but it even works if I have 4 numbers.
Notice it only increments if the key I'm letting go up is not pressed.
useHotkeys(
'1',
() => {
if (!isHotkeyPressed('1')) {
set1((v) => v + 1);
}
},
{
keyup: true,
}
);
useHotkeys(
'2',
() => {
if (!isHotkeyPressed('2')) {
set2((v) => v + 1);
}
},
{
keyup: true,
}
);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论