Sign in to follow this  
Followers 0

BlockInputEx UDF!

68 posts in this topic

Posted (edited) · Report post

Hi,

This UDF originaly started in this thread, on the latest post you can find rasim's examples and my initial BlockInputEx udf example. But eventualy i improved the UDF and i thought that it would be better to place it here, in Examples forum.

This UDF supports few features that built-in BlockInput() function does not.
Here is a quick "features list" if you can call it like this :):

  • Block seperately mouse or keyboard input.
  • Block specific keyboard/mouse keys/clicks.
    [+] Not only hex keys are supported, string keys (such as {ENTER}) is also valid.
  • Block all keyboard/mouse keys *except* specific keys/events.
  • Block keys by CLASS Name (see UDF documentation).
  • Block inputs only for specific window.
  • BlockInput does not re-enables input after pressing Ctrl+Alt+Del.

Note: This UDF, same as built-in BlockInput function, can not block Ctrl+Alt+Del, however, it will not re-enable the input.

Example #1 (hWindows usage):








#include <BlockInputEx.au3>

;================== hWindows usage Example ==================

HotKeySet("{ESC}", "_Quit") ;This will trigger an exit (on any window except the window of Notepad).

Run(@WindowsDir & "\Notepad.exe", "", @SW_MAXIMIZE)
WinWait("[REGEXPCLASS:Notepad.*]")
$hNotepad_Wnd = WinGetHandle("[REGEXPCLASS:Notepad.*]")

ControlSetText($hNotepad_Wnd, "", "", _
	"Now try to input some keys in here..." & @CRLF & _
	"Well, that's the idea, you can't, don't you?" & @CRLF & @CRLF & ":)")

;Here we block *All* keyboard keys for specific window (in this case the Notepad's window).
_BlockInputEx(3, "", "", $hNotepad_Wnd)

;This is only for testing, so if anything go wrong, the script will exit after 10 seconds.
AdlibRegister("_Quit", 10000)

While 1
    Sleep(100)
WEnd

Func _Quit()
	Exit
EndFunc

Example #2 (CLASSes usage):








#include <BlockInputEx.au3>

;================== CLASSes usage Example ==================

HotKeySet("{ESC}", "_Quit");This will trigger an exit

;Here we block Numeric keyboard keys, "Test" string (every char in this group), and UP / DOWN arrow keys.
_BlockInputEx(3, "", "[:NUMBER:]|[Test]|{UP}|{DOWN}")

;This is only for testing, so if anything go wrong, the script will exit after 10 seconds.
AdlibRegister("_Quit", 10000)

While 1
	Sleep(100)
WEnd

Func _Quit()
	Exit
EndFunc

Download (In the archive there is 11 nice examples including the two above):
[attachment=40371:BlockInputEx_UDF_1.8.zip]
[attachment=33607:BlockInputEx_UDF_1.6.zip]
[attachment=32065:_BlockInputEx_UDF_1.5.zip]
[attachment=31350:_BlockInputEx_UDF_1.4.zip]
[attachment=28001:_BlockInputEx_UDF_1.3.zip]
[attachment=24093:_BlockIn..._UDF_1.2.zip]
[attachment=24087:_BlockIn..._UDF_1.1.zip]
[attachment=24072:_BlockInputEx_UDF.zip]


Enjoy! coffee.gif

=================================================================

ChangeLog:[spoiler]



[v1.8 - 12.05.2013, 12:16]
* Fixed issue when some numeric keyboard keys was not blocked.
* Fixed issue when ALT+Tab was not blocked.

[v1.6 - 04.04.2011, 22:30]
* Fixed (again, caused by fix with re-enabled input) an issue with held down "Alt + Ctrl" keys after the user was called "Alt + Ctrl + Del".
* Fixed an issue with wrong parameter passed to _WinAPI_CallNextHookEx. Thanks to Ascend4nt.
+ Added remarks to the UDF header.
+ Added "Example - Block TaskMan.au3".

[v1.5 - 11.10.2010, 22:20]
* Fixed an issue with re-enabled input after pressing Ctrl+Alt+Del.
* Now the $hWindows parameter can be used for mouse blocking.
---For that reason the $i_MouseHookGetAncestorHwnd variable was added to the UDF,
---if user sets it to 0, then the $hWindows is compared to currently hovered window handle, otherwise (1 - default) it's compared to ancestor of the hovered window/control.

[v1.4 - 03.08.2010, 12:00]
+ AutoIt 3.3.6.1 support.
* Fixed an issue with held down "Alt + Ctrl" keys after the user was called "Alt + Ctrl + Del". It was causing a problems to use HotKeySet later.
* Fixed examples to be compatible with AutoIt 3.3.6.1.
* Minor "cosmetic" changes.

[v1.3 - 24.09.2009, 23:00]
+ Added _BlockInputEx Example (Pass Lock)
* Fixed few examples.
* Fixed spell mistakes in the UDF.

[v1.2 - 16.01.2009, 21:00]
+ Added key strings support.
------Now users can set simple hotkey strings for Exclude/Include parameters + Group chars, i.e: "[Group]|{UP}|{DOWN}"
-------(See UDF documentation)
+ Added mouse events blocking support to the $sExclude/$sInclude parameters.
+ Added example for mouse events blocking.

[v1.1 - 16.01.2009]
+ Added CLASSes support (see UDF documentation), thanks to FireFox for the idea and the keys lists.
+ Added example for CLASS usage.
* Changed behaviour of $iBlockMode parameter as following:
 








1  - Block All
<= 0  - UnBlock all
2  - Block only mouse
3  - Block only keyboard.
* Fixed hard-crash that related to incorrect BlockInput releasing process (only callbacks was released, not the window hooks).

[v1.0 - 15.01.2009]
First release.

 

[/spoiler]

Edited by MrCreatoR
anhyeuem, yahaosoft, lancebh and 1 other like this

Share this post


Link to post
Share on other sites



Posted · Report post

@MrCreator
Nice one :) I will add some functions to this udf

Cheers, FireFox.

Share this post


Link to post
Share on other sites

Posted (edited) · Report post

@MrCreator

UDF :
#include-once
#include <WindowsConstants.au3>
#include <WinAPI.au3>
;

#Region ======================== BlockInputEx UDF Info ========================
; Extended (advanced) function to block mouse & keyboard inputs.
;
; AutoIt Version: 3.2.12.1+
; Author: G.Sandler (a.k.a MrCreatoR). Initial idea and hooks example by rasim.
#EndRegion ======================== BlockInputEx UDF Info ========================
;

Global $ah_MouseKeyboard_WinHooks[7]

; #FUNCTION# ====================================================================================================
================
; Name...........: _BlockInputEx
; Description ...: Disable/enable the mouse and/or keyboard. Supporting blocking (by include/exclude) of seperate keys.
; Syntax.........: _BlockInputEx( [iBlockMode [, sExclude [, sInclude [, hWindows]]]] )
; Parameters ....: $iBlockMode - Set the block mode. -1 = UnBlock all
;                                        1 = Block All
;                                        2 = Block only mouse
;                                        3 = Block only keyboard
;                                        4 = Block only func keys
;                                        5 = Block only alpha keys
;                                        6 = Block only numeric keys
;                                        7 = Block only arrow keys
;                                        8 = Block only special keys
;                  $sExclude   - Keys hex-list (| delimited) to *exclude* when blocking (Only for keyboard blocking).
;                                 [*] All keys will be blocked, except the keys in $sExclude list.
;                                 [!] The hex keys list can be found in _IsPressed documentation section.
;                          [/] For $iBlockMode = 4 to 8, $sExclude will be enable for 1 / disabled for <> 1
;                  $sInclude   - Keys hex-list (| delimited) to *include* when blocking (Only for keyboard blocking).
;                                 [*] Only these keys will be blocked, the $sExclude ignored in this case.
;                                 [!] The hex keys list can be found in _IsPressed documentation section.
;                          [/] For $iBlockMode = 4 to 8, $sInclude will be enable for 1 / disabled for <> 1
;                  $hWindows   - Window handles list (| delimited) to limit the blocking process (Only for keyboard blocking).
; Return values .: Success     - 1.
;                  Failure     - 0 and set @error to 1. This can happend only when passing wrong parameters.
; Author(s)......: Rasim, MrCreator, FireFox
; Modified.......: MrCreator (15/01/09) ; FireFox (15/01/09)
; Remarks .......: This UDF includes OnAutoItExit function to release the callback/hooks resources.
;                    [*] See also native BlockInput() documentation.
; Related .......: BlockInput?
; ====================================================================================================
===========================
Func _BlockInputEx($iBlockMode = -1, $sExclude = '', $sInclude = '', $hWindows = '')
    If $iBlockMode < -1 Or $iBlockMode > 8 Then Return SetError(1, 0, 0) ;Only -1, 0, 1 and 2 modes are supported.
    
    If $iBlockMode = -1 Then Return _BlockInput_UnhookWinHooks_Proc()
    
    Local $pStub_KeyProc = 0, $pStub_MouseProc = 0, $hHook_Keyboard = 0, $hHook_Mouse = 0
    Local $hHook_Module = _WinAPI_GetModuleHandle(0)
    
    For $i = 0 To 3
        If $ah_MouseKeyboard_WinHooks[$i] > 0 Then
            _BlockInput_UnhookWinHooks_Proc()
            ExitLoop
        EndIf
    Next
    
    If $iBlockMode = 1 Or $iBlockMode = 2 Then
        $pStub_MouseProc = DllCallbackRegister("_MouseHook_Proc", "int", "int;ptr;ptr")
        $hHook_Mouse = _WinAPI_SetWindowsHookEx($WH_MOUSE_LL, DllCallbackGetPtr($pStub_MouseProc), $hHook_Module, 0)
    EndIf
    
    If $iBlockMode = 1 Or $iBlockMode = 3 Then
        $pStub_KeyProc = DllCallbackRegister("_KeyBoardHook_Proc", "int", "int;ptr;ptr")
        $hHook_Keyboard = _WinAPI_SetWindowsHookEx($WH_KEYBOARD_LL, DllCallbackGetPtr($pStub_KeyProc), $hHook_Module, 0)
    ElseIf $iBlockMode = 4 Then
        $pStub_KeyProc = DllCallbackRegister("_KeyBoardHook_Proc", "int", "int;ptr;ptr")
        $hHook_Keyboard = _WinAPI_SetWindowsHookEx($WH_KEYBOARD_LL, DllCallbackGetPtr($pStub_KeyProc), $hHook_Module, 0)
        If $sExclude = 1 Then
            $sExclude = '0x70|0x71|0x72|0x73|0x74|0x75|0x76|0x77|0x78|0x79|0x7A|0x7B|0x7C|0x7D|0x7E|0x7F' & _
                    '|0x80H|0x81H|0x82H|0x83H|0x84H|0x85H|0x86H|0x87H'
        ElseIf $sInclude = 1 Then
            $sInclude = '0x70|0x71|0x72|0x73|0x74|0x75|0x76|0x77|0x78|0x79|0x7A|0x7B|0x7C|0x7D|0x7E|0x7F' & _
                    '|0x80H|0x81H|0x82H|0x83H|0x84H|0x85H|0x86H|0x87H'
        EndIf
    ElseIf $iBlockMode = 5 Then
        $pStub_KeyProc = DllCallbackRegister("_KeyBoardHook_Proc", "int", "int;ptr;ptr")
        $hHook_Keyboard = _WinAPI_SetWindowsHookEx($WH_KEYBOARD_LL, DllCallbackGetPtr($pStub_KeyProc), $hHook_Module, 0)
        If $sExclude = 1 Then
            $sExclude = '0x41|0x42|0x43|0x44|0x45|0x46|0x47|0x48|0x49|0x4A|0x4B|0x4C|0x4D|0x4E|0x4F|0x50' & _
                    '|0x51|0x52|0x53|0x54|0x55|0x56|0x57|0x58|0x59|0x5A'
        ElseIf $sInclude = 1 Then
            $sInclude = '0x41|0x42|0x43|0x44|0x45|0x46|0x47|0x48|0x49|0x4A|0x4B|0x4C|0x4D|0x4E|0x4F|0x50' & _
                    '|0x51|0x52|0x53|0x54|0x55|0x56|0x57|0x58|0x59|0x5A'
        EndIf
    ElseIf $iBlockMode = 6 Then
        If $sExclude = 1 Then
            $sExclude = '0x30|0x31|0x32|0x33|0x34|0x35|0x36|0x37|0x38|0x39|0x60|0x61|0x62|0x63|0x64|0x65' & _
                    '|0x66|0x67|0x68|0x69'
        ElseIf $sInclude = 1 Then
            $sInclude = '0x30|0x31|0x32|0x33|0x34|0x35|0x36|0x37|0x38|0x39|0x60|0x61|0x62|0x63|0x64|0x65' & _
                    '|0x66|0x67|0x68|0x69'
        EndIf
    ElseIf $iBlockMode = 7 Then
        If $sExclude = 1 Then
            $sExclude = '0x25|0x26|0x27|0x28'
        ElseIf $sInclude = 1 Then
            $sInclude = '0x25|0x26|0x27|0x28'
        EndIf
    ElseIf $iBlockMode = 8 Then
        If $sExclude = 1 Then
            $sExclude = '0x08|0x09|0x0C|0x0D|0x10|0x11|0x12|0x13|0x14|0x1B|0x20|0x21|0x22|0x23|0x24' & _
                    '|0x29|0x2A|0x2B|0x2C|0x2D|0x2E|0x5B|0x5C|0x6A|0x6B|0x6C|0x6D|0x6E|0x6F|0x90|0x91|0xA0' & _
                    '|0xA1|0xA2|0xA3|0xA4|0xA5|0xBA|0xBB|0xBC|0xBD|0xBE|0xBF|0xC0|0xDB|0xDC|0xDD'
        ElseIf $sInclude = 1 Then
            $sInclude = '0x08|0x09|0x0C|0x0D|0x10|0x11|0x12|0x13|0x14|0x1B|0x20|0x21|0x22|0x23|0x24' & _
                    '|0x29|0x2A|0x2B|0x2C|0x2D|0x2E|0x5B|0x5C|0x6A|0x6B|0x6C|0x6D|0x6E|0x6F|0x90|0x91|0xA0' & _
                    '|0xA1|0xA2|0xA3|0xA4|0xA5|0xBA|0xBB|0xBC|0xBD|0xBE|0xBF|0xC0|0xDB|0xDC|0xDD'
        EndIf
    EndIf
    
    $ah_MouseKeyboard_WinHooks[0] = $pStub_KeyProc
    $ah_MouseKeyboard_WinHooks[1] = $pStub_MouseProc
    $ah_MouseKeyboard_WinHooks[2] = $hHook_Keyboard
    $ah_MouseKeyboard_WinHooks[3] = $hHook_Mouse
    $ah_MouseKeyboard_WinHooks[4] = "|" & $sInclude & "|"
    $ah_MouseKeyboard_WinHooks[5] = "|" & $sExclude & "|"
    $ah_MouseKeyboard_WinHooks[6] = "|" & $hWindows & "|"
    
    Return 1
EndFunc   ;==>_BlockInputEx

;KeyBoard hook processing function
Func _KeyBoardHook_Proc($nCode, $wParam, $lParam)
    If $nCode < 0 Then Return _WinAPI_CallNextHookEx($ah_MouseKeyboard_WinHooks[4], $nCode, $wParam, $lParam)
    
    Local $KBDLLHOOKSTRUCT = DllStructCreate("dword vkCode;dword scanCode;dword flags;dword time;ptr dwExtraInfo", $lParam)
    Local $vkCode = "0x" & Hex(DllStructGetData($KBDLLHOOKSTRUCT, "vkCode"), 2)
    
    Local $sInclude = $ah_MouseKeyboard_WinHooks[4]
    Local $sExclude = $ah_MouseKeyboard_WinHooks[5]
    Local $hWnds = $ah_MouseKeyboard_WinHooks[6]
    
    If $sInclude <> "||" Then ;Include proc
        If StringInStr($sInclude, "|" & $vkCode & "|") And _
                ($hWnds = "||" Or StringInStr($hWnds, "|" & WinGetHandle("[ACTIVE]") & "|")) Then Return 1 ;Block processing!
    Else ;Exclude proc
        If Not StringInStr($sExclude, "|" & $vkCode & "|") And _
                ($hWnds = "||" Or StringInStr($hWnds, "|" & WinGetHandle("[ACTIVE]") & "|")) Then Return 1 ;Block processing!
    EndIf
    
    _WinAPI_CallNextHookEx($ah_MouseKeyboard_WinHooks[2], $nCode, $wParam, $lParam) ;Continue processing
EndFunc   ;==>_KeyBoardHook_Proc

;Mouse hook processing function
Func _MouseHook_Proc($nCode, $wParam, $lParam)
    If $nCode < 0 Then Return _WinAPI_CallNextHookEx($ah_MouseKeyboard_WinHooks[3], $nCode, $wParam, $lParam) ;Continue processing
    
    Return 1 ;Block processing!
EndFunc   ;==>_MouseHook_Proc

;Releases callbacks and Unhook Windows hooks
Func _BlockInput_UnhookWinHooks_Proc()
    If $ah_MouseKeyboard_WinHooks[0] > 0 Then DllCallbackFree($ah_MouseKeyboard_WinHooks[0])
    If $ah_MouseKeyboard_WinHooks[1] > 0 Then DllCallbackFree($ah_MouseKeyboard_WinHooks[1])
    
    If $ah_MouseKeyboard_WinHooks[2] > 0 Then _WinAPI_UnhookWindowsHookEx($ah_MouseKeyboard_WinHooks[2])
    If $ah_MouseKeyboard_WinHooks[3] > 0 Then _WinAPI_UnhookWindowsHookEx($ah_MouseKeyboard_WinHooks[3])
    
    For $i = 0 To 3
        $ah_MouseKeyboard_WinHooks[$i] = 0
    Next
    
    Return 1
EndFunc   ;==>_BlockInput_UnhookWinHooks_Proc

;Called when script exits to release resources.
Func OnAutoItExit()
    _BlockInputEx(0)
EndFunc   ;==>OnAutoItExitƒo݊÷ ØLZš™^jëhŠ×6#include <BlockInputEx.au3>

;================== hWindows usage Example ==================

HotKeySet("{ESC}", "_Quit") ;This will trigger an exit

;Here we block alphabetic keyboard keys :
_BlockInputEx(5, 0, 1)

;This is only for testing, so if anything go wrong, the script will exit after 10 seconds.
AdlibEnable("_Quit", 10000)

While 1
    Sleep(100)
WEnd

Func _Quit()
    Exit
EndFunc


Cheers, FireFox. Edited by FireFox

Share this post


Link to post
Share on other sites

Posted · Report post

MrCreator, Thank you! :)

This is perfect because I need lock out my mouse and keyboard whenever my niece comes over to watch movies.

Share this post


Link to post
Share on other sites

Posted · Report post

I must be missing something...execution stops when I respond to the first MsgBox below??
Can you help?

RWBaker

#include <BlockInputEx.au3>
MsgBox(0,"","Start blocking for 5 seconds...")
_BlockInputEx(-1)
Sleep(5000)
_BlockInputEx(0)
MsgBox(0,"","Input unblocked now.") ; execution is terminated after response to this statement???
MsgBox(0,"","Input unblocked now 2.") ; this statement is not executed???
Exit

Share this post


Link to post
Share on other sites

Posted (edited) · Report post

Update!

History version:

[v1.1 - 16.01.2009]
+ Added CLASSes support (see UDF documentation), thanks to FireFox for the idea and the keys lists.
+ Added example for CLASS usage.
* Changed behaviour of $iBlockMode parameter as following:

1  - Block All
<= 0  - UnBlock all
2  - Block only mouse
3  - Block only keyboard.


* Fixed hard-crash that related to incorrect BlockInput releasing process (only callbacks was released, not the window hooks).

[v1.0 - 15.01.2009]
First release.


Please check the first post. Edited by MrCreatoR

Share this post


Link to post
Share on other sites

Posted · Report post

@MrCreator
Have you seen my reply ?

Cheers, FireFox.

Share this post


Link to post
Share on other sites

Posted · Report post

@MrCreator
Have you seen my reply ?

Cheers, FireFox.

Sure i have, how then i could add the classes support? :) If you got the last UDF version you could notice it :lmao: - Thanks.

P.S
I am working on a new version, where we can set as include/exclude parameters a simple key strings (such as {Enter}, or even a Group like in RegExpReplace, i.e: "[Test]", here the chars t,e and s will be included/excluded when blocking).
I also want to add a support for Mouse events blocking, different mouse buttons and whell scrolling...

Share this post


Link to post
Share on other sites

Posted · Report post

@MrCreator
Thanks, keep it up as you said :)

Cheers, FireFox.

Share this post


Link to post
Share on other sites

Posted (edited) · Report post

I am working on a new version, where we can set as include/exclude parameters a simple key strings (such as {Enter}, or even a Group like in RegExpReplace, i.e: "[Test]", here the chars t,e and s will be included/excluded when blocking).
I also want to add a support for Mouse events blocking, different mouse buttons and whell scrolling...

Done!

History Version:

[v1.2 - 16.01.2009, 21:00]
+ Added key strings support.
--Now users can set simple hotkey strings for Exclude/Include parameters + Group chars, i.e: "[Group]|{UP}|{DOWN}"
---(See UDF documentation)
+ Added mouse events blocking support to the $sExclude/$sInclude parameters.
+ Added example for mouse events blocking.

P.S
I think it's a last one for today :) Edited by MrCreatoR

Share this post


Link to post
Share on other sites

Posted · Report post

@MrCreator
I think nothing can be done now for keyboard after this udf and IsPressed_UDF :)

Cheers, FireFox.

Share this post


Link to post
Share on other sites

Posted · Report post

There is a mistake in the «_BlockInputEx Example (Block ALL, standard usage).au3», where _BlockInputEx(-1) should be _BlockInputEx(1). I will fix that with the next update.

Share this post


Link to post
Share on other sites

Posted · Report post

@MrCreatoR
I fixed that in my example :)

Cheers, FireFox.

Share this post


Link to post
Share on other sites

Posted · Report post

Hello all!
I have one suggestion that can be usefull to all:
How about a password for unlocking?
I've tried to do it, but no luck (yet)... General idea is to display input box asking for password when unlocking, but problem is that you have to unlock keyboard in order to type the pass, but do not let the user go away from input box untill the pass is correct...

any ideas?

Share this post


Link to post
Share on other sites

Posted (edited) · Report post

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.
That's about it, the rest you can see from the code.

CODE
#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
Edited by elemirac

Share this post


Link to post
Share on other sites

Posted (edited) · Report post

Sorry, I had a small bug in the last one. Fixed it here by adding Func _preUnlock(). Without this function I had a problem if someone strikes PAUSE key in PC Unlocked state, or if the same key is pressed while the password window is already shown.
Here is the corrected code:

#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}", "_preUnlock")

; 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
;$password = "" ; Declaration of input box variable (must be like this)

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 _preUnlock is making sure that _UnBlock should be called, without this the script has a bug
;-------------------------------------------------------------------------------------------------
Func _preUnlock()
	If WinExists("Lock") Then
		call("_UnBlock")
	Else
		Beep(500, 100)
	EndIf
EndFunc


; 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", "", "*",-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
		;WinClose("UnBlock")
		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
;-------------------------------------------------------------------------------------------
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
Edited by elemirac

Share this post


Link to post
Share on other sites

Posted (edited) · Report post

Small update!

History version:

[v1.3 - 24.09.2009, 23:00]
+ Added _BlockInputEx Example (Pass Lock)
* Fixed few examples.
* Fixed spell mistakes in the UDF.

[v1.2 - 16.01.2009, 21:00]
+ Added key strings support.
------Now users can set simple hotkey strings for Exclude/Include parameters + Group chars, i.e: "[Group]|{UP}|{DOWN}"
-------(See UDF documentation)
+ Added mouse events blocking support to the $sExclude/$sInclude parameters.
+ Added example for mouse events blocking.

Edited by MrCreatoR

Share this post


Link to post
Share on other sites

Posted · Report post

this is great ty i love it.

Share this post


Link to post
Share on other sites

Posted · Report post

Awesome script and very useful. Is there any way to get it to block USER mouse input but still enable script input via mousemove()?

Share this post


Link to post
Share on other sites

Posted · Report post

Very nice! This will really help me out in one of my programs :)

Just one thing though... Is it possible to block all input except for a certain window? I see here you can block input on just a specified window but what about the other way round?

Share this post


Link to post
Share on other sites

Posted · Report post

i found a problem with this u cant have your script using mousemove when u have blocked the inputs.

Share this post


Link to post
Share on other sites

Posted (edited) · Report post

nice UDF!

I am trying to block input to everything but a particular window. I have tried using the following code with no success. is this possible with your UDF? or is $hWindows only intended to control blocking FOR a specific window?

#include <BlockInputEx.au3>

;================== hWindows usage Example ==================

HotKeySet("{ESC}", "_Quit")

Run(@WindowsDir & "\Notepad.exe", "", @SW_MAXIMIZE)
WinWait("[REGEXPCLASS:Notepad.*]")
$hNotepad_Wnd = WinGetHandle("[REGEXPCLASS:Notepad.*]")

ControlSetText($hNotepad_Wnd, "", "", _
	"Now try to input some keys in here..." & @CRLF & _
	"Well, that's the idea, you can't, don't you?" & @CRLF & @CRLF & ":)")

;Here we block *All* keyboard keys for every window but Notepad's window
_BlockInputEx(3, "[:ALPHA:]|[:NUMBER:]|{ENTER}|{ALT}|{TAB}|{BACKSPACE}", "", $hNotepad_Wnd)

While 1
    Sleep(10000)
	_Quit()
WEnd

Func _Quit()
	Exit
EndFunc


-Homes32 Edited by Homes32

Share this post


Link to post
Share on other sites

Posted · Report post

I have a problem : when I use this on my script
_BlockInputEx(3, "[:ALPHA:]|[:NUMBER:]|{ENTER}|{BACKSPACE}","{LWIN}|{RWIN}|{DEL}|{LCTRL}|{RCTRL}|{ALT}|{TAB}|{F1}|{F2}|{F3}|{F4}|{F5}|{F6}|{F7}|{F8}|{F9}|{F10}|{F11}|{F12}")


Afterthat, these keys :LWIN, RWIN, DEL, LCTRL, RCTRL, ALT, TAB, F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12 is blocked, although the script is ended!

Share this post


Link to post
Share on other sites

Posted · Report post

Oh! The problem was solved!

Share this post


Link to post
Share on other sites

Posted · Report post

UDF updated!

History version:

[v1.4 - 03.08.2010, 12:00]
+ AutoIt 3.3.6.1 support.
* Fixed an issue with held down "Alt + Ctrl" keys after the user was called "Alt + Ctrl + Del". It was causing a problems to use HotKeySet later.
* Fixed examples to be compatible with AutoIt 3.3.6.1.
* Minor "cosmetic" changes.

[v1.3 - 24.09.2009, 23:00]
+ Added _BlockInputEx Example (Pass Lock)
* Fixed few examples.
* Fixed spell mistakes in the UDF.

Please check the first post.

Share this post


Link to post
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
Sign in to follow this  
Followers 0

  • Recently Browsing   0 members

    No registered users viewing this page.