Jump to content

BlockInput except for HotkeySet or specific keys? [Solved/Recommended Function]


ken82m
 Share

Recommended Posts

Looks like it's been asked but not in a while. Thought their may be a possible solution.

I have a hotkey specified to kill a script (F3 right now but I don't care which one).

I need to block input but do want to allow the user to press F3 to kill it.

Anyone have any ideas?

___________________________

If this is not possible quick question.

I haven't had time to test it yet.

It I lock the mouse in one position using _MouseTrap will that effect AutoIT's ability to move the mouse?

Thanks,

Kenny

Edited by ken82m

 "I believe that when we leave a place, part of it goes with us and part of us remains... Go anywhere, when it is quiet, and just listen.. After a while, you will hear the echoes of all our conversations, every thought and word we've exchanged.... Long after we are gone our voices will linger in these walls for as long as this place remains."

Link to comment
Share on other sites

Found this on google: http://www.autohotkey.com/docs/commands/Input.htm

And they have source code maybe the devs could take a look :P

http://www.autohotkey.com/download/AutoHotkey_source.exe

The basic idea would seem like a useful function, or addition to BlockInput :(

Thanks,

Kenny

 "I believe that when we leave a place, part of it goes with us and part of us remains... Go anywhere, when it is quiet, and just listen.. After a while, you will hear the echoes of all our conversations, every thought and word we've exchanged.... Long after we are gone our voices will linger in these walls for as long as this place remains."

Link to comment
Share on other sites

ken82m

You can use a keyboard hook. Example:

#include <WinAPI.au3>

HotKeySet("{F3}", "_MyFunc")

Global $pStub_KeyProc = DllCallbackRegister("_KeyProc", "int", "int;ptr;ptr")

MsgBox(0, "", 'Now we block all keyboard keys except a "F3"')

Global $hHook = _WinAPI_SetWindowsHookEx($WH_KEYBOARD_LL, DllCallbackGetPtr($pStub_KeyProc), _WinAPI_GetModuleHandle(0), 0)

While 1
    Sleep(100)
WEnd

Func _MyFunc()
    MsgBox(0, "MyFunc", "Function called by F3")
EndFunc

Func _KeyProc($nCode, $wParam, $lParam)
    If $nCode < 0 Then Return _WinAPI_CallNextHookEx($hHook, $nCode, $wParam, $lParam)
    
    Local $KBDLLHOOKSTRUCT = DllStructCreate("dword vkCode;dword scanCode;dword flags;dword time;ptr dwExtraInfo", $lParam)
    Local $vkCode = DllStructGetData($KBDLLHOOKSTRUCT, "vkCode")
    
    If $vkCode <> 0x72 Then Return 1
    
    _WinAPI_CallNextHookEx($hHook, $nCode, $wParam, $lParam)
EndFunc

Func OnAutoitExit()
    DllCallbackFree($pStub_KeyProc)
    _WinAPI_UnhookWindowsHookEx($hHook)
EndFunc

:P

Link to comment
Share on other sites

beautiful, thanks :P

I won't ask you to explain all that most of the commands I have no idea how to use lol.

But I'm assuming 0x72 is referring to F3 ?

I see 72 is the identifier for F3 in _IsPressed.

So I could use say 0x73 for the letter F4 ?

And if I wanted multiple keys I could use:

If $vkCode <> 0x72 AND $vkCode <> 0x73 Then Return 1

__________________________________________

Now I'm not using block input so anyone know how to kill the mouse without affecting MouseMove/MouseClick commands?

Thanks,

Kenny

Edited by ken82m

 "I believe that when we leave a place, part of it goes with us and part of us remains... Go anywhere, when it is quiet, and just listen.. After a while, you will hear the echoes of all our conversations, every thought and word we've exchanged.... Long after we are gone our voices will linger in these walls for as long as this place remains."

Link to comment
Share on other sites

Just noticed one small problem.

It blocks all the keys but F3 - Perfect

I hit F3 I get the MSGBOX - Perfect

My keyboard still doesn't work though. Once I click okay on the msgbox with my mouse the script exits and then I get my keyboard back.

 "I believe that when we leave a place, part of it goes with us and part of us remains... Go anywhere, when it is quiet, and just listen.. After a while, you will hear the echoes of all our conversations, every thought and word we've exchanged.... Long after we are gone our voices will linger in these walls for as long as this place remains."

Link to comment
Share on other sites

ken82m

I won't ask you to explain all that most of the commands

Google and the searching on this forum your friends. :(

ken82m

And if I wanted multiple keys I could use:

If $vkCode <> 0x72 AND $vkCode <> 0x73 Then Return 1

Yes.

ken82m

My keyboard still doesn't work though. Once I click okay on the msgbox with my mouse the script exits and then I get my keyboard back.

Extended example with mouse blocking:

#include <WinAPI.au3>

HotKeySet("{F3}", "_UnBlock")

Global $pStub_KeyProc = DllCallbackRegister("_KeyProc", "int", "int;ptr;ptr")
Global $pStub_MouseProc = DllCallbackRegister ("_Mouse_Handler", "int", "int;ptr;ptr")

MsgBox(0, "", 'Now we block mouse and all keyboard keys except a "F3"')

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)

While 1
    Sleep(100)
WEnd

Func _UnBlock()
    DllCallbackFree($pStub_KeyProc)
    DllCallbackFree($pStub_MouseProc)
    _WinAPI_UnhookWindowsHookEx($hHookKeyboard)
    _WinAPI_UnhookWindowsHookEx($hHookMouse)
    MsgBox(0, "_UnBlock", "Input unblocked")
    Exit
EndFunc

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 <> 0x72 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

:P

Link to comment
Share on other sites

Yes without a doubt a true god :(

If I had the frequent flyer miles I'd buy you a beer lol

Thank you very much!

Definitely needs to be added to the next beat build or two in my opinion. :P

-Kenny

EDIT: Submitted feature request with a link to this thread. Ticket# 622

Edited by ken82m

 "I believe that when we leave a place, part of it goes with us and part of us remains... Go anywhere, when it is quiet, and just listen.. After a while, you will hear the echoes of all our conversations, every thought and word we've exchanged.... Long after we are gone our voices will linger in these walls for as long as this place remains."

Link to comment
Share on other sites

Good example rasim!

Here is a UDF style :P

#include <WinAPI.au3>

HotKeySet("{ESC}", "_Quit")
HotKeySet("{F1}", "_Quit")
HotKeySet("{F2}", "_Quit")
HotKeySet("{F3}", "_Quit")
HotKeySet("{F4}", "_Quit")

Global $ah_Hooks = _BlockInputEx(1, -1, "0x1B|0x70|0x71|0x72|0x73") ;0x1B={ESC},0x70="{F1}",0x71="{F2}",0x72="{F3}",0x73="{F4}"

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

While 1
    Sleep(100)
WEnd

Func _Quit()
    _BlockInputEx($ah_Hooks)
    
    Exit
EndFunc

;$iBlockMode Option:
;                    -1 - Block All
;                    0  - Block only mouse
;                    1  - Block only keyboard
Func _BlockInputEx($ah_Block_Hooks=0, $iBlockMode=-1, $sExclude="")
    Select
        Case IsArray($ah_Block_Hooks)
            If $ah_Block_Hooks[1] > 0 Then DllCallbackFree($ah_Block_Hooks[1])
            If $ah_Block_Hooks[2] > 0 Then DllCallbackFree($ah_Block_Hooks[2])
            
            If $ah_Block_Hooks[3] > 0 Then _WinAPI_UnhookWindowsHookEx($ah_Block_Hooks[3])
            If $ah_Block_Hooks[4] > 0 Then _WinAPI_UnhookWindowsHookEx($ah_Block_Hooks[4])
            
            Return 1
        Case IsNumber($ah_Block_Hooks) And $ah_Block_Hooks > 0
            Local $pStub_KeyProc = 0, $pStub_MouseProc = 0, $hHookKeyboard = 0, $hHookMouse = 0
            
            If $iBlockMode = -1 Or $iBlockMode = 0 Then
                $pStub_MouseProc = DllCallbackRegister("_Mouse_Proc", "int", "int;ptr;ptr")
                $hHookMouse = _WinAPI_SetWindowsHookEx($WH_MOUSE_LL, DllCallbackGetPtr($pStub_MouseProc), _
                    _WinAPI_GetModuleHandle(0), 0)
            EndIf
            
            If $iBlockMode = -1 Or $iBlockMode = 1 Then
                $pStub_KeyProc = DllCallbackRegister("_Key_Proc", "int", "int;ptr;ptr")
                $hHookKeyboard = _WinAPI_SetWindowsHookEx($WH_KEYBOARD_LL, DllCallbackGetPtr($pStub_KeyProc), _
                    _WinAPI_GetModuleHandle(0), 0)
            EndIf
            
            Local $aRet[5] = ["|" & $sExclude & "|", $pStub_KeyProc, $pStub_MouseProc, $hHookKeyboard, $hHookMouse]
            
            Return $aRet
        Case Else
            Return SetError(1, 0, 0)
    EndSelect
EndFunc

Func _Key_Proc($nCode, $wParam, $lParam)
    If $nCode < 0 Then Return _WinAPI_CallNextHookEx($ah_Hooks[3], $nCode, $wParam, $lParam)
    
    Local $KBDLLHOOKSTRUCT = DllStructCreate("dword vkCode;dword scanCode;dword flags;dword time;ptr dwExtraInfo", $lParam)
    Local $vkCode = DllStructGetData($KBDLLHOOKSTRUCT, "vkCode")
    
    If Not StringInStr($ah_Hooks[0], "|0x" & Hex($vkCode, 2) & "|") Then Return 1
    
    _WinAPI_CallNextHookEx($ah_Hooks[3], $nCode, $wParam, $lParam)
EndFunc

Func _Mouse_Proc($nCode, $wParam, $lParam)
    If $nCode < 0 Then Return _WinAPI_CallNextHookEx($ah_Hooks[4], $nCode, $wParam, $lParam)
    
    Return 1
EndFunc
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

Thanks for the UDF version, mighty handy :P

One question though. I ran the udf code as is and it's locking the mouse for me but not the keyboard?

-Kenny

 "I believe that when we leave a place, part of it goes with us and part of us remains... Go anywhere, when it is quiet, and just listen.. After a while, you will hear the echoes of all our conversations, every thought and word we've exchanged.... Long after we are gone our voices will linger in these walls for as long as this place remains."

Link to comment
Share on other sites

@ken82m

I ran the udf code as is and it's locking the mouse for me but not the keyboard

Oops, fixed :P (see my last post). Also i changed the UDF a little, now you can choose what to block - mouse & keyboard, only mouse, or only keyboard (see $iBlockMode param).

 

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

thanks man :P

I didn't want to bother you with that one figured I would add it myself but thanks :(

 "I believe that when we leave a place, part of it goes with us and part of us remains... Go anywhere, when it is quiet, and just listen.. After a while, you will hear the echoes of all our conversations, every thought and word we've exchanged.... Long after we are gone our voices will linger in these walls for as long as this place remains."

Link to comment
Share on other sites

  • 1 month later...

Well Cool code really appreciated kinda advance code but i can understand that :)

How to use combination keys like here i need to use underscore to Unhook keyboard instead of F3

If $wParam = $WM_KEYDOWN Then
    If $nCode =  0xA1 And 0xBD then 
MsgBox(0 , "", "_")
Endif
Edited by autoitxp
Link to comment
Share on other sites

If looks like it can be done with _WinAPI_GetAsyncKeyState()

I added something the other day for ALT+key combinations but that was easy cause there is a built in flag for that.

I managed to get the shift combo to work but only once if you continue to hold shift it will not function.

I don't know what this means which is probably why I can't get it to work lol

If the most significant bit is set the key is down, and if the least significant bit is set, the key was pressed after the previous call to GetAsyncKeyState.

I'm trying MSDN now.

Kenny

 "I believe that when we leave a place, part of it goes with us and part of us remains... Go anywhere, when it is quiet, and just listen.. After a while, you will hear the echoes of all our conversations, every thought and word we've exchanged.... Long after we are gone our voices will linger in these walls for as long as this place remains."

Link to comment
Share on other sites

I created another version of this last week that would allow all keys except the ones I specified.

I the shift combo's blocked on that one

But I can't get them to be allowed through in this one

 "I believe that when we leave a place, part of it goes with us and part of us remains... Go anywhere, when it is quiet, and just listen.. After a while, you will hear the echoes of all our conversations, every thought and word we've exchanged.... Long after we are gone our voices will linger in these walls for as long as this place remains."

Link to comment
Share on other sites

@Mr Creator

Thanks for your example :)

Ive tested it and found one beug if you enter only one key for block :

Global $ah_Hooks = _BlockInputEx(1, -1, "0x1B");Computer lag with only one key
Global $ah_Hooks = _BlockInputEx(1, -1, "0x1B|0x1B");Computer works with same key

And ive added some keys from ispressed to block all specials keys but all keys with caracter works [A-Z/a-z/0-9/*/-+] etc...

Global $ah_Hooks = _BlockInputEx(1, -1, "0x01|0x02|0x08|0x09|0x14|0x20|0x25|0x27|0x2D|0x2E|0x30|0x31|0x32|0x33|0x34"& _
"0x35|0x36|0x37|0x38|0x39|0x41|0x42|0x43|0x44|0x45|0x46|0x47|0x48|0x49|0x4A|0x4B|0x4C|0x4D|0x4E|0

x4F|0x50|0x51|0x52"& _
"0x53|0x54|0x55|0x56|0x57|0x58|0x59|0x5A|0x60|0x61|0x62|0x63|0x64|0x65|0x66|0x67|0x68|0x69|0x6A|0

x6B|0x6C|0x6D|0x6E"& _
"0x6F|0xA0|0xA1|0xBA|0xBB|0xBC|0xBD|0xBE|0xBF|0xC0|0xDB|0xDC|0xDD")
Link to comment
Share on other sites

  • 5 weeks later...

1) Excuse my ignorance, but is there a way to disable ctrl-alt-del? I'm a newbe and don't know how to make dll-calls, but on this site http://www.andreavb.com/tip020011.html I found the following text... which I think may work also with AutoIt?

--------

:: How to Disable the Ctrl-Alt-Del keys combination...

Author

Andrea Tincani

Language

VB5, VB6

Operating Systems

Windows 95 and 98

API Declarations

Private Declare Function SystemParametersInfo Lib "user32.dll" Alias "SystemParametersInfoA" (ByVal uAction As Long, ByVal uParam As Long, ByVal lpvParam As Any, ByVal fuWinIni As Long) As Long

Module

Sub DisableCtrlAltDelete(bDisabled As Boolean)

Dim x As Long

x = SystemParametersInfo(97, bDisabled, CStr(1), 0)

End Sub

---------

2) Is it possible use this great script so that I need to push many buttons to unlock the PC? It would e.g. be nice if all five buttons esc, f1, f2, f3, f4 had to be pushed at the same time.

3) Since this is my first post I also take the chance to ask if there is any way to send a ctrl-alt-del while the computer is locked? I know that it is often done by remote control software, like e.g. VNC, so I suppose it should be possible to do even if it is not not very simple.

Link to comment
Share on other sites

@perwhis

instead of blocking these keys, the better way is to use winlockdll.dll with disable taskmgr function or write a key in regedit for disable it :)

;regedit enable
RegWrite("HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Policies\System", "DisableTaskMgr", "REG_DWORD", 1)
;regedit disable
;~ RegDelete("HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Policies\System", "DisableTaskMgr")

;winlockdll.dll enable
DllCall("winlockdll.dll","int","CtrlAltDel_Enable_Disable","int",0)
;winlockdll.dll disable
;~ DllCall("winlockdll.dll","int","CtrlAltDel_Enable_Disable","int",1)

Cheers, FireFox.

Edited by FireFox
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...