首页 > 解决方案 > Send a key-code to a running application in windows

问题描述

I am running windows 10. Is there a possibility to send (from the command line) a keycode to a windows application that is already running? (such that this application will think that I pressed a certain key?)

The request is to send keys from the command line or other, to control application tab menu command. For example to open or close file or close the application itself.

标签: windowscommand-line

解决方案


(echo with createobject^("wscript.shell"^)
echo .run "notepad.exe"
echo wscript.sleep 1000
echo .sendkeys "IN-THE-NAME-OF-ALLAH"
echo wscript.sleep 300
echo .sendkeys "(%%F)S"
echo  wscript.sleep 2000
echo  .sendkeys "myText.txt"
echo .sendkeys "{enter}" 
echo wscript.sleep 1000
echo .sendkeys "%%{F4}"
echo end with) > %temp%\sk.vbs
start /w %temp%\sk.vbs

此示例显示如何将密钥从 cmd 发送到 Notepad.exe 等应用程序以控制 .

在您的情况下不需要此第 2 行echo .run "notepad.exe",因为您已经运行了您的应用程序,如果您想通过此批处理文件运行您的应用程序,您将删除 notepad.exe 并为您的应用程序添加完整路径。

然后根据需要增加第 3 行的睡眠时间记住 1000 平均 1 秒

在第 6 行中,(%%F)S表示发送键 Alt+F 以打开文件选项卡菜单,然后发送键 S 以从此菜单中选择保存

在第 11 行中,%%{F4}表示发送键 Alt+F4 以关闭应用程序


推荐阅读