按下键时如何发送text/string powershell
How to send text/string when key is pressed powershell
我想使用功能键制作宏来改进我的工作流程,但下面的代码不起作用,我认为它很容易解释,而代码是 运行 如果我按 X 键,则显示不同的文本已发送。
$wshell = New-Object -ComObject wscript.shell;
# choose the key you are after
$key = [System.Windows.Input.Key]::LeftCtrl
$isCtrl = [System.Windows.Input.Keyboard]::IsKeyDown($key)
while ($true)
{
if ($isCtrl)
{
$wshell.SendKeys('Thank you for using the service.')
}
}
上面的代码不起作用。但是,如果我只使用下面的代码,它确实会按预期发送字符串。
$wshell = New-Object -ComObject wscript.shell;
sleep 1
$wshell.SendKeys('Digital service desk')
您一直在循环内检查相同的值,因为 $isCtrl
在进入循环后从未被赋值。
更改为:
while ($true)
{
$isCtrl = [System.Windows.Input.Keyboard]::IsKeyDown($key)
if ($isCtrl)
{
$wshell.SendKeys('Thank you for using the service.')
}
}
以便您每次都重新检查控件是否被按下。
如 Mathias R. Jessen 所述,我没有更新循环内 $isCtrl 的值。如果有人想使用它,我现在 post 代码。
$wshell = New-Object -ComObject wscript.shell;
Add-Type -AssemblyName WindowsBase
Add-Type -AssemblyName PresentationCore
while ($true)
{
if ([System.Windows.Input.Keyboard]::IsKeyDown([System.Windows.Input.Key]::F2))
{
sleep 1
$wshell.SendKeys('Message after pressing F2')
} elseif ([System.Windows.Input.Keyboard]::IsKeyDown([System.Windows.Input.Key]::F4)){
sleep 1
$wshell.SendKeys('Message after pressing F4')
}
}
我想使用功能键制作宏来改进我的工作流程,但下面的代码不起作用,我认为它很容易解释,而代码是 运行 如果我按 X 键,则显示不同的文本已发送。
$wshell = New-Object -ComObject wscript.shell;
# choose the key you are after
$key = [System.Windows.Input.Key]::LeftCtrl
$isCtrl = [System.Windows.Input.Keyboard]::IsKeyDown($key)
while ($true)
{
if ($isCtrl)
{
$wshell.SendKeys('Thank you for using the service.')
}
}
上面的代码不起作用。但是,如果我只使用下面的代码,它确实会按预期发送字符串。
$wshell = New-Object -ComObject wscript.shell;
sleep 1
$wshell.SendKeys('Digital service desk')
您一直在循环内检查相同的值,因为 $isCtrl
在进入循环后从未被赋值。
更改为:
while ($true)
{
$isCtrl = [System.Windows.Input.Keyboard]::IsKeyDown($key)
if ($isCtrl)
{
$wshell.SendKeys('Thank you for using the service.')
}
}
以便您每次都重新检查控件是否被按下。
如 Mathias R. Jessen 所述,我没有更新循环内 $isCtrl 的值。如果有人想使用它,我现在 post 代码。
$wshell = New-Object -ComObject wscript.shell;
Add-Type -AssemblyName WindowsBase
Add-Type -AssemblyName PresentationCore
while ($true)
{
if ([System.Windows.Input.Keyboard]::IsKeyDown([System.Windows.Input.Key]::F2))
{
sleep 1
$wshell.SendKeys('Message after pressing F2')
} elseif ([System.Windows.Input.Keyboard]::IsKeyDown([System.Windows.Input.Key]::F4)){
sleep 1
$wshell.SendKeys('Message after pressing F4')
}
}