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

useKeyPress是否可以支持重复按一个按键N次然后再触发回调逻辑 #2286

Open
LaxusT opened this issue Aug 8, 2023 · 9 comments
Labels
feature New feature or request pr welcomed

Comments

@LaxusT
Copy link

LaxusT commented Aug 8, 2023

比如

useKeyPress(['s', 's', 's'], () => {
   setState(1);
});

连续按下三次“s”按键再执行setState(1)

@liuyib
Copy link
Collaborator

liuyib commented Aug 8, 2023

目前不支持这种需求。可以自己记录触发的次数,然后根据次数再执行某些逻辑

@LaxusT
Copy link
Author

LaxusT commented Aug 9, 2023

目前不支持这种需求。可以自己记录触发的次数,然后根据次数再执行某些逻辑

我可以提交PR改造下ahooks提供这种场景么?类似我上面的例子。

@LaxusT
Copy link
Author

LaxusT commented Aug 9, 2023

现有通过计数肯定是能实现的,只是想便捷点,只想写快捷键 + 回调逻辑,相当于把计数逻辑放到ahooks里了,不知是否合理。

@liuyib
Copy link
Collaborator

liuyib commented Aug 10, 2023

我可以提交PR改造下ahooks提供这种场景么?类似我上面的例子。

可以说说你的思路,准备加怎样的参数来支持这个能力,像你举例的这种用法是不太行的,如果重复按三次要这样写:

useKeyPress(['s', 's', 's'], () => {
   setState(1);
});

那重复N多次,理论上要写N次,虽然可以通过一些语法简化(useKeyPress([...new Array(3).fill('s')],这样重复的次数可以动态配置),但感觉写着怪怪的。

如果有更好的思路可以分享下,一起评估下,可以的话来个 PR


另外,useKeyPress 里支持的快捷键格式有:字符串 'ctrl.enter'、数组 组合键 ['ctrl.enter'] / 多个键['a', 'b']、回调 (event) => !filterKeys.includes(event.key)。如果要做的话,你的这个需求需要考虑尽量支持以上所有格式。所以内置起来还是挺麻烦的,最好能有好的思路兼容以上所有情况~

@LaxusT
Copy link
Author

LaxusT commented Aug 10, 2023

嗯,我明白,我想想。

@liuyib liuyib added pr welcomed feature New feature or request labels Aug 11, 2023
@lixiaowei11111
Copy link

嗯,我明白,我想想。

大佬你好,增加一种新的格式来兼容是否可行呢 useKeyPress([{'shift.c':2},{'a':3},{'1':4},{2:3}])

@liuyib
Copy link
Collaborator

liuyib commented Sep 27, 2023

useKeyPress([{'shift.c':2},{'a':3},{'1':4},{2:3}])

看起来不错。

可以在此基础上,扩展一些其他思路:

  1. 全部键需要定制次数
useKeyPress({
  'shift.c': 2,
  a: 3,
  '1': 4,
  2: 3,
});
  1. 部分键需要定制次数
useKeyPress(['ctrl.c', { 'shift.c': 2 }, 'shift']);

@lixiaowei11111
Copy link

useKeyPress([{'shift.c':2},{'a':3},{'1':4},{2:3}])

看起来不错。

可以在此基础上,扩展一些其他思路:

  1. 全部键需要定制次数
useKeyPress({
  'shift.c': 2,
  a: 3,
  '1': 4,
  2: 3,
});
  1. 部分键需要定制次数
useKeyPress(['ctrl.c', { 'shift.c': 2 }, 'shift']);

这个连击的有效时间段是否需要考虑可以使用者自定义呢, 比如初始值2000ms(默认情况下在2000ms内,连按Q键10次就会触发回调),
那是否可以增加一个options属性,可以让使用者自己来定义一个统一的有效连击的时间区间呢

useKeyPress([ { 2: 3 } ], fn, { timeInterval: 2000 } );

或者,让使用者可以定义每个连击按键的有效时间区间

useKeyPress( [ {'a': 2 }, {"shift.c": { counter: 2 , timeInterval: 5000  } ] );
useKeyPress({
  'shift.c': { counter: 2,timeInterval: 2500 },
  a: 3,
  '1': 4,
  2: 3,
});

@liuyib
Copy link
Collaborator

liuyib commented Sep 27, 2023

如果加上连击能力,确实要考虑“有效时间段”、“连击最小间隔”等这些情况~

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or request pr welcomed
Projects
None yet
Development

No branches or pull requests

3 participants