* Conoscenza del computer >> sistemi >  >> Windows 11

Simulare pressione tasti in file batch Windows (SendKeys via WSH)

Limiti dei batch puri

Un file .bat esegue comandi DOS/console. Non può cliccare GUI né premere tasti in finestre Windows senza strumenti esterni.

Soluzione: Windows Script Host (WSH) + SendKeys

  1. Crea un file tasti.vbs (VBScript):
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "notepad.exe"
WScript.Sleep 1000
WshShell.SendKeys "Hello from batch!"
WshShell.SendKeys "{ENTER}"
  1. Nel batch mio.bat richiama:
@echo off
cscript //nologo tasti.vbs

Codici SendKeys principali

TastoCodice
Alt%
Ctrl^
Shift+
Enter{ENTER}
Tab{TAB}
F1‑F12{F1}…{F12}
Freccia su{UP}
Freccia giù{DOWN}
Esc{ESC}

Esempio: aprire Word e creare nuovo documento

Set sh = CreateObject("WScript.Shell")
sh.Run "winword.exe"
WScript.Sleep 3000
sh.SendKeys "^n"   ' Ctrl+N = nuovo documento

Alternative più potenti

  • AutoHotkey (script .ahk) – hotkey, GUI automation, DLL calls.
  • PowerShell + Add-Type -AssemblyName System.Windows.Forms → [System.Windows.Forms.SendKeys]::SendWait().
  • Python + pyautogui / pynput.

Attenzione

SendKeys è fragile: dipende da focus finestra, tempistiche, layout tastiera. Usa per task semplici; per automazione robusta preferisci AutoHotkey o Python.

Windows 11