Hide Cursor While Typing

Automatically hides the cursor while typing and shows it when you stop or move the mouse

スクリプトをインストールするには、Tampermonkey, GreasemonkeyViolentmonkey のような拡張機能のインストールが必要です。

You will need to install an extension such as Tampermonkey to install this script.

スクリプトをインストールするには、TampermonkeyViolentmonkey のような拡張機能のインストールが必要です。

スクリプトをインストールするには、TampermonkeyUserscripts のような拡張機能のインストールが必要です。

このスクリプトをインストールするには、Tampermonkeyなどの拡張機能をインストールする必要があります。

このスクリプトをインストールするには、ユーザースクリプト管理ツールの拡張機能をインストールする必要があります。

(ユーザースクリプト管理ツールは設定済みなのでインストール!)

このスタイルをインストールするには、Stylusなどの拡張機能をインストールする必要があります。

このスタイルをインストールするには、Stylus などの拡張機能をインストールする必要があります。

このスタイルをインストールするには、Stylus tなどの拡張機能をインストールする必要があります。

このスタイルをインストールするには、ユーザースタイル管理用の拡張機能をインストールする必要があります。

このスタイルをインストールするには、ユーザースタイル管理用の拡張機能をインストールする必要があります。

このスタイルをインストールするには、ユーザースタイル管理用の拡張機能をインストールする必要があります。

(ユーザースタイル管理ツールは設定済みなのでインストール!)

このスクリプトの質問や評価の投稿はこちら通報はこちらへお寄せください
// ==UserScript==
// @name         Hide Cursor While Typing
// @namespace    http://tampermonkey.net/
// @version      1.0a
// @description  Automatically hides the cursor while typing and shows it when you stop or move the mouse
// @author       You
// @match        *://*/*
// @grant        none
// @run-at       document-start
// ==/UserScript==

(function() {
    'use strict';

    // Configuration
    const HIDE_DELAY = 0; // Hide cursor immediately when typing starts
    const SHOW_DELAY = 1000; // Show cursor 1 second after typing stops (in milliseconds)

    let typingTimer = null;
    let isTyping = false;

    // Create and inject style element for cursor hiding
    const style = document.createElement('style');
    style.id = 'hide-cursor-while-typing-style';
    style.textContent = `
        body.hide-cursor-typing,
        body.hide-cursor-typing * {
            cursor: none !important;
        }
    `;

    // Inject style as early as possible
    const injectStyle = () => {
        if (!document.head) {
            // If head doesn't exist yet, wait a bit
            setTimeout(injectStyle, 10);
            return;
        }
        if (!document.getElementById('hide-cursor-while-typing-style')) {
            document.head.appendChild(style);
        }
    };

    injectStyle();

    /**
     * Hides the cursor by adding a CSS class to the body
     */
    const hideCursor = () => {
        if (!isTyping) {
            document.body.classList.add('hide-cursor-typing');
            isTyping = true;
        }
    };

    /**
     * Shows the cursor by removing the CSS class from the body
     */
    const showCursor = () => {
        if (isTyping) {
            document.body.classList.remove('hide-cursor-typing');
            isTyping = false;
        }
    };

    /**
     * Handles keyboard events - hides cursor and sets timer to show it later
     * @param {KeyboardEvent} e - The keyboard event
     */
    const handleKeyPress = (e) => {
        // Ignore modifier keys alone (Shift, Ctrl, Alt, Meta)
        if (['Shift', 'Control', 'Alt', 'Meta'].includes(e.key)) {
            return;
        }

        // Hide cursor immediately when typing
        hideCursor();

        // Clear existing timer
        if (typingTimer) {
            clearTimeout(typingTimer);
        }

        // Set new timer to show cursor after user stops typing
        typingTimer = setTimeout(() => {
            showCursor();
        }, SHOW_DELAY);
    };

    /**
     * Handles mouse movement - shows cursor immediately
     */
    const handleMouseMove = () => {
        // Clear the typing timer if mouse moves
        if (typingTimer) {
            clearTimeout(typingTimer);
            typingTimer = null;
        }
        // Show cursor immediately on mouse movement
        showCursor();
    };

    // Attach event listeners
    document.addEventListener('keydown', handleKeyPress, true);
    document.addEventListener('mousemove', handleMouseMove, true);

    // Cleanup on page unload (good practice)
    window.addEventListener('beforeunload', () => {
        if (typingTimer) {
            clearTimeout(typingTimer);
        }
        showCursor();
    });

    console.log('[Hide Cursor While Typing] Userscript loaded successfully');
})();