【问题标题】:Assign a hotkey to a powershell script's function为 powershell 脚本的函数分配热键
【发布时间】:2017-01-10 13:30:00
【问题描述】:

我希望脚本在后台运行,并在按下 CTRL+1 之类的热键时将变量的内容“粘贴”到当前活动的文本字段中。我到目前为止的代码:

function Send($key)
{
    $wsh.SendKeys($key)
}

$wsh = New-Object -ComObject WScript.Shell

$c1 = "Text to be sent #1"
$c2 = "Text to be sent #2"
$c3 = "Text to be sent #3"
$c4 = "Text to be sent #4"
$c0 = "Text to be sent #5"

switch ($check)
{
    1 {Send($c1)}
    2 {Send($c2)}
    3 {Send($c3)}
    4 {Send($c4)}
    0 {Send($c0)}
    default {Sleep(1)}
}

我尝试搜索,但找不到任何可行的解决方案。 我目前使用具有 similair 功能的 AutoIt 脚本,虽然我没有编写这个脚本,不幸的是我不认识作者。

#include < Misc.au3 >

Func _SendEx($ss, $warn = "")
    Local $iT = TimerInit()

    While _IsPressed("10") Or _IsPressed("11") Or _IsPressed("12")
        If $warn <> "" And TimerDiff($iT) > 1000 Then
            MsgBox(262144, "Avertissement", $warn)
        EndIf
        Sleep(50)
    WEnd
    Send($ss)
EndFunc;==>_SendEx

HotKeySet ("^1", "c1")
HotKeySet ("^2", "c2")
HotKeySet ("^3", "c3")
HotKeySet ("^4", "c4")
HotKeySet ("^0", "c0")

While True
    Sleep (10000)
WEnd

Func c1 ()
    _SendEx ("text 1")
EndFunc

Func c2 ()
    _SendEx ("text 2")
EndFunc

Func c3 ()
    _SendEx ("text 3")
EndFunc

Func c4 ()
    _SendEx ("text 4")
EndFunc

Func c0 ()
    _SendEx ("text 0")
EndFunc

我想在 powershell 中重新创建,如何为该功能分配热键?

【问题讨论】:

    标签: powershell autoit


    【解决方案1】:

    首先,没有本地方法可以执行此操作。

    如果您真的想继续使用这种方法,那么我建议您将函数放在 .ps1

    然后,使用批处理脚本 (.bat) 调用脚本

    最后,您可以为该批处理文件设置一个热键,您可以在每次 Windows 启动时注册该热键。

    一步一步的方法截图如下:

    Creating Shortcuts

    希望对你有帮助。

    【讨论】:

      【解决方案2】:

      听起来您正在尝试模拟类似于许多罗技键盘的宏按钮。如果您尝试运行脚本来侦听全局击键,然后在按下某个组合时触发粘贴,您可能需要查看来自 user32.dll 和 kernel32.dll 的 pinvoke,特别是 SetWindowsHookExFindWindow、@987654323 @、BringWindowToTopSystems.Windows.Forms.SendKeys

      简而言之,您应该可以使用 SetWindowsHookEx 和 WH_KEYBOARD_LL 设置挂钩,在处理程序中使用 FindWindow/FindWindowEx 定位目标窗口,使用 BringWindowToTop,然后通过 SendKeys 发送 ^V 键以粘贴到字段中。有很多用于 powershell 的键盘记录器 examples 可以向您展示如何使用 pinvoke 和设置挂钩。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-02-02
        • 1970-01-01
        • 2014-06-15
        • 2013-03-20
        • 1970-01-01
        • 1970-01-01
        • 2019-03-07
        • 2016-07-09
        相关资源
        最近更新 更多