Jump to content

BlockInputEx UDF!


MrCreatoR
 Share

Recommended Posts

@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   ;==>OnAutoItExitoÝ÷ Ø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
Link to comment
Share on other sites

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

RW Baker

Link to comment
Share on other sites

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

 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Link to comment
Share on other sites

@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...

 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Link to comment
Share on other sites

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

 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Link to comment
Share on other sites

  • 2 weeks later...

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.

 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Link to comment
Share on other sites

  • 5 months later...

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?

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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
Link to comment
Share on other sites

  • 2 months later...

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

 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Link to comment
Share on other sites

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?

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...