CSchantz Wiki

Autotyper Wayland

Create and run file

mkdir -p ~/autotyper
cat > ~/autotyper/auto_typer.py << 'EOF'
#!/usr/bin/env python3
"""
AutoTyper - Text typer for IPMI consoles with hotkey support
"""

import gi
gi.require_version('Gtk', '4.0')

from gi.repository import Gtk, GLib, Gdk
import subprocess
import time
import threading

try:
    from pynput import keyboard
    KEYBINDER_AVAILABLE = True
except ImportError:
    KEYBINDER_AVAILABLE = False

class AutoTyperApp(Gtk.Application):
    def __init__(self):
        super().__init__(application_id="com.example.autotyper")
        self.connect('activate', self.on_activate)
        self.hotkey_active = False
        self.text_to_type = ""
        self.typing_speed = 50
        self.delay_seconds = 10
        self.hotkey_binding = None
        self.hotkey_combo = None
        self.listener = None
        self.listening = False

    def on_activate(self, app):
        self.win = Gtk.ApplicationWindow(application=app)
        self.win.set_title("AutoTyper")
        self.win.set_default_size(500, 700)
        self.win.set_resizable(True)
        self.win.set_deletable(True)
        
        vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=12)
        vbox.set_margin_top(12)
        vbox.set_margin_bottom(12)
        vbox.set_margin_start(12)
        vbox.set_margin_end(12)
        
        title = Gtk.Label()
        title.set_markup("<big><b>AutoTyper</b></big>")
        title.set_halign(Gtk.Align.START)
        vbox.append(title)
        
        desc = Gtk.Label()
        desc.set_markup("Paste text, set a hotkey, and automatically type it out.\nPerfect for IPMI consoles and restricted environments.")
        desc.set_wrap(True)
        desc.set_halign(Gtk.Align.START)
        desc.get_style_context().add_class("dim-label")
        vbox.append(desc)
        
        text_label = Gtk.Label()
        text_label.set_markup("<b>Text to Type:</b>")
        text_label.set_halign(Gtk.Align.START)
        vbox.append(text_label)
        
        scroll = Gtk.ScrolledWindow()
        scroll.set_vexpand(True)
        scroll.set_hexpand(True)
        scroll.set_min_content_height(200)
        
        self.text_view = Gtk.TextView()
        self.text_view.set_wrap_mode(Gtk.WrapMode.WORD)
        scroll.set_child(self.text_view)
        vbox.append(scroll)
        
        speed_label = Gtk.Label()
        speed_label.set_markup("<b>Typing Speed:</b>")
        speed_label.set_halign(Gtk.Align.START)
        vbox.append(speed_label)
        
        speed_box = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=12)
        speed_box.set_homogeneous(False)
        
        speed_scale = Gtk.Scale()
        speed_scale.set_range(10, 200)
        speed_scale.set_value(self.typing_speed)
        speed_scale.set_draw_value(False)
        speed_scale.set_hexpand(True)
        speed_scale.connect('value-changed', self.on_speed_changed)
        speed_box.append(speed_scale)
        
        self.speed_value = Gtk.Label()
        self.speed_value.set_text("50ms")
        self.speed_value.set_width_chars(6)
        speed_box.append(self.speed_value)
        
        vbox.append(speed_box)
        
        delay_label = Gtk.Label()
        delay_label.set_markup("<b>Delay Before Typing:</b>")
        delay_label.set_halign(Gtk.Align.START)
        vbox.append(delay_label)
        
        delay_box = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=12)
        delay_box.set_homogeneous(False)
        
        delay_scale = Gtk.Scale()
        delay_scale.set_range(1, 60)
        delay_scale.set_value(10)
        delay_scale.set_draw_value(False)
        delay_scale.set_hexpand(True)
        delay_scale.connect('value-changed', self.on_delay_changed)
        delay_box.append(delay_scale)
        
        self.delay_value = Gtk.Label()
        self.delay_value.set_text("10s")
        self.delay_value.set_width_chars(6)
        delay_box.append(self.delay_value)
        
        vbox.append(delay_box)
        
        hotkey_label = Gtk.Label()
        hotkey_label.set_markup("<b>Hotkey:</b>")
        hotkey_label.set_halign(Gtk.Align.START)
        vbox.append(hotkey_label)
        
        hotkey_box = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=8)
        hotkey_box.set_homogeneous(False)
        
        self.hotkey_button = Gtk.Button(label="Record Hotkey")
        self.hotkey_button.connect('clicked', self.on_record_hotkey)
        hotkey_box.append(self.hotkey_button)
        
        self.hotkey_display = Gtk.Label()
        self.hotkey_display.set_markup("<i>No hotkey set</i>")
        self.hotkey_display.set_halign(Gtk.Align.START)
        self.hotkey_display.set_hexpand(True)
        hotkey_box.append(self.hotkey_display)
        
        vbox.append(hotkey_box)
        
        button_box = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=8)
        button_box.set_halign(Gtk.Align.CENTER)
        
        clear_btn = Gtk.Button(label="Clear")
        clear_btn.connect('clicked', self.on_clear)
        button_box.append(clear_btn)
        
        type_btn = Gtk.Button(label="Type Now")
        type_btn.get_style_context().add_class("suggested-action")
        type_btn.connect('clicked', self.on_type_now)
        button_box.append(type_btn)
        
        vbox.append(button_box)
        
        self.status = Gtk.Label()
        self.status.set_markup("<small>Ready</small>")
        self.status.get_style_context().add_class("dim-label")
        vbox.append(self.status)
        
        self.win.set_child(vbox)
        self.win.present()

    def on_speed_changed(self, scale):
        self.typing_speed = int(scale.get_value())
        self.speed_value.set_text(f"{self.typing_speed}ms")
    
    def on_delay_changed(self, scale):
        self.delay_seconds = int(scale.get_value())
        self.delay_value.set_text(f"{self.delay_seconds}s")

    def on_clear(self, button):
        self.text_view.get_buffer().set_text("", -1)
        self.status.set_markup("<small>Cleared</small>")

    def on_type_now(self, button):
        self.get_text()
        if self.text_to_type:
            self.status.set_markup(f"<small><b>⏱️ Click in target window NOW - typing starts in {self.delay_seconds} seconds</b></small>")
            GLib.timeout_add_seconds(self.delay_seconds, self.type_text)
        else:
            self.status.set_markup("<small><span foreground='#ff6b6b'>No text to type!</span></small>")

    def on_record_hotkey(self, button):
        if not KEYBINDER_AVAILABLE:
            self.status.set_markup("<small><span foreground='#ff6b6b'>Install pynput: pip3 install --user pynput</span></small>")
            return
        
        self.hotkey_button.set_label("Press hotkey now...")
        self.hotkey_button.set_sensitive(False)
        self.status.set_markup("<small>Waiting for hotkey press... (click elsewhere to cancel)</small>")
        
        self.win.minimize()
        
        def listen_for_hotkey():
            def on_press(key):
                try:
                    key_str = str(key).replace("Key.", "").replace("'", "")
                    self.hotkey_combo = key_str
                    self.listener.stop()
                    GLib.idle_add(self.hotkey_recorded, key_str)
                except Exception as e:
                    print(f"Error recording hotkey: {e}")
            
            self.listener = keyboard.Listener(on_press=on_press)
            self.listener.start()
        
        thread = threading.Thread(target=listen_for_hotkey, daemon=True)
        thread.start()
    
    def hotkey_recorded(self, key_str):
        self.hotkey_button.set_label(f"Hotkey: {key_str}")
        self.hotkey_button.set_sensitive(True)
        self.hotkey_display.set_markup(f"<b>{key_str}</b>")
        self.status.set_markup(f"<small>Hotkey set to <b>{key_str}</b>. Press it anytime to type!</small>")
        self.win.unminimize()
        self.start_hotkey_listener()

    def get_text(self):
        buffer = self.text_view.get_buffer()
        start = buffer.get_start_iter()
        end = buffer.get_end_iter()
        self.text_to_type = buffer.get_text(start, end, False)

    def type_text(self):
        try:
            for char in self.text_to_type:
                if char == '\n':
                    subprocess.run(['xdotool', 'key', 'Return'], check=False)
                elif char == '\t':
                    subprocess.run(['xdotool', 'key', 'Tab'], check=False)
                else:
                    subprocess.run(['xdotool', 'type', '--', char], check=False)
                
                time.sleep(self.typing_speed / 1000.0)
            
            self.status.set_markup("<small><span foreground='#51cf66'>Typing complete!</span></small>")
        except FileNotFoundError:
            self.status.set_markup("<small><span foreground='#ff6b6b'>xdotool not found. Install it: sudo dnf install xdotool</span></small>")
        except Exception as e:
            self.status.set_markup(f"<small><span foreground='#ff6b6b'>Error: {str(e)}</span></small>")
        
        return False
    
    def start_hotkey_listener(self):
        if self.listening or not KEYBINDER_AVAILABLE:
            return
        
        self.listening = True
        
        def listen():
            from pynput import keyboard
            
            def on_press(key):
                try:
                    current_key = str(key).replace("Key.", "").replace("'", "")
                    if current_key == self.hotkey_combo:
                        GLib.idle_add(self.trigger_typing_from_hotkey)
                except:
                    pass
            
            listener = keyboard.Listener(on_press=on_press)
            listener.start()
            listener.join()
        
        thread = threading.Thread(target=listen, daemon=True)
        thread.start()
    
    def trigger_typing_from_hotkey(self):
        self.get_text()
        if self.text_to_type:
            self.status.set_markup(f"<small><b>⏱️ Hotkey triggered! Click in target window - typing starts in {self.delay_seconds} seconds</b></small>")
            GLib.timeout_add_seconds(self.delay_seconds, self.type_text)
        return False


def main():
    app = AutoTyperApp()
    return app.run(None)


if __name__ == '__main__':
    main()
EOF

Dependencies

sudo dnf install -y python3-gi gtk4 xdotool
pip3 install --user pynput
chmod +x ~/autotyper/auto_typer.py
python3 ~/autotyper/auto_typer.py

Quick Launch

cat > ~/.local/bin/autotyper << 'EOF'
#!/bin/bash
python3 ~/autotyper/auto_typer.py
EOF
chmod +x ~/.local/bin/autotyper