1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Vue.js3で「ctrl+z」「ctrl+shift+z」の処理を実装する

Posted at

経緯

先日Undo/Redo機能の実装をする機会がありました。
ついでにショートカットキーで実装できないものかChatGPT 4oに聞いたところ教えてくれたので備忘録として残します。

実装

<script setup>
import { onMounted } from "vue";

// キーボードショートカットのハンドラー
// ※undo(), redo()の中身の処理は省略
const handleKeydown = (e) => {
    if (e.ctrlKey || e.metaKey) {
        if (e.key === 'z') {
            undo(); // Ctrl+Z (Undo)
        } else if (e.key === 'Z') {
            redo(); // Ctrl+Shift+Z (Redo)
        }
        e.preventDefault(); // デフォルトの動作を無効化
    }
};

// イベントリスナーを登録する
onMounted(() => {
    window.addEventListener('keydown', handleKeydown);
});

</script>

参考記事

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?