Skip to content

Instantly share code, notes, and snippets.

@nopeless
Created July 15, 2025 08:30
Show Gist options
  • Select an option

  • Save nopeless/1c312e6d4c65e758368d92a6d2e32632 to your computer and use it in GitHub Desktop.

Select an option

Save nopeless/1c312e6d4c65e758368d92a6d2e32632 to your computer and use it in GitHub Desktop.
#Requires AutoHotkey v2.0
#SingleInstance
#UseHook
; V2 rewrite and simplification by author: nopeless @ github.com
;
; Originally written by Lexikos (404)
; http://www.autohotkey.com/forum/post-147849.html#147849
; Modifications by Trevor Bekolay for the How-To Geek
; http://www.howtogeek.com/
#<!l:: {
KeyWait "LWin"
KeyWait "LAlt"
KeyWait "l"
MsgBox("Windows+Alt+L to unlock (Click OK to start blocking)")
BlockKeyboard(true)
}
BlockKeyboard(block) {
Static hHook := 0, cb := ""
If (!cb)
cb := CallbackCreate(BlockKeyboard_HookProc)
If (block && !hHook) {
hHook := DllCall("SetWindowsHookEx"
, "int", 13
, "ptr", cb
, "ptr", 0
, "uint", 0)
}
If (!block && hHook) {
DllCall("UnhookWindowsHookEx", "ptr", hHook)
hHook := 0
}
}
BlockKeyboard_HookProc(nCode, wParam, lParam)
{
Static winPressed := 0, altPressed := 0
if (NumGet(lParam, 8, "UInt") & 0x80) {
winPressed := 0
altPressed := 0
return 1
}
key := NumGet(lParam, 4, "UInt")
if (key = 91) {
winPressed := 1
return 1
}
if (key = 56) {
altPressed := 1
return 1
}
if (winPressed && altPressed && key = 38)
{
BlockKeyboard(false)
winPressed := 0
altPressed := 0
}
return 1
}
@nopeless
Copy link
Author

BlockKeyboard(block) {
    Static kHook := 0, mHook := 0, cb := ""

    If (!cb)
        cb := CallbackCreate(BlockKeyboard_HookProc)

    If (block && !kHook && !mHook) {
        kHook := DllCall("SetWindowsHookEx"
            , "int", 13
            , "ptr", cb
            , "ptr", 0
            , "uint", 0)
        mHook := DllCall("SetWindowsHookEx"
            , "int", 14
            , "ptr", cb
            , "ptr", 0
            , "uint", 0)
    }

    If (!block && kHook) {
        DllCall("UnhookWindowsHookEx", "ptr", kHook)
        kHook := 0
    }
    
    If (!block && mHook) {
        DllCall("UnhookWindowsHookEx", "ptr", mHook)
        mHook := 0
    }
}

Replace BlockKeyboard with this to also block mouse inputs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment