OK, I managed to solve thisone myself. Here is my code for Keyboard and mouse lock that requires password to unlock. This code may need some optimizing but it works fine. (I used the code from previous thread and modified it)
When the program starts it writes date and time of start in log file, and waits for 10 seconds of idle time to lock keyboard and mouse. When they are locked splash screen is displayed. If you want to unlock you must press PAUSE key (which I used because it is all the way in the corner and can't be missed), and after that type the password. If the usual pass is typed then all is unlocked but the program still counts idle time and will lock all again after 10 seconds, but if you type special secret pass, then all is unlocked and program exits.
Program writes to log file each time someone unlocks or locks PC, or if someone attempts to unlock and types the wrong pass.
#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_outfile=lockPC.exe
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#include <WinAPI.au3>
#include <Timers.au3>
#include <Date.au3>
Opt("TrayMenuMode", 1)
Opt("WinTitleMatchMode", 2)
HotKeySet("{PAUSE}", "_UnBlock")
; Values
;-------
$idleTimeValue = 10000 ; Idle time to wait until lock
$validPass = "pass" ; This password unlocks all, but program still counts idle time and will lock all again if there's no user input
$ultimatePass = "secret" ; With this password all is unlocked and program exits
$iniFile = "C:\lockPC.log" ; Location and name of log file
IniWrite($iniFile, @ComputerName & " - Log", _Now(), "Program started")
; This is the initial idle time check, after this IDLE time PC is locked
;-----------------------------------------------------------------------
while 1
if _Timer_GetIdleTime()<$idleTimeValue Then
ContinueLoop
Else
ExitLoop
EndIf
WEnd
Global $pStub_KeyProc = DllCallbackRegister("_KeyProc", "int", "int;ptr;ptr")
Global $pStub_MouseProc = DllCallbackRegister ("_Mouse_Handler", "int", "int;ptr;ptr")
Global $hHookKeyboard = _WinAPI_SetWindowsHookEx($WH_KEYBOARD_LL, DllCallbackGetPtr($pStub_KeyProc), _WinAPI_GetModuleHandle(0), 0)
Global $hHookMouse = _WinAPI_SetWindowsHookEx($WH_MOUSE_LL, DllCallbackGetPtr($pStub_MouseProc), _WinAPI_GetModuleHandle(0), 0)
SplashTextOn ( "Lock", "Keyboard and mouse locked!" & @LF & @LF & "Press PAUSE key to unlock",300,100,20,-1,1,"",14,200)
for $anim = 0 to 360
WinSetTrans("Lock","",$anim/2)
Next
IniWrite($iniFile, @ComputerName & " - Log", _Now(), "PC Locked")
While 1
Sleep(100)
WEnd
; Func _UnBlock runs password check and unlocks only keys needed for password input
;----------------------------------------------------------------------------------
Func _UnBlock()
for $anim = 360 to 0 step -1
WinSetTrans("Lock","",$anim/2)
Next
SplashOff()
DllCallbackFree($pStub_KeyProc)
_WinAPI_UnhookWindowsHookEx($hHookKeyboard)
$pStub_KeyProc = DllCallbackRegister("_noTab", "int", "int;ptr;ptr")
$hHookKeyboard = _WinAPI_SetWindowsHookEx($WH_KEYBOARD_LL, DllCallbackGetPtr($pStub_KeyProc), _WinAPI_GetModuleHandle(0), 0)
$password = InputBox("UnBlock", "Enter the password", "", "*8",-1,130)
; Password check
;----------------
if $password = $validPass Then
call("_unlockAll")
IniWrite($iniFile, @ComputerName & " - Log", _Now(), "PC Unlocked")
while 1
; Again idle time check, after this IDLE time PC is locked
;----------------------------------------------------------
if _Timer_GetIdleTime()<$idleTimeValue Then
ContinueLoop
Else
call("_unlockAll")
call("_block")
IniWrite($iniFile, @ComputerName & " - Log", _Now(), "PC Locked")
SplashTextOn ( "Lock", "Keyboard and mouse locked!" & @LF & @LF & "Press PAUSE key to unlock",300,100,20,-1,1,"",14,200)
for $anim = 0 to 360
WinSetTrans("Lock","",$anim/2)
Next
ExitLoop
EndIf
WEnd
;Exit
ElseIf $password = $ultimatePass Then
call("_unlockAll")
IniWrite($iniFile, @ComputerName & " - Log", _Now(), "Secret Password - Program end")
Exit
else
call("_unlockAll")
call("_block")
IniWrite($iniFile, @ComputerName & " - Log", _Now(), "Unlock Attempt - Wrong password")
SplashTextOn ( "Lock", "Keyboard and mouse locked!" & @LF & @LF & "Press PAUSE key to unlock",300,100,20,-1,1,"",14,200)
for $anim = 0 to 360
WinSetTrans("Lock","",$anim/2)
Next
EndIf
EndFunc
; Func _KeyProc determines which keys are locked - $vkCode <> values are keys that are UNLOCKED (Pause key [0x13] and ENTER [0x0D])
;----------------------------------------------------------------------------------------------------------------------------------
Func _KeyProc($nCode, $wParam, $lParam)
If $nCode < 0 Then Return _WinAPI_CallNextHookEx($hHookKeyboard, $nCode, $wParam, $lParam)
Local $KBDLLHOOKSTRUCT = DllStructCreate("dword vkCode;dword scanCode;dword flags;dword time;ptr dwExtraInfo", $lParam)
Local $vkCode = DllStructGetData($KBDLLHOOKSTRUCT, "vkCode")
If $vkCode <> 0x13 and $vkCode <> 0x0D Then Return 1
_WinAPI_CallNextHookEx($hHookKeyboard, $nCode, $wParam, $lParam)
EndFunc
Func _Mouse_Handler($nCode, $wParam, $lParam)
If $nCode < 0 Then Return _WinAPI_CallNextHookEx($hHookMouse, $nCode, $wParam, $lParam)
Return 1
EndFunc
; Func _block is locking everything
;----------------------------------
Func _block()
$pStub_KeyProc = DllCallbackRegister("_KeyProc", "int", "int;ptr;ptr")
$pStub_MouseProc = DllCallbackRegister ("_Mouse_Handler", "int", "int;ptr;ptr")
$hHookKeyboard = _WinAPI_SetWindowsHookEx($WH_KEYBOARD_LL, DllCallbackGetPtr($pStub_KeyProc), _WinAPI_GetModuleHandle(0), 0)
$hHookMouse = _WinAPI_SetWindowsHookEx($WH_MOUSE_LL, DllCallbackGetPtr($pStub_MouseProc), _WinAPI_GetModuleHandle(0), 0)
EndFunc
; Func _noTab turns off CTRL, Tab, Left Win, Right Win and App key during the password input, so you are stuck in the password window
;-------------------------------------------------------------------------------------------------------------------------
Func _noTab($nCode, $wParam, $lParam)
If $nCode < 0 Then Return _WinAPI_CallNextHookEx($hHookKeyboard, $nCode, $wParam, $lParam)
Local $KBDLLHOOKSTRUCT = DllStructCreate("dword vkCode;dword scanCode;dword flags;dword time;ptr dwExtraInfo", $lParam)
Local $vkCode = DllStructGetData($KBDLLHOOKSTRUCT, "vkCode")
If $vkCode = 0x11 or $vkCode = 0x09 or $vkCode = 0x5B or $vkCode = 0x5C or $vkCode = 0x5D Then Return 1
_WinAPI_CallNextHookEx($hHookKeyboard, $nCode, $wParam, $lParam)
EndFunc
; Func _unlockAll unlocks all (?! really:))
;----------------------------------------
Func _unlockAll()
DllCallbackFree($pStub_KeyProc)
DllCallbackFree($pStub_MouseProc)
_WinAPI_UnhookWindowsHookEx($hHookKeyboard)
_WinAPI_UnhookWindowsHookEx($hHookMouse)
EndFunc