Skip to content

Commit

Permalink
FIX: ホットキーでshift+数字を登録時のエラーをなくす (#1964)
Browse files Browse the repository at this point in the history
* FIX: ホットキーでshift+数字を登録時のエラーをなくす

event.code式に書き換え
.がPeriodだったりと様々な対応しきれないものがあるので対応しているもの以外は弾くようにした

* Update: とりあえず既存の保存形式のままにし、-と/も認める

* fix: lintエラーを修正

* Update src/plugins/hotkeyPlugin.ts

Co-authored-by: Hiroshiba <[email protected]>

* Update src/plugins/hotkeyPlugin.ts

Co-authored-by: Hiroshiba <[email protected]>

* Update hotkeyPlugin.ts

* Apply suggestions from code review

---------

Co-authored-by: Hiroshiba <[email protected]>
  • Loading branch information
tsym77yoshi and Hiroshiba authored May 20, 2024
1 parent 3f41639 commit 403b44a
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions src/plugins/hotkeyPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,10 +278,12 @@ const combinationToBindingKey = (
// MetaキーはCommandキーとして扱う
// NOTE: hotkeys-jsにはWinキーが無く、Commandキーとして扱われている
// NOTE: Metaキーは以前採用していたmousetrapがそうだった名残り
// NOTE: hotkeys-jsでは方向キーのarrowプレフィックスが不要
const bindingKey = combination
.toLowerCase()
.split(" ")
.map((key) => (key === "meta" ? "command" : key))
.map((key) => key.replace("arrow", ""))
.join("+");
return bindingKey as BindingKey;
};
Expand Down Expand Up @@ -310,15 +312,18 @@ export const eventToCombination = (event: KeyboardEvent): HotkeyCombination => {
if (event.metaKey) {
recordedCombination += "Meta ";
}
if (event.key === " ") {
recordedCombination += "Space";
} else {
if (["Control", "Shift", "Alt", "Meta"].includes(event.key)) {
recordedCombination = recordedCombination.slice(0, -1);
} else {
recordedCombination +=
event.key.length > 1 ? event.key : event.key.toUpperCase();
}
// event.codeからevent.key形式へと変換
// TODO: 主要なキーも使えるようにする
const eventKey = event.code.replace(/Key|Digit|Numpad/, "");
// 英字 数字 上下左右 Enter Space Backspace Delete Escape F1~ - /のみ認める
if (
/^([A-Z]|\d|Arrow(Up|Down|Left|Right)|Enter|Space|Backspace|Delete|Escape|F\d+|-|\/)$/.test(
eventKey,
)
) {
recordedCombination += eventKey;
}
// 修飾キーのみだった場合末尾がスペースになるので削除
recordedCombination = recordedCombination.replace(/\s$/, "");
return HotkeyCombination(recordedCombination);
};

0 comments on commit 403b44a

Please sign in to comment.