Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Stay Awake


  • Please log in to reply
9 replies to this topic
Howardb1
  • Members
  • 13 posts
  • Last active: Jul 03 2013 07:45 AM
  • Joined: 17 May 2013

This script, when compiled and executed, keeps my computer's screen saver from running, even after several hours.  I put this script together using ideas and bits of scrips that I found in this form and internet.  It is one of my first scripts, but I think it has some interesting things to share, such as the programmatic ForceSingleInstance technique and the dragable thick frame around the gui form's button.

 

Again, almost all of this was all stuff I found that other people had posted.  I hope that posting all of this combined into one useful example will help others to develop their own programs.

#Persistent
#SingleInstance off   ; Explicidly handle single instance

if !A_isCompiled {

   msgbox,This program will only run when it is compiled.
   exitapp
   
}

; force single instance by killing off any running copies of the same program.

If ForceSingleInstance() {

  MsgBox, 4,,
             ( LTRIM ; Remove leading spaces and end all but the last line with `n
               Another copy of this program was closed.
               Do you wish to close this one, too?
             )
  IfMsgBox Yes
    ExitApp

}

SetTimer, PressTheKey, 120000  ; Check for inactivity every two minutes.

Gui, New  ; Creates a new unnamed GUI.
Gui +AlwaysOnTop +ToolWindow -Border  +0x400000 -Caption -SysMenu -MaximizeBox -MinimizeBox
Gui, Margin, 0, 0

Gui, Color, EEAA99
Gui +LastFound  ; Make the GUI window the last found window for use by the line below.
WinSet, TransColor, EEAA99

; The block character is used as a border around the button.
t:=   Chr( 219 ) . Chr( 219 ) . Chr( 219 ) . Chr( 219 ) . Chr( 219 ) . Chr( 219 )
    . Chr( 219 ) . Chr( 219 ) . Chr( 219 ) . Chr( 219 )

; The button 'sits' in the center of the square formed by the block characters.
; This was necessary because the button could not placed over the background of
; blocks, so it was placed in the center of square frame of blocks.

gui, font, s1, Terminal

; Top line

Gui, Add, Text,   x00 y00 w50 h05 gText_Move, %  t

; 1st row - Left and Right lines

Gui, Add, Text,   x00 y05 w03 h05 gText_Move, %  t

; Button

gui, font, s8, Verdana
Gui, Add, Button, xp+3 yp-1 w34 h20 Default gButtonDie_Pressed, Die!

gui, font, s1, Terminal

Gui, Add, Text, x37 y05 w03 h05 gText_Move, %  t

; 2nd row - Left and Right lines

Gui, Add, Text,   x00 y10 w03 h05 gText_Move, %  t
Gui, Add, Text,   x37 y10 w03 h05 gText_Move, %  t

; 3rd row - Left and Right lines

Gui, Add, Text,   x00 y15 w03 h05 gText_Move, %  t
Gui, Add, Text,   x37 y15 w03 h05 gText_Move, %  t


; 4rd row - Left and Right lines

Gui, Add, Text,   x00 y20 w03 h05 gText_Move, %  t
Gui, Add, Text,   x37 y20 w03 h05 gText_Move, %  t

; Bottom line

Gui, Add, Text,   x00 y22 w50 h05 gText_Move, %  t


Gui, Show,, New GUI Window

Gui, Show
Return

Text_Move:
  PostMessage, 0xA1, 2,,, A
Return

PressTheKey:

  ; If the user hasn't done anything in the last minute and a half then ...
  if ( A_TimeIdlePhysical >= 90000 )
    Send, {Pause}                   ; simulate the user pressing the pause key,
                                    ; which normally doesn't actually do anything.
  Return

GuiClose:
GuiEscape:
ButtonDie_Pressed:

  SetTimer,, Off ; Turn off the pulse.
  Gui, Destroy
  ExitApp


ForceSingleInstance() {

  ; Get the pid from the 1st instance that is in the ErrorLevel

  Process, Exist, %A_ScriptName%


  ; store the pid of the instance
  
  FirstInstancePID := ErrorLevel

  
  ; if the first instance is this script ...

  if ( FirstInstancePID = DllCall( "GetCurrentProcessId" ) )

    return false

  else {
  
    ; show the window if hidden
    
    winshow, ahk_pid %FirstInstancePID% ahk_class AutoHotkeyGUI

    
    ; Activate the first instance 
    
    winactivate, ahk_pid %FirstInstancePID% ahk_class AutoHotkeyGUI
 
    
    ; Send an Escape to the acitve window to close it
    
    Send {Escape}

    
    ; we have a return for the first instance so exit the app
    
    return true

  }
}


C1ark
  • Members
  • 26 posts
  • Last active: Apr 18 2014 09:22 AM
  • Joined: 25 May 2013

An awesome script. But, Couldnt you just disable your screensaver?



nbirnel
  • Members
  • 2 posts
  • Last active: Jun 06 2013 07:28 PM
  • Joined: 03 Jun 2013

C1ark,

 

Sometimes group policy disabling the screensaver. But, here's something a bit shorter:

CoordMode, Mouse, Screen

MouseGetPos, CurrentX, CurrentY

Loop {
    Sleep, 60000
    LastX := CurrentX
    LastY := CurrentY
    MouseGetPos, CurrentX, CurrentY
    If (CurrentX = LastX and CurrentY = LastY) {
        MouseMove, 1, 1, , R
        Sleep, 100
        MouseMove, -1, -1, , R
    }
}

http:birnel.org/~noah/software/nosleep



Howardb1
  • Members
  • 13 posts
  • Last active: Jul 03 2013 07:45 AM
  • Joined: 17 May 2013

C1ark, what nbirnel said (about group policy).

nbirnel, Thanks, that is short :)

 

Anyone know a way to run one of the window screen savers.  I created my script to prevent the company's screen saver from locking my computer, but I miss the regular windows screen saver that showed before my company took it away.  I'm thinking of adding an option to my script that would display the screen saver and then turn it off when I taped a key or mouse button, or move the mouse.  I also would need to know where the normal windows screen savers are stored.



robert_ilbrink
  • Members
  • 561 posts
  • Last active: Nov 07 2019 05:14 PM
  • Joined: 05 May 2012

How about this:

SetTimer, MoveMouse, 60000 ; Run MoveMouse every 1 minute

MoveMouse:
    MouseMove, 1, 0, 1, R  ;Move the mouse one pixel to the right
    MouseMove, -1, 0, 1, R ;Move the mouse back one pixel
return


Howardb1
  • Members
  • 13 posts
  • Last active: Jul 03 2013 07:45 AM
  • Joined: 17 May 2013

Thanks!  That would work well.

 

This program went through a few evolutions, but initially I went with the control key rather than a mouse because I was more comfortable with that.  My situation frequently interrupts me while I work on my computer and my company's program would lock the computer.  However, I work from home, so there's no real security issue, and frankly I got real tired of having to always log back in to continue my work.

 

As the central part of my 'stay awake' program, your mouse solution and the pause button that I went with would both work, yours because it returns the mouse to where it was and mine becuase the pause button is ignored by any of the programs I use.

 

It may seem strange that I'm now interested in calling the Windows' Screen Saver after  some time, but now that I can keep the computer from being locked, I'd kind of like to have the screen saver running when I'm not there to see the screen.



robert_ilbrink
  • Members
  • 561 posts
  • Last active: Nov 07 2019 05:14 PM
  • Joined: 05 May 2012

You could write your own screensaver in AutoHotKey. The windows Screensaver will never kick-in since the mouse is moved every minute or so.



Howardb1
  • Members
  • 13 posts
  • Last active: Jul 03 2013 07:45 AM
  • Joined: 17 May 2013

Actually, the screen saver will never kick-in because my company has set a policy that prevents it from being run or changed in any way.  What I've done is created a program that kicks off before their program does and locks my computer.  So, yes, I basically need to run a windows screen saver on my own, but I want to run my favorite windows' screen saver instead of creating one of my own.  I don't have access to the Windows' screen saver configuration tool because of my policy, so I also need to know where the standard Windows 7 screen saver files are located.



robert_ilbrink
  • Members
  • 561 posts
  • Last active: Nov 07 2019 05:14 PM
  • Joined: 05 May 2012

I think we are talking about different things. Suppose you could "activate" the (or an other) screensaver, your own screen saver would never activate since you keep your mouse moving every minute. That is why I think you would need to create your own. Your own screen saver would need to "see" the difference between moving one pixel automatically v.s. moving multiple pixels by hand (and hence deactivating again).



Howardb1
  • Members
  • 13 posts
  • Last active: Jul 03 2013 07:45 AM
  • Joined: 17 May 2013

Now that you've 'spelled it out', yes, my script will need to know that its keeping the system awake AND run its own screen saver of some sort until the user does something to awaken it.  Too bad because I don't think that I could duplicate my favorite screen saver - its the one with the different color ribbons that drift around the screen.  But, I know how to do the simipler bouncing line version, so I'll try that.  Alternatively, there were a bunch of nice fireworks simulations floating around a few years ago.  Maybe I can find one of those.   Thanks for your comments. happy.png