Jump to content

Attaching a hotkey to this script.


Recommended Posts

Hi all,

Just started using AutoIT this week and have to say I'm rather enjoying it and the possibility of things it can do. Basically, I want to use AutoIT on a kiosk to block user input at certain times such as when it boots and at night when I'll use the "monitor off" script. I looked around for a way to be able to toggle BlockInput on and off but no luck. Then I came across the code below which blocks the input when it runs and you can turn it off by hitting F3 which is almost perfect.

I was just hoping somebody could help me tweak this code so that rather than it block the input as soon as I click it, it will do so after I hit F2 for example. Then I can run it on boot and be able to toggle it on and off when I need. The other factor here is that the kiosk does not have a delete button so CTRL+ALT+DEL isn't possible.

#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")
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)
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

Wouldn't have thought it would be that difficult to attach a hotkey to this scipt but my limited knowledge here can't work it out.

Many thanks in advance.

Link to comment
Share on other sites

F4 to block

F3 to unblock

Does this work for you?

#include 
HotKeySet("{F3}", "_UnBlock")
HotKeySet("{F4}","_Block")

Func _Block()
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)
ConsoleWrite("Blocked" & @CRLF)
EndFunc

Func _UnBlock()
DllCallbackFree($pStub_KeyProc)
DllCallbackFree($pStub_MouseProc)
_WinAPI_UnhookWindowsHookEx($hHookKeyboard)
_WinAPI_UnhookWindowsHookEx($hHookMouse)
ConsoleWrite("Unblocked" & @CRLF)
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

While 1
Sleep(100)
WEnd

[font="helvetica, arial, sans-serif"]Hobby graphics artist, using gimp.Automating pc stuff, using AutoIt.Listening to music, using Grooveshark.[/font]Scripts:[spoiler]Simple ScreenshotSaves you alot of trouble when taking a screenshot!Don't remember what happened with this, but aperantly the exe is all i got.If you don't want to run it, simply don't._IsRun UDFIt figures out if the script has ben ran before based on the info in a ini file.If you don't want to use exactly what i wrote, you can use it as inspiration.[/spoiler]

Link to comment
Share on other sites

Hi Maffe,

Many thanks for your reply. When I copy the whole script I get an error message about "cannot parse #include". If I remove the #include from the top of the script I get an error with line 7 when I hit F4 saying "Variable used without being declared". Hopefully this is a simple to fix and I apologise in advance if it's a bit of an easy answer, as I said I'm new to this and hoping to learn it well so I can be the one helping the new kids one day.

Thanks again.

EDIT: Sorry Maffe, replaced '#include' with '#include <WinAPI.au3>' like in the original script and it seems to work. Thanks ever so much.

Edited by CNeedham
Link to comment
Share on other sites

Sorry to double post like a noob, can't edit my previous post now. One final question, I promise. Is there a way to change this to use keys such as CTRL+L to Lock and CTRL+U to unlock? If I change the hotkeys it locks fine but doesn't unlock, I would assume this is because somewhere the block code says block everything but F3 or all the F keys. Can this be changed? I mentioned earlier that this is for a kiosk and I've just noticed that this kiosk doesn't have any of the F keys which makes this awesome code unusable.

Many many thanks in advance.

Edited by CNeedham
Link to comment
Share on other sites

The #include at the top should say:

#include <WinAPI.au3>

Some versions of AutoIt will get an variable used before declared on $pStub_KeyProc if you try to unblock before blocking. Should be fixed in the latest AutoIt.

Link to comment
Share on other sites

This should work regardless:

#include
HotKeySet("{F3}", "_UnBlock")
HotKeySet("{F4}","_Block")

Global $pStub_KeyProc, $pStub_MouseProc, $hHookKeyboard, $hHookMouse

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)
ConsoleWrite("Blocked" & @CRLF)
EndFunc

Func _UnBlock()
DllCallbackFree($pStub_KeyProc)
DllCallbackFree($pStub_MouseProc)
_WinAPI_UnhookWindowsHookEx($hHookKeyboard)
_WinAPI_UnhookWindowsHookEx($hHookMouse)
ConsoleWrite("Unblocked" & @CRLF)
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

While 1
Sleep(100)
WEnd

Edit: The forum code tags are what is stripping the <WinAPI.au3> from the #include

Edited by John
Link to comment
Share on other sites

Thanks John, that makes sense. Any idea on how to change the hotkeys to something like CTRL+L rather than using F keys? If I just change the hotkeys it locks but wont unlock, I would assume this is because somewhere it says 'don't block F3' so it's free to turn input back on, can this be changed to 'don't block CTRL' for example?

Thanks in advance.

Link to comment
Share on other sites

I understand the HotKeySet and Send function and I know that if I wanted to have CTRL+L to lock and CTRL+U to unlock then it would look something like this:

HotKeySet("+{U}", "_UnBlock")
HotKeySet("+{L}","_Block")

But as I said previously, this works fine for blocking the input but doesn't work for unblocking it. It would be my guess that the original script blocks everything except F3 which was the original unlock button. So when I change this, surely something else has to change that I am yet to spot which prevents the CTRL and U keys from being blocked in the first place.

Link to comment
Share on other sites

Ok, I tried it and see what's you mean. You can fix it in the line in the _KeyProc() function where it says:

If $vkCode <> 0x72 Then Return 1

The 0x72 is the F3 key. Change this line to:

If $vkCode <> 0x72 And $vkCode <> 0x55 And $vkCode <> 0xA0 Then Return 1

This leaves F3, Left Shift, and U unblocked allowing your last example to work.

For a complete list of the vkCode keys go here:

http://www.kbdedit.com/manual/low_level_vk_list.html

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