Click here to Skip to main content
15,616,520 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I want to put the mouse cursor in the center of primary screen everytime the user enters for example his password (or without a password if there's no one set) or enters with fingerprints and enters the desktop of the user from lock screen so he won't have to look for the mouse cursor especially when there are multiple screens.

What I have tried:

Cursor.Position = new Point(
//Screen.PrimaryScreen.Bounds.X + 
Screen.PrimaryScreen.Bounds.Width / 2,
// Screen.PrimaryScreen.Bounds.Y +
 Screen.PrimaryScreen.Bounds.Height / 2);
Updated 15-Nov-20 11:37am
Richard MacCutchan 15-Nov-20 4:45am    
You need to find out whether Windows broadcasts a notification message to tell all applications that the screen is unlocking.
john1990_1 15-Nov-20 14:09pm    
Or I can make my program look in Even Viewer with a timer every 100 milliseconds and check if there was an unlock in the past 150 milliseconds and then if yes not do the action again for 400 milliseconds...
Richard MacCutchan 15-Nov-20 14:48pm    
Well, whatever works for you.
john1990_1 15-Nov-20 16:17pm    
I was expecting some help as what I'm finding in Google isn't working.

1 solution

Wait for the Session Switch event.[^].

static void SystemEvents_SessionSwitch(object sender, Microsoft.Win32.SessionSwitchEventArgs e)
     if (e.Reason == SessionSwitchReason.SessionUnlock)
         //unlock screen
         Console.WriteLine("Screen unlocked");
Share this answer
john1990_1 15-Nov-20 17:55pm    
Thx, I added in Main() this:
SessionSwitch += SystemEvents_SessionSwitch;
And still the event doesn't fire (not even to the breakpoint on the "if").
Dave Kreskowiak 15-Nov-20 23:32pm    
It works find in a Windows Forms app. The SystemEvents class is wrapping the WTSRegisterSessionNotification Win32 function. It requires a window with a message pump in order to work, so it will not work in a Console or Windows Service app.
john1990_1 15-Nov-20 23:33pm    
I'm running it in Windows Forms app, maybe I should move it to FormMain's class not in Program class and Main()?
Dave Kreskowiak 16-Nov-20 1:31am    
Uhhh, eah. The message pump on a Forms app doesn't start until the Run method is called.
john1990_1 16-Nov-20 1:56am    
I changed to:
void FormMain_Shown(object sender, EventArgs e)
SessionSwitch += SystemEvents_SessionSwitch;
And put it all in FormMain which is called from Run in Main(), maybe I should run another form and close it in Main() so this starts firing?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900