none
Disengage Screensaver RRS feed

  • Question

  • 
    

    I have an application that reads RFID cards to identify users. I have the OS configured to start a screensaver after an hour of inactivity. The problem that I am running into is that after the screensaver activates, users are going up to the machine and instead of touching the screen to deactivate the screensaver, they are scanning their card. The OS doesn't see this as an input event so the screensaver continues to run.

    Is there a way to simulate some kind of input event to tell Windows to deactivate the screensaver?

    Please not that I said deactivate, not stop.  I can find all kinds of informaiton on turning a screensaver on and off.  I don't want to effect the screensaver settings at all.  I just want to simulate an input event to get the screensaver to stop running.  When the machine is inactive again for an hour, I would like the screensaver to start again.

    Thanks.

    John

    John

    Friday, September 12, 2014 4:13 PM

Answers

  • Does your app receive input from the card scanner while the screen saver is on? If so you can send a mouse event at that point which will cause the screen saver to stop. And it will restart at whatever interval you have it set at.

    I used this code in a timer. The screen saver started one minute after the app launched. The timer interval is 1 minute and 10 seconds. 10 seconds after the screen saver began the mouse event occurs causing the screen saver to stop running for 1 minute until it starts again.

    Of course a left mouse down and left mouse up occur at whatever the location of the cursor is. So if a control is under the cursor it would be selected. You could alter that to a middle or right mouse button though if those will do the same thing. Also if you don't send an up after a down then the system or app thinks the mousebutton is still pressed I believe.

    These are the constructs for those

    Const MOUSEEVENTF_MIDDLEDOWN As Int32 = &H20
    Const MOUSEEVENTF_MIDDLEDUP As Int32 = &H40
    Const MOUSEEVENTF_ABSOLUTE As Int32 = &H8000
    Const MOUSEEVENTF_MOVE As Int32 = &H1
    Const MOUSEEVENTF_RIGHTDOWN As Int32 = &H8
    Const MOUSEEVENTF_RIGHTUP As Int32 = &H10

    Option Strict On
    
    Public Class Form1
    
        Private Declare Function apimouse_event Lib "user32" Alias "mouse_event" (ByVal dwFlags As Int32, ByVal dX As Int32, ByVal dY As Int32, ByVal cButtons As Int32, ByVal dwExtraInfo As Int32) As Boolean
        Const MOUSEEVENTF_LEFTDOWN As Int32 = &H2
        Const MOUSEEVENTF_LEFTUP As Int32 = &H4
    
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Me.Location = New Point(CInt((Screen.PrimaryScreen.WorkingArea.Width / 2) - (Me.Width / 2)), CInt((Screen.PrimaryScreen.WorkingArea.Height / 2) - (Me.Height / 2)))
            Timer1.Interval = 70000
            Timer1.Start()
        End Sub
    
        Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
            Timer1.Stop()
            apimouse_event(MOUSEEVENTF_LEFTDOWN + MOUSEEVENTF_LEFTUP, Cursor.Position.X, Cursor.Position.Y, 0, 0)
        End Sub
    
    End Class


    La vida loca



    • Edited by Mr. Monkeyboy Friday, September 12, 2014 5:42 PM
    • Marked as answer by Johngeh Wednesday, September 17, 2014 2:03 AM
    Friday, September 12, 2014 5:40 PM

All replies

  • Does your app receive input from the card scanner while the screen saver is on? If so you can send a mouse event at that point which will cause the screen saver to stop. And it will restart at whatever interval you have it set at.

    I used this code in a timer. The screen saver started one minute after the app launched. The timer interval is 1 minute and 10 seconds. 10 seconds after the screen saver began the mouse event occurs causing the screen saver to stop running for 1 minute until it starts again.

    Of course a left mouse down and left mouse up occur at whatever the location of the cursor is. So if a control is under the cursor it would be selected. You could alter that to a middle or right mouse button though if those will do the same thing. Also if you don't send an up after a down then the system or app thinks the mousebutton is still pressed I believe.

    These are the constructs for those

    Const MOUSEEVENTF_MIDDLEDOWN As Int32 = &H20
    Const MOUSEEVENTF_MIDDLEDUP As Int32 = &H40
    Const MOUSEEVENTF_ABSOLUTE As Int32 = &H8000
    Const MOUSEEVENTF_MOVE As Int32 = &H1
    Const MOUSEEVENTF_RIGHTDOWN As Int32 = &H8
    Const MOUSEEVENTF_RIGHTUP As Int32 = &H10

    Option Strict On
    
    Public Class Form1
    
        Private Declare Function apimouse_event Lib "user32" Alias "mouse_event" (ByVal dwFlags As Int32, ByVal dX As Int32, ByVal dY As Int32, ByVal cButtons As Int32, ByVal dwExtraInfo As Int32) As Boolean
        Const MOUSEEVENTF_LEFTDOWN As Int32 = &H2
        Const MOUSEEVENTF_LEFTUP As Int32 = &H4
    
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Me.Location = New Point(CInt((Screen.PrimaryScreen.WorkingArea.Width / 2) - (Me.Width / 2)), CInt((Screen.PrimaryScreen.WorkingArea.Height / 2) - (Me.Height / 2)))
            Timer1.Interval = 70000
            Timer1.Start()
        End Sub
    
        Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
            Timer1.Stop()
            apimouse_event(MOUSEEVENTF_LEFTDOWN + MOUSEEVENTF_LEFTUP, Cursor.Position.X, Cursor.Position.Y, 0, 0)
        End Sub
    
    End Class


    La vida loca



    • Edited by Mr. Monkeyboy Friday, September 12, 2014 5:42 PM
    • Marked as answer by Johngeh Wednesday, September 17, 2014 2:03 AM
    Friday, September 12, 2014 5:40 PM
  • @ MonkeyBoy

         Your code don't work for me !! (Windows 8.1)

    @ Johngeh

      this example show how to start and stop the screen saver pragmatically ( OS starting with Windows NT)

    The button start the screen saver ... The timer stop it after 10 seconds

    Option Strict On
    
    Imports System.Runtime.InteropServices
    
    Public Class Form1
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            TurnOnScreenSaver()
            Timer1.Interval = 10000
            Timer1.Start()
        End Sub
    
        Public Sub TurnOnScreenSaver()
            SendMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_SCREENSAVE, IntPtr.Zero)
        End Sub
    
    
    
        Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
            TurnOffScreenSaver()
        End Sub
    
        Private Delegate Function EnumDesktopWindowsProc(hDesktop As IntPtr, lParam As IntPtr) As Boolean
    
        Public Sub TurnOffScreenSaver()
            Dim hDesktop As IntPtr = OpenDesktop("Screen-saver", 0, False, DESKTOP_READOBJECTS Or DESKTOP_WRITEOBJECTS)
            If (hDesktop <> IntPtr.Zero) Then
                EnumDesktopWindows(hDesktop, New EnumDesktopWindowsProc(AddressOf KillScreenSaverFunc), IntPtr.Zero)
                CloseDesktop(hDesktop)
            Else
                Throw New Exception("OS not supported")
            End If
        End Sub
    
        Private Function KillScreenSaverFunc(hWnd As IntPtr, lParam As IntPtr) As Boolean
            If (IsWindowVisible(hWnd)) Then
                SendMessage(hWnd, WM_CLOSE, IntPtr.Zero, IntPtr.Zero)
            End If
            Return True
        End Function
    
    
        '===========================================
        'API Region
        '===========================================
    
        Public Const WM_SYSCOMMAND As Integer = &H112
        Public SC_SCREENSAVE As IntPtr = New IntPtr(&HF140)
        Private HWND_BROADCAST As IntPtr = New IntPtr(&HFFFF)
        Private Const WM_CLOSE As Integer = 16
        Private Const DESKTOP_WRITEOBJECTS As Integer = &H80
        Private Const DESKTOP_READOBJECTS As Integer = &H1
    
        <DllImport("user32.dll", CharSet:=CharSet.Auto)>
        Private Shared Function OpenDesktop(hDesktop As String, Flags As Integer, Inherit As Boolean, DesiredAccess As Integer) As IntPtr
        End Function
    
        <DllImport("user32.dll", CharSet:=CharSet.Auto)>
        Private Shared Function EnumDesktopWindows(hDesktop As IntPtr, callback As EnumDesktopWindowsProc, lParam As IntPtr) As Boolean
        End Function
    
        <DllImport("user32.dll", CharSet:=CharSet.Auto)>
        Private Shared Function CloseDesktop(hDesktop As IntPtr) As Boolean
        End Function
    
        <DllImport("user32.dll", CharSet:=CharSet.Auto)>
        Private Shared Function IsWindowVisible(hWnd As IntPtr) As Boolean
        End Function
    
        <DllImport("User32.dll")>
        Public Shared Function SendMessage(hWnd As IntPtr, Msg As Integer, wParam As IntPtr, lParam As IntPtr) As IntPtr
        End Function
    
    End Class

    Friday, September 12, 2014 6:32 PM
  • This code worked perfectly.  Thanks Mr. Monkeyboy.

    John

    Wednesday, September 17, 2014 2:03 AM
  • Sorry Crazypennie, your code didn't work for me. (Windows 7).

    The screen saver didn't start and when the timer ticked, I got the "OS not supported" error.


    John

    Wednesday, September 17, 2014 2:05 AM