Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

powershell mouse move does not prevent idle mode

Before I start, here is my very first little code I wrote in 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) 

What do I want to achieve?

Well, I want to move the mouse cursor every 4 minutes to prevent the screensaver from appearing (every second in the code above for testing). The code does really move the mouse every time one pixel up and then down immediately. The thing is, the screensaver (or idle mode of windows) is still appearing.

Now, I am learning PowerShell and I have little experience with the Windows architecture.

Does anybody see my mistake? I would appreciate an answer a lot! :D Thanks in advance.

like image 431
nivoe Avatar asked Apr 05 '13 14:04

nivoe


People also ask

How do I move my mouse in PowerShell?

To move the cursor one character to the left, press the Left arrow . To move the cursor one word to the left, press Ctrl + Left arrow . To move the cursor one character to the right, press the Right arrow . To move the cursor one word to the right, press Ctrl + Right arrow .

How do I turn off idle mode?

Go to Control Panel > Personalization > Change Screensaver. Next to On Resume, Display Logon Screen, uncheck the box. This prevents your system from sleeping.

How do I stop a PowerShell script?

Open a PowerShell console session, type exit , and press the Enter key. The PowerShell console will immediately close. This keyword can also exit a script rather than the console session.


2 Answers

The solution from the blog Prevent desktop lock or screensaver with PowerShell is working for me. Here is the relevant script, which simply sends a single period to the shell:

param($minutes = 60)  $myshell = New-Object -com "Wscript.Shell"  for ($i = 0; $i -lt $minutes; $i++) {   Start-Sleep -Seconds 60   $myshell.sendkeys(".") } 

and an alternative from the comments, which moves the mouse a single pixel:

$Pos = [System.Windows.Forms.Cursor]::Position [System.Windows.Forms.Cursor]::Position = New-Object System.Drawing.Point((($Pos.X) + 1) , $Pos.Y) $Pos = [System.Windows.Forms.Cursor]::Position [System.Windows.Forms.Cursor]::Position = New-Object System.Drawing.Point((($Pos.X) - 1) , $Pos.Y) 
like image 128
JPBlanc Avatar answered Oct 07 '22 18:10

JPBlanc


I tried a mouse move solution too, and it likewise didn't work. This was my solution, to quickly toggle Scroll Lock every 4 minutes:

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 } 

I used Scroll Lock because that's one of the most useless keys on the keyboard. Also could be nice to see it briefly blink every now and then. This solution should work for just about everyone, I think.

Some people get success using $WShell.sendkeys("SCROLLLOCK") instead of $WShell.sendkeys("{SCROLLLOCK}")

See also:

  • https://ss64.com/vb/sendkeys.html
  • http://devguru.com/content/technologies/wsh/wshshell-sendkeys.html
like image 26
Andrew Avatar answered Oct 07 '22 19:10

Andrew