Prevent Screen Saver

Zenerdiode

Active Member
Licensed User
I would like to prevent the screen saver and suspend/hibernate on the desktop. Is there anything I may call repeatedly in a timer to acomplish this?

I appreciate the Hardware library for the device has .keepalive; I'm looking for a .keepalive or .keepscreen type function for the desktop.
 

Cableguy

Expert
Licensed User
Longtime User
Use the FOCUS method and set the Forms Focus...This will bring it to the Front of the z order in the Open windows...And thus keep the desktop alive....
I think...
 

Zenerdiode

Active Member
Licensed User
I've tried this:

B4X:
Sub App_Start
   Form1.Show
   Timer1.Enabled=True 'Timer1.Interval=5000
End Sub

Sub Timer1_Tick
   Form1.Focus
End Sub

but unfortunately the screen saver still intervenes.
 

RandomCoder

Well-Known Member
Licensed User
Longtime User
You could modify the registry key HKEY_CURRENT\Control Panel\Desktop\ScreenSaveActive=0 and then restire it back to a valueof 1 when your done.
This will definately work (as I've used it to manually bypass my work PC from time to time) but there is probably a much simpler way of achieving this.

Regards,
RandomCoder
 

Zenerdiode

Active Member
Licensed User
@Cableguy
I understand your thoughts, but Form1.Focus is not bringing the window to the front.

@RandomCoder
The app I'm writing is for monitoring and capturing RS232 data streams for our work PCs too. However the Admis have gone wild with Group Policies - everything is locked down. Registry editing is one of the first things to be banned. :( The screen saver is big Flash object that cooks the CPU and I may need to monitor for hours without intervention.

Luckily our 'build' includes .Net 2.0 so I can write Basic4ppc apps :)

I would appreciate some assistance from our Door library experts. Included is a .cs solution that is reported to work - in the Timer sub it brings the Form to the front, uses SendKeys.Send presumably to simulate a keypress then restores the previously focussed window.
 

Attachments

  • KeepAlive.zip
    1.3 KB · Views: 268

Erel

B4X founder
Staff member
Licensed User
Longtime User
This code seems to work:
obj is an Object (door library).
B4X:
Sub Globals
    'Declare the global variables here.

End Sub

Sub App_Start
    Form1.Show
    obj.New1(False)
    obj.CreateNew("System.Windows.Forms.SendKeys" & obj.System_Windows_Forms)
    timer1.Interval = 60000 'one minute
    timer1.Enabled = True
End Sub

Sub Timer1_Tick
    obj.RunMethod2("Send","{INS}","System.String") 'send Insert key.
End Sub
 

Zenerdiode

Active Member
Licensed User
Thank You Erel. :) I compiled your example and for me it works as follows; the screen saver does execute but the next time the timer ticks - the SendKey is recognised and the screen saver clears. For some reason SendKey does not reset the screen saver 'time elapsed' timer. :(

Your example was exactly what I had asked for - just I hadn't done enough on the research side. Others have run into the same problem using SendKey and the solution was to inject a zero move into the mouse stream. (It doesn't have to actually move the mouse cursor) I sincerely apologise that this snippet is in Visual Basic but I'm hoping you may port it to Basic4ppc.

B4X:
Option Explicit

Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (pDst As Any, pSrc As Any, ByVal ByteLen As Long)
Private Declare Function SendInput Lib "user32.dll" (ByVal nInputs As Long, pInputs As INPUT_TYPE, ByVal cbSize As Long) As Long

Private Const MOUSEEVENTF_MOVE = &H1
Private Const INPUT_MOUSE = 0

Private Type MOUSEINPUT
  dx As Long
  dy As Long
  mouseData As Long
  dwFlags As Long
  dwtime As Long
  dwExtraInfo As Long
End Type

Private Type INPUT_TYPE
  dwType As Long
  xi(0 To 23) As Byte
End Type

Private inputEvent(0) As INPUT_TYPE

Private Sub Form_Load()
   Dim mouseEvent As MOUSEINPUT
   
   ' Load up the event record
   mouseEvent.dx = 0
   mouseEvent.dy = 0
   mouseEvent.mouseData = 0
   mouseEvent.dwFlags = MOUSEEVENTF_MOVE
   mouseEvent.dwtime = 0
   mouseEvent.dwExtraInfo = 0
    
   ' Copy the record into the input array
   inputEvent(0).dwType = INPUT_MOUSE
   CopyMemory inputEvent(0).xi(0), mouseEvent, Len(mouseEvent)
    
End Sub

Private Sub Timer1_Timer()
    Dim intX As Integer
    
    intX = SendInput(1, inputEvent(0), Len(inputEvent(0)))
End Sub
 

Zenerdiode

Active Member
Licensed User
Erel,

The line:
obj.RunMethod2("Send","{INS}","System.String")

...will sometimes leave a focussed window in overtype mode - which was not desirable.

I've changed it to:
obj.RunMethod2("Send","+","System.String")

...which sends the Shift key.

A very strange thing is happening though. If the B4PPC window is inactive; the application works as desired and the screen saver never starts. However, if the B4PPC window is the active one; the screensaver activates until the next timer_tick. I presume thats because the screensaver will have to be the topmost and active window. Why is the B4PPC window unable to act on its own SendKeys and therefore reset the screensaver countdown?
 

Zenerdiode

Active Member
Licensed User
Returning to this problem, I see I may be able to move the mouse cursor with the Cursor.Position property; getting the position and setting it just one pixel different. Would you have a go at putting this into 'Door speak' for me please?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Not sure if it will affect the screen saver:
B4X:
Sub Globals
    'Declare the global variables here.

End Sub

Sub App_Start
    Form1.Show
    cursor.New1(False)
    cursor.CreateNew("System.Windows.Forms.Cursor" & cursor.System_Windows_Forms)
    point.New1(False)
    point.Value = cursor.GetProperty("Position")
    point.SetProperty("X",point.GetProperty("X") + 100)
    point.SetProperty("Y",point.GetProperty("Y") + 100)
    cursor.SetProperty2("Position", point.Value)
End Sub
point and cursor are Objects (from the door library).
 

Zenerdiode

Active Member
Licensed User
Not sure if it will affect the screen saver:

Thanks Erel, your code does make the mouse pointer jump around - but again, very strangely, this does not affect the screensaver intervention until after it has started. :BangHead: The next move of the mouse (by the above code or physical gesture) after it cuts in will cancel the screensaver.

Others are using SendInput which I don't think is supported in .Net, whereas we have already tried SendKeys and that didn't work either.

Many Thanks for trying though. :)
 
Top