在我开始之前,这里是我的第一个小的代码,我在PowerShell中写了:)
[System.Windows.Forms.Cursor]::Position = `
New-Object System.Drawing.Point($pos.X, ($pos.Y - 1))
[System.Windows.Forms.Cursor]::Position = `
New-Object System.Drawing.Point($pos.X, $pos.Y)
我想要什么来实现呢?
好吧,我想移动鼠标光标每4分钟以防止出现屏幕保护程序(每秒在上面的代码进行测试)。 该代码确确实实把鼠标移动,每次一个像素起来,然后马上下来。 问题是,屏幕保护程序(或Windows的空闲模式)仍然出现。
现在,我正在学习PowerShell和我有与Windows架构的经验。
有谁看到我的错误? 我将不胜感激回答了很多! :d在此先感谢。
Answer 1:
从博客的解决方案防止锁定桌面或屏保使用PowerShell为我工作。 下面是相关的脚本,它只是发送一个周期的外壳:
param($minutes = 60)
$myshell = New-Object -com "Wscript.Shell"
for ($i = 0; $i -lt $minutes; $i++) {
Start-Sleep -Seconds 60
$myshell.sendkeys(".")
}
并从评论,这将鼠标移动的单个像素的替代:
$Pos = [System.Windows.Forms.Cursor]::Position
[System.Windows.Forms.Cursor]::Position = New-Object System.Drawing.Point((($Pos.X) + 1) , $Pos.Y)
Answer 2:
我也有类似的情况,即下载需要保持活跃过夜,需要一个按键为刷新我的连接。 我还发现,鼠标移动不起作用。 但是,使用记事本和发送键功能似乎已经完成了帽子戏法。 我送一个空间,而不是一个“” 因为如果有一个[是/否]弹出,它会自动点击使用空格键默认响应。 下面是使用的代码。
param($minutes = 120)
$myShell = New-Object -com "Wscript.Shell"
for ($i = 0; $i -lt $minutes; $i++) {
Start-Sleep -Seconds 30
$myShell.sendkeys(" ")
}
此功能将对于指定的120分钟(2小时)工作,但是可以修改为通过增加或减少所述输入的秒,或增加或减少分钟参数的指定值所需的时间。
只要运行在PowerShell ISE中的脚本或PowerShell中,并打开记事本。 的空间将是在指定的时间间隔输入的时间($分钟)所要求的长度。
祝好运!
Answer 3:
有一个模拟解决方案,这也。 这里有一个名为“超时拦截” Android应用程序振动在设定的时间间隔,你把你的鼠标就可以了。 https://play.google.com/store/apps/details?id=com.isomerprogramming.application.timeoutblocker&hl=en
Answer 4:
试试这个:(来源: http://just-another-blog.net/programming/powershell-and-the-net-framework/ )
Add-Type -AssemblyName System.Windows.Forms
$position = [System.Windows.Forms.Cursor]::Position
$position.X++
[System.Windows.Forms.Cursor]::Position = $position
while(1) {
$position = [System.Windows.Forms.Cursor]::Position
$position.X++
[System.Windows.Forms.Cursor]::Position = $position
$time = Get-Date;
$shorterTimeString = $time.ToString("HH:mm:ss");
Write-Host $shorterTimeString "Mouse pointer has been moved 1 pixel to the right"
#Set your duration between each mouse move
Start-Sleep -Seconds 150
}
Answer 5:
<# Stay Awake by Frank Poth 2019-04-16 #>
(Get-Host).UI.RawUI.WindowTitle = "Stay Awake"
[System.Console]::BufferWidth = [System.Console]::WindowWidth = 40
[System.Console]::BufferHeight = [System.Console]::WindowHeight = 10
$shell = New-Object -ComObject WScript.Shell
$start_time = Get-Date -UFormat %s <# Get the date in MS #>
$current_time = $start_time
$elapsed_time = 0
Write-Host "I am awake!"
Start-Sleep -Seconds 5
$count = 0
while($true) {
$shell.sendkeys("{NUMLOCK}{NUMLOCK}") <# Fake some input! #>
if ($count -eq 8) {
$count = 0
Clear-Host
}
if ($count -eq 0) {
$current_time = Get-Date -UFormat %s
$elapsed_time = $current_time - $start_time
Write-Host "I've been awake for "([System.Math]::Round(($elapsed_time / 60), 2))" minutes!"
} else { Write-Host "Must stay awake..." }
$count ++
Start-Sleep -Seconds 2.5
}
那重要的部分是$shell.sendkeys("{NUMLOCK}{NUMLOCK}")
这将注册在NumLock键两台印刷机和傻子壳以为输入被输入。 这个今天通过这并没有为我工作的各种脚本搜索后,我写的。 希望它可以帮助别人!
Answer 6:
我试过一个鼠标移动解决方案也是如此,它也没有工作。 这是我的解决方案,以快速切换滚动锁定每4分钟:
Clear-Host
Echo "Keep-alive with Scroll Lock..."
$WShell = New-Object -com "Wscript.Shell"
while ($true)
{
$WShell.sendkeys("{SCROLLLOCK}")
Start-Sleep -Milliseconds 100
$WShell.sendkeys("{SCROLLLOCK}")
Start-Sleep -Seconds 240
}
我用滚动锁定,因为这是在键盘上最没用的关键之一。 也可以很高兴地看到它短暂闪烁飘飞。 这个解决方案应该只是每个人的工作,我想。
也可以看看:
- https://ss64.com/vb/sendkeys.html
- http://devguru.com/content/technologies/wsh/wshshell-sendkeys.html
Answer 7:
我添加了一个通知,你可以很容易地启用/禁用只是它的变量设置为$ true或$ false。 另外,鼠标光标移动1个像素右,然后1个PX留下因此,它基本上即使经过多次反复停留在同一个地方。
# Lines needed for the notification
[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
Add-Type -AssemblyName System.Windows.Forms
$isNotificationOn = $true
$secondsBetweenMouseMoves = 6
$Pos = [System.Windows.Forms.Cursor]::Position
$PosDelta = 1
$logFilename = "previousMouseMoverAction.txt"
$errorLogFilename = "mouseMoverLog.txt"
if (!(Test-Path "$PSScriptRoot\$logFilename")) {
New-Item -path $PSScriptRoot -name $logFilename -type "file" -value "right"
Write-Host "Warning: previousMouseMoverAction.txt missing, created a new one."
}
$previousPositionChangeAction = Get-Content -Path $PSScriptRoot\$logFilename
if ($previousPositionChangeAction -eq "left") {
$PosDelta = 1
Set-Content -Path $PSScriptRoot\$logFilename -Value 'right'
} else {
$PosDelta = -1
Set-Content -Path $PSScriptRoot\$logFilename -Value 'left'
}
for ($i = 0; $i -lt $secondsBetweenMouseMoves; $i++) {
[System.Windows.Forms.Cursor]::Position = New-Object System.Drawing.Point((($Pos.X) + $PosDelta) , $Pos.Y)
if ($isNotificationOn) {
# Sending a notification to the user
$global:balloon = New-Object System.Windows.Forms.NotifyIcon
$path = (Get-Process -id $pid).Path
$balloon.Icon = [System.Drawing.Icon]::ExtractAssociatedIcon($path)
$balloon.BalloonTipIcon = [System.Windows.Forms.ToolTipIcon]::Warning
$balloon.BalloonTipText = 'I have just moved your cheese...'
$balloon.BalloonTipTitle = "Attention, $Env:USERNAME"
$balloon.Visible = $true
$balloon.ShowBalloonTip(3000)
}
}
文章来源: powershell mouse move does not prevent idle mode