windows - 如何将批处理输出写入文本光标?

标签 windows cmd keyboard-shortcuts command-prompt

在 Windows 7 中,最终我想绑定(bind)一个快捷键 (Ctrl+Alt+D) 以吐出 20120913 1232 形式的日期时间戳.
我要挂断的步骤是让批处理文件向我的文本光标写入任何内容。目前我正在将它发送到 clip.exe ,然后我粘贴,但我想消除剪贴板的中间步骤。如果这是不可能的,还有其他方法可以解决我的问题吗?

echo %date:~-4%%date:~4,2%%date:~7,2% %time:~0,2%%time:~3,2% | clip

最佳答案

您无法从 cmd 执行此操作。您需要使用 VBS、Js 或 PowerShell。这是一个简单的 VBS 脚本,可以满足您的需求。只需在运行此 VBS 文件的桌面上创建一个快捷方式并为其分配一个快捷键

Set WshShell = WScript.CreateObject("WScript.Shell")
' Switch back to the previous window. Use Alt+Esc because Alt+Tab sometimes doesn't work
' WshShell.SendKeys "%{TAB}", True
WshShell.SendKeys "%{ESC}", True

' Get the time string
Dim dt, datetime_str
dt =  now
datetime_str = Year(dt) & Right("0" & Month(dt), 2) & Right("0" & Day(dt), 2)
datetime_str = datetime_str & " " & Right("0" & Hour(dt), 2) & Right("0" & Minute(dt), 2)

' Send the date string to the target window
WshShell.SendKeys datetime_str
然而Autohotkey (AHK) 将是一个更好的解决方案,并且运行速度更快。下面是 2 个示例函数,它们将向当前应用程序键入日期时间字符串
; Map Ctrl+Alt+T to send the date string
^!t::
FormatTime, currentdate,, yyyyMMdd HHmm
SendInput %currentdate%
Return

; Map Ctrl+Alt+D to send the date string
^!D::
SendInput %A_YYYY%%A_MM%%A_DD% %A_Hour%%A_Min%
Return
将上述脚本另存为 *.ahk 并运行它。 AHK 将在后台运行并听取按键序列以执行所需的操作

关于windows - 如何将批处理输出写入文本光标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12410957/

相关文章:

c++ - Crypto++ RSAES 类中的内存泄漏

c++ - Eclipse CDT - 如何在外部控制台 (cmd.exe) 中运行已编译的 .exe

eclipse - 如何将 Intellij IDEA 键盘映射导入 Eclipse?

windows - cmd命令转到打开批处理文件的子目录

c - VS2010 - 使用 cmd.exe 调试 win32 控制台应用程序

javascript - 如何在 JavaScript 中触发键盘快捷键功能?

java - 如何在 Java AWT 应用程序中禁用系统键盘处理?

windows - 如何使用 Python 在 Windows 防火墙中读取和添加规则

windows - 通过CMD自动重启无响应程序

c - 为什么 RegisterClassEx 失败