Jump to content

AutoIT Code to Detect Mouse click on "Lock" option from Startup Menu in Windows 10


rohit8366
 Share

Recommended Posts

Hi All,

I am a novice AutoIt Scripter with decent exposure to AutoIt scripting in spare time from past few months. I create standalone applications to  automate simple windows tasks or create user convenience apps/scripts. 

I am trying to create an AutoIt script/application which will notify the user to log off his/her chat client before the user locks his/her windows screen. The Script/Application will have to perform two checks:

1)  User locks screen using Keyboard

(I was able to achieve this functionality by using _isPressed() function to detect when Windows button is pressed, could not use the HotkeySet() because of restrictions). Frankly used the sample example from _IsPressed() help file. 

#include <Misc.au3>
#include <MsgBoxConstants.au3>

Local $hDLL = DllOpen("user32.dll")

While 1
    If _IsPressed("5B", $hDLL)  Then
        ConsoleWrite("_IsPressed - Left Windows Key was pressed." & @CRLF)
        MsgBox($MB_ICONWARNING, "Left Windows Key was pressed", "The Left Windows Key was pressed, Please Logoff from your chat")
        ; Wait until key is released.
        While _IsPressed("10", $hDLL)
            Sleep(250)
        WEnd
        ConsoleWrite("_IsPressed - Windows Key was released." & @CRLF)
   ElseIf _IsPressed("5C", $hDLL)  Then
        ConsoleWrite("_IsPressed - Right Windows Key was pressed." & @CRLF)
        MsgBox($MB_ICONWARNING, "Right Windows Key was pressed", "The Right Windows Key was pressed, Please Logoff from your chat")
        ; Wait until key is released.
        While _IsPressed("10", $hDLL)
            Sleep(250)
        WEnd
        ConsoleWrite("_IsPressed - Windows Key was released." & @CRLF)
    ElseIf _IsPressed("1B", $hDLL) Then
        MsgBox($MB_SYSTEMMODAL, "_IsPressed", "The Esc Key was pressed, therefore alert application will be terminated.")
        ExitLoop
    EndIf
    Sleep(250)
WEnd

DllClose($hDLL)

 

2) User locks screen using the startup menu with help of mouse 

Now this is the challenging part which has got me stuck up, I want to come up with a way to detect the mouse hover clicking on the startup menu , then the user clicking on his/her windows login name(Windows10) and finally when the mouse hovers on "Lock" option from the menu, I need to display a Message box as in the above snippet(Hope the line was not confusing). Can this function be achieved using AutoIt? :)

I will also have to implement this functionality in Windows 7 as well, if this is achievable.

Any Help/guidance will be greatly appreciated.

Thanks,

Rohit

Link to comment
Share on other sites

You would have to have a couple modes in which this script would work, seeing as how Windows 10 allows for different ways to display the start up menu.
You could then map out the screen, detect a mouse click within a certain range, and then wait for mouse to hover in a different square.

Action example;
-Start up menu opens,
-User hovers over profile icon (within a square detected by AutoIt)
-Wait for mouse click
-Wait for mouse to move over "Lock" (within a square detected by AutoIt)
-Perform desired action

You might wanna add a timeout, in case they change their mind. You could wait for a color change (where the "lock" dialog that pops up, for example), and if the color changes again to an undesired color, restart the sequence.
 

Link to comment
Share on other sites

Note it's possible to lock the pc with WIN+L

Also the way you are doing it now it is possible the script won't see the _IsPressed in the 250ms sleep. I suggest event based instead. I've seen it done on the forum before, but i'm not sure if it falls under keylogger-ish behavor.

All in all i don't think you can prevent a lock screen, though you can detect it with "WTSRegisterSessionNotification" and the "WM_WTSSESSION_CHANGE" message.

So you could maybe make your script log the client out on lock screen event and log back on

Link to comment
Share on other sites

On 10/1/2016 at 6:13 AM, Aareon said:

You would have to have a couple modes in which this script would work, seeing as how Windows 10 allows for different ways to display the start up menu.
You could then map out the screen, detect a mouse click within a certain range, and then wait for mouse to hover in a different square.

Action example;
-Start up menu opens,
-User hovers over profile icon (within a square detected by AutoIt)
-Wait for mouse click
-Wait for mouse to move over "Lock" (within a square detected by AutoIt)
-Perform desired action

You might wanna add a timeout, in case they change their mind. You could wait for a color change (where the "lock" dialog that pops up, for example), and if the color changes again to an undesired color, restart the sequence.
 

@Aareon Thank you so much for the above suggestion and apologies for my delayed response.

As far as I can understand, You are suggesting me to follow the above algorithm identify the action of screen lock using start up menu.

The problem with above approach is that the screen resolution for different PCs is different based on Monitor size, I want to develop something more generic.  Please do correct me if I am interpreting something incorrect.

Thank you again for your advise :)

 

Link to comment
Share on other sites

On 10/2/2016 at 10:17 AM, genius257 said:

Note it's possible to lock the pc with WIN+L

Also the way you are doing it now it is possible the script won't see the _IsPressed in the 250ms sleep. I suggest event based instead. I've seen it done on the forum before, but i'm not sure if it falls under keylogger-ish behavor.

All in all i don't think you can prevent a lock screen, though you can detect it with "WTSRegisterSessionNotification" and the "WM_WTSSESSION_CHANGE" message.

So you could maybe make your script log the client out on lock screen event and log back on

@genius257 I hope the above situation does not fall under the keylogger-ish behavior. Don't Want to break the forum rules :sweating:

All I want to do is create an App/AutoIt script to identify the user action of user "Locking" his/her windows screen and Notifying him/her via a message box to simply log out of his/her Chat application, before locking the Windows Screen

Link to comment
Share on other sites

10 hours ago, rohit8366 said:

@Aareon Thank you so much for the above suggestion and apologies for my delayed response.

As far as I can understand, You are suggesting me to follow the above algorithm identify the action of screen lock using start up menu.

The problem with above approach is that the screen resolution for different PCs is different based on Monitor size, I want to develop something more generic.  Please do correct me if I am interpreting something incorrect.

Thank you again for your advise :)

 

AutoIt allows you to get the DPI of the screen, which you can use to move the "map" around according to each user screen. Hope that helps.

Edit:
_WinAPI_GetThemeSysSize may be what I was referring to. Again, really hope this helps.

Edited by Aareon
Link to comment
Share on other sites

Also, keylogging is simply capturing key presses and logging them in order to capture passwords and other critical personal information. Simply waiting for a keypress such as Win+L is not keylogging. Feel free to correct me if I'm wrong.

Link to comment
Share on other sites

12 hours ago, Aareon said:

AutoIt allows you to get the DPI of the screen, which you can use to move the "map" around according to each user screen. Hope that helps.

Edit:
_WinAPI_GetThemeSysSize may be what I was referring to. Again, really hope this helps.

Thanks for providing the insights, I will try look the help for more information on the _WinAPI methods

Link to comment
Share on other sites

12 hours ago, Aareon said:

Also, keylogging is simply capturing key presses and logging them in order to capture passwords and other critical personal information. Simply waiting for a keypress such as Win+L is not keylogging. Feel free to correct me if I'm wrong.

Yes even I believe the same and, hence posted the question on the Forum. :D

Link to comment
Share on other sites

I did not say your code could be keylogger-ish @rohit8366.

On 2/10/2016 at 6:47 AM, genius257 said:

I suggest event based instead. I've seen it done on the forum before, but i'm not sure if it falls under keylogger-ish behavor.

And I'm sorry i have not replied until now. I was looking for some code to show what i meant.

;Initialization
$hStub_KeyProc = DllCallbackRegister("_KeyProc", "long", "int;wparam;lparam")
$hmod = _WinAPI_GetModuleHandle(0)
$hHook = _WinAPI_SetWindowsHookEx($WH_KEYBOARD_LL, DllCallbackGetPtr($hStub_KeyProc), $hmod)


Func _KeyProc($nCode, $wParam, $lParam)
    Local $tKEYHOOKS, $keyCode
    $tKEYHOOKS = DllStructCreate($tagKBDLLHOOKSTRUCT, $lParam)
    If $nCode < 0 Then
        Return _WinAPI_CallNextHookEx($hHook, $nCode, $wParam, $lParam)
    EndIf
    If $wParam = $WM_KEYDOWN then
        Local $keyCode = DllStructGetData($tKEYHOOKS, "vkCode")
        Switch $keyCode
            Case 32; space
                ;Do something...
            ; Insert more cases here...
        EndSwitch
    EndIf
    Return _WinAPI_CallNextHookEx($hHook, $nCode, $wParam, $lParam)
EndFunc


;Cleanup
_WinAPI_UnhookWindowsHookEx($hHook)
DllCallbackFree($hStub_KeyProc)

It should capture any key press. So i would say keylogger-ish?

But now that I've found my code, i can find it is also used in an example so it should be fine: _WinAPI_GetModuleHandle

I could look into the mouse thing as well soon if i find the time :)

Edited by genius257
Edited due to spelling errors *blush*
Link to comment
Share on other sites

On 10/6/2016 at 1:50 AM, genius257 said:

I did not say your code could be keylogger-ish @rohit8366.

And I'm sorry i have not replied until now. I was looking for some code to show what i meant.

;Initialization
$hStub_KeyProc = DllCallbackRegister("_KeyProc", "long", "int;wparam;lparam")
$hmod = _WinAPI_GetModuleHandle(0)
$hHook = _WinAPI_SetWindowsHookEx($WH_KEYBOARD_LL, DllCallbackGetPtr($hStub_KeyProc), $hmod)


Func _KeyProc($nCode, $wParam, $lParam)
    Local $tKEYHOOKS, $keyCode
    $tKEYHOOKS = DllStructCreate($tagKBDLLHOOKSTRUCT, $lParam)
    If $nCode < 0 Then
        Return _WinAPI_CallNextHookEx($hHook, $nCode, $wParam, $lParam)
    EndIf
    If $wParam = $WM_KEYDOWN then
        Local $keyCode = DllStructGetData($tKEYHOOKS, "vkCode")
        Switch $keyCode
            Case 32; space
                ;Do something...
            ; Insert more cases here...
        EndSwitch
    EndIf
    Return _WinAPI_CallNextHookEx($hHook, $nCode, $wParam, $lParam)
EndFunc


;Cleanup
_WinAPI_UnhookWindowsHookEx($hHook)
DllCallbackFree($hStub_KeyProc)

It should capture any key press. So i would say keylogger-ish?

But now that I've found my code, i can find it is also used in an example so it should be fine: _WinAPI_GetModuleHandle

I could look into the mouse thing as well soon if i find the time :)

Hey Thanks for the reply @genius257. What matters is your help and guidance :) 

Even I have been very busy in the Office with extra work and was not able to work on this piece of code. The above code is a new sea of knowledge for me to explore, as I have not used Win API functions until now.

I am loving AutoIt!

 

 

Link to comment
Share on other sites

3 hours ago, rohit8366 said:

Hey Thanks for the reply @genius257. What matters is your help and guidance :) 

Even I have been very busy in the Office with extra work and was not able to work on this piece of code. The above code is a new sea of knowledge for me to explore, as I have not used Win API functions until now.

I am loving AutoIt!

Well I'm happy to hear :)

I've included an example with mouse hook. I know it's basically the same principle, but at least I've added the struct for you ;)

#include <WindowsConstants.au3>
#include <WinAPI.au3>

Opt("GuiOnEventMode", 1)
$hWnd = GUICreate("", 700, 320)
GUISetOnEvent(-3, "_MyExit", $hWnd)

$hLowLevelMouseProc = DllCallbackRegister("LowLevelMouseProc", ( ( (@OSArch="IA64") Or (@OSArch="X64") )? "INT64" : "LONG" ), "INT;DWORD;DWORD")
$hMod = _WinAPI_GetModuleHandle(0)
$hHook = _WinAPI_SetWindowsHookEx($WH_MOUSE_LL, DllCallbackGetPtr($hLowLevelMouseProc), $hMod)

GUISetState()

While 1
    Sleep(10)
WEnd

Func LowLevelMouseProc($nCode, $wParam, $lParam)
    Local $tagMSLLHOOKSTRUCT = "STRUCT;LONG X;LONG Y;ENDSTRUCT;DWORD mouseData;DWORD flags;DWORD time;"&( ( (@OSArch="IA64") Or (@OSArch="X64") )? "UINT64" : "UINT" )&" dwExtraInfo"
    $tMSLLHOOKSTRUCT = DllStructCreate($tagMSLLHOOKSTRUCT, $lParam)
    ConsoleWrite("LowLevelMouseProc"&@CRLF&@TAB&"$nCode: "&$nCode&@CRLF&@TAB&"$wParam: "&$wParam&@CRLF&@TAB&"$lParam: "&$lParam&@CRLF)
    ConsoleWrite(@TAB&"X: "&$tMSLLHOOKSTRUCT.X&@CRLF&@TAB&"Y: "&$tMSLLHOOKSTRUCT.Y&@CRLF)
    Return _WinAPI_CallNextHookEx($hHook, $nCode, $wParam, $lParam)
EndFunc

Func _MyExit()
    _WinAPI_UnhookWindowsHookEx($hHook)
    DllCallbackFree($hLowLevelMouseProc)
    Exit
EndFunc

with this you can get notified whenever a move or click is being done. this combined with something like _WinAPI_WindowFromPoint and/or _WinAPI_GetClassName.

Then you could match for [CLASS:Windows.UI.Core.CoreWindow] (the start menu in windows 10) and if true check position.

Just a suggestion ^^

You can read more about the information from the hook here: SetWindowsHookEx function, here: LowLevelMouseProc callback function and here: MSLLHOOKSTRUCT structure

Edited by genius257
Forgot the code block *blush*
Link to comment
Share on other sites

On 9/27/2016 at 7:56 PM, rohit8366 said:

I am trying to create an AutoIt script/application which will notify the user to log off his/her chat client before the user locks his/her windows screen.

let's ponder this for a minute. why should the user log-off the chat client before locking the workstation?

the reason i'm asking is this: is the chat client directly impacted by the "locked" state? or is the chat client impacted by user inactivity for some time?

if it's the former, you are in trouble - you cannot prevent locking a workstation (or if you can, it should be extremely complicated, and most probably malicious). you can detect it, but not prevent it.

if it's the latter, i think you are taking the wrong approach here. this is what i would do:

first, check if the chat client can be logged-off by a script, using ControlSend and such, that would work even in a "locked" state.

if so, write an AutoIt script that will launch at user logon and will run silent in the background. script logic as follows (pseudo-code):

if idle time reached then
    ask user: "do you want to keep working with your chat client?"
    unless user press YES, close the chat client gracefully.

reasoning: if in "locked" state, the MsgBox should time-out without the user even noticing it.

this is run constantly in a loop, of course.

 

Signature - my forum contributions:

Spoiler

UDF:

LFN - support for long file names (over 260 characters)

InputImpose - impose valid characters in an input control

TimeConvert - convert UTC to/from local time and/or reformat the string representation

AMF - accept multiple files from Windows Explorer context menu

DateDuration -  literal description of the difference between given dates

Apps:

Touch - set the "modified" timestamp of a file to current time

Show For Files - tray menu to show/hide files extensions, hidden & system files, and selection checkboxes

SPDiff - Single-Pane Text Diff

 

Link to comment
Share on other sites

  • 1 year later...

Hi All,

I am so sorry for replying SUPER late again on my post, but after creating the above thread I quit the Job in few months and hence the project was dropped as I was developing an AutoIT script to help my previous colleagues, from getting shouted at for not logging out of a chat application as it is normal human tendency to simply run out of his/her system and forget to logout of a chat application. I am really thankful for all the active members for guiding me and helping me on the above script which I wanted to create, You all are the best !

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...