hooks icon indicating copy to clipboard operation
hooks copied to clipboard

[Feature request] useKeyPress 回调传入触发的快捷键

Open yunsii opened this issue 3 years ago • 2 comments
trafficstars

使用场景

使用单个 hook 注册多个快捷键的不同回调,回调时不能直接知道是什么快捷键触发,特别是使用组合键时。

例如:

useKeyPress(['r', 'o', 'l', 'a', 's'], (event) => {})

通过一个 hook 注册了 5 个快捷键,每个快捷键对应不同的逻辑,目前是在回调里自己额外处理的。不方便分别注册实现,因为是通过注册器得到的一类快捷键定义。

yunsii avatar Dec 22 '21 12:12 yunsii

Hello @theprimone. We totally like your proposal/feedback, welcome to send us a Pull Request for it. Please send your Pull Request to proper branch (feature branch for the new feature, master for bugfix and other changes), fill the Pull Request Template here, provide changelog/TypeScript/documentation/test cases if needed and make sure CI passed, we will review it soon. We appreciate your effort in advance and looking forward to your contribution!

你好 @theprimone,我们完全同意你的提议/反馈,欢迎直接在此仓库 创建一个 Pull Request 来解决这个问题。请将 Pull Request 发到正确的分支(新特性发到 feature 分支,其他发到 master 分支),务必填写 Pull Request 内的预设模板,提供改动所需相应的 changelog、TypeScript 定义、测试用例、文档等,并确保 CI 通过,我们会尽快进行 Review,提前感谢和期待您的贡献。

giphy

github-actions[bot] avatar Dec 22 '21 12:12 github-actions[bot]

看了下代码,好像还不是三两下能改好的……

yunsii avatar Feb 25 '22 13:02 yunsii

@yunsii 这个是不是应该再自己的逻辑里面去判断比较好,信息都在e中,可以根据自己的需要包一层e,因为这个组合ctrl+c会跟你业务定义的逻辑相关有的人定义ctrl&c或者ctrl + c,觉得不应该在useKeyPress中处理

Simon-He95 avatar Apr 23 '23 05:04 Simon-He95

@Simon-He95 是可以自己处理的,但是就想你说的如果额外定义了组合键判断起来就很麻烦。我最初的想法是这样:

const hotKeys = [
	{
		keys: ['ctrl', 'c'],
		callback: () => {}
	},
	{
		keys: ['c'],
		callback: () => {}
	},
]

// ...

useKeyPress([hotKeys.map(item => item.keys)], (event, keys) => {
	hotKeys.find(item => _.isEqual(item.keys, keys))?.callback()
})

或者说内部直接支持传 callback 得了?

yunsii avatar Apr 23 '23 05:04 yunsii

我觉得还是不要支持传callback比较好,会增加额外的开销,还是第二个参数传string[]的方式好一些

Simon-He95 avatar Apr 23 '23 06:04 Simon-He95

能支持第二个入参挺好的,特别是这种场景

yunsii avatar Apr 23 '23 10:04 yunsii