Disable all keyboard buttons

Post your working scripts, libraries and tools for AHK v1.1 and older
chaoscreater
Posts: 60
Joined: 12 Sep 2019, 21:15

Re: Disable all keyboard buttons

25 Feb 2023, 21:40

SpeedMaster wrote:
07 Jul 2019, 05:47
Hello,
I added an option to also hide the screen and choose the hiding color 8-) :thumbup:
Press Alt + F3 to test

how to use the function:
hk(keyboard:=false, mouse:=0, message:="", timeout:=3, displayonce:=false,screen:=false, screencolor:="blue")

keyboard (true/false).......................... disable/enable keyboard
mouse=1........................................ disable all mouse buttons
mouse=2........................................ disable right mouse button only
msessage....................................... display a message
timeout........................................ how long to display the message in sec
displayonce (true/false) ...................... display a message only once or always
hide the screen (true/false)................... hide or show everything
ScreenColor ................................... RGB Hex background color for the hiding GUI

Code: Select all

#SingleInstance force

!F1::hk(1,1,"KEYBOARD AND MOUSE LOCKED!  -  ALT+F2 TO UNLOCK")              ; Disable all keyboard keys and mouse buttons
!F2::hk(0,0,"KEYBOARD AND MOUSE UNLOCKED!  -  ALT-F1 TO LOCK")              ; Enable all keyboard keys and mouse buttons
!F3::hk(1,1,"KEYBOARD MOUSE AND SCREEN LOCKED!  -  ALT+F2 TO UNLOCK",,,1,"teal")   ; Disable keyboard mouse and screen


hk(keyboard:=false, mouse:=0, message:="", timeout:=3, displayonce:=false,screen:=false, screencolor:="blue") { 

;keyboard (true/false).......................... disable/enable keyboard
;mouse=1........................................ disable all mouse buttons
;mouse=2........................................ disable right mouse button only
;msessage....................................... display a message
;timeout........................................ how long to display the message in sec
;displayonce (true/false) ...................... display a message only once or always
;hide the screen (true/false)................... hide or show everything
;ScreenColor ................................... RGB Hex background color for the hiding GUI 


   static AllKeys, z, d, kb, ms, sc
   z:=message, d:=displayonce, kb:=keyboard, ms:=mouse, sc:=screen

      For k,v in AllKeys {
           Hotkey, *%v%, Block_Input, off         ; initialisation
      }
   if !AllKeys {
      s := "||NumpadEnter|Home|End|PgUp|PgDn|Left|Right|Up|Down|Del|Ins|"
      Loop, 254
         k := GetKeyName(Format("VK{:0X}", A_Index))
       , s .= InStr(s, "|" k "|") ? "" : k "|"
      For k,v in {Control:"Ctrl",Escape:"Esc"}
         AllKeys := StrReplace(s, k, v)
      AllKeys := StrSplit(Trim(AllKeys, "|"), "|")
   }
   ;------------------
   if (mouse!=2)  ; if mouse=1 disable right and left mouse buttons  if mouse=0 don't disable mouse buttons
    {
        For k,v in AllKeys {
           IsMouseButton := Instr(v, "Wheel") || Instr(v, "Button")
           Hotkey, *%v%, Block_Input, % (keyboard && !IsMouseButton) || (mouse && IsMouseButton) ? "On" : "Off"
        }
    }
   if (mouse=2)   ;disable right mouse button (but not left mouse)
    {                
     ExcludeKeys:="LButton"
      For k,v in AllKeys {
           IsMouseButton := Instr(v, "Wheel") || Instr(v, "Button")
           if v not in %ExcludeKeys%
           Hotkey, *%v%, Block_Input, % (keyboard && !IsMouseButton) || (mouse && IsMouseButton) ? "On" : "Off"
        }
    }
   if d
    {
   if (z != "") {
      Progress, +AlwaysOnTop W2000 H43 b zh0 cwFF0000 FM20 CTFFFFFF,, %z%
      SetTimer, TimeoutTimer, % -timeout*1000
   }
   else
      Progress, Off
     }
   Block_Input:
   if (d!=1)
    {
   if (z != "") {
	    if (kb || ms)
			Progress, W2000 H43 b zh0 cwFF0000 FM20 CTFFFFFF,, %z%
		else
			Progress, W2000 H43 b zh0 cw009F00 FM20 CTFFFFFF,, %z%
		SetTimer, TimeoutTimer, % -timeout*1000
   }
   else
      Progress, Off
     }


if (sc=1)
   { 
     Gui screen:  -Caption
     Gui screen: Color,  % screencolor
     Gui screen: Show, x0 y0 h74 w%a_screenwidth% h%a_screenheight%, New GUI Window
   }
   else
      gui screen: Hide


   Return
   TimeoutTimer:
   Progress, Off
   Return
}
Cheers


I'm running into a weird issue. All I'm doing is just changing from ALT+F1 to SHIFT+ALT+F1 and ALT+F2 to SHIFT+ALT+F2.

If I press SHIFT+ALT+F2, it triggers the unlock, great. However, if I lock first (with SHIFT+ALT+F1) and then try to unlock, it won't trigger the unlock. Any ideas why?
User avatar
SpeedMaster
Posts: 494
Joined: 12 Nov 2016, 16:09

Re: Disable all keyboard buttons

26 Feb 2023, 20:11

chaoscreater wrote:
25 Feb 2023, 21:40
If I press SHIFT+ALT+F2, it triggers the unlock, great. However, if I lock first (with SHIFT+ALT+F1) and then try to unlock, it won't trigger the unlock. Any ideas why?
Combinations of three or more keys are generally not supported but you can try this...

Code: Select all

+!F1::
; always make  sure that the keys are released before blocking the keyboard ! :)
KeyWait, shift, up  
KeyWait, alt, up
KeyWait, F1, up
hk(1,1,"KEYBOARD AND MOUSE LOCKED!  -  Shift-ALT-F2 TO UNLOCK")              ; Disable all keyboard keys and mouse buttons
return

+F2::
if GetKeyState("Alt", "P")                                                  ; Press in this order Shift-Alt-F2   (Alt Shift F2 will not work)
hk(0,0,"KEYBOARD AND MOUSE UNLOCKED!  - ALT-Shift-F1 TO LOCK")              ; Enable all keyboard keys and mouse buttons
return
Regards
zed6250jb
Posts: 54
Joined: 20 Apr 2024, 18:25
Contact:

Re: Disable all keyboard buttons

23 Jun 2023, 20:08

Is there a way to leave the script attached intact, but add an activation based on timer to disabled kb and mouse? so for example after 5 minuets automatically disable kb and mouse. There is also another additional idea to add to that and create an idle rest watchdog so everytime a key or mouse has activity the timer resets without disable. Please let me know.
zed6250jb
Posts: 54
Joined: 20 Apr 2024, 18:25
Contact:

Re: Disable all keyboard buttons

23 Jun 2023, 20:11

attached
Attachments
Untitled.ahk
(1.3 KiB) Downloaded 33 times
User avatar
A Keymaker
Posts: 457
Joined: 31 May 2022, 13:46
Location: Donjon du Château de Mérovingien

Re: Disable all keyboard buttons

04 Jul 2023, 11:41

A Keymaker wrote:
12 Jul 2022, 08:00
How these scripts would have to be modified in order to:
[...]
A-B: yes

C-E: I will take care of this soon
User avatar
A Keymaker
Posts: 457
Joined: 31 May 2022, 13:46
Location: Donjon du Château de Mérovingien

Re: Disable all keyboard buttons

09 Jul 2023, 03:43

A Keymaker wrote:
04 Jul 2023, 11:41
A Keymaker wrote:
12 Jul 2022, 08:00
How these scripts would have to be modified in order to:
[...]
[...]
C-E: I will take care of this soon
C and E [i.e.. blocking of mouse input] can be achieved with this script

Code: Select all

counter := 0

BlockInput, MouseMove
Hotkey, *LButton, BlockInputMouseMoveClick
Hotkey, *MButton, BlockInputMouseMoveClick
Hotkey, *RButton, BlockInputMouseMoveClick
Hotkey, *WheelUp, BlockInputMouseMoveClick
Hotkey, *WheelDown, BlockInputMouseMoveClick
Hotkey, *WheelLeft, BlockInputMouseMoveClick
Hotkey, *WheelRight, BlockInputMouseMoveClick
Hotkey, *XButton1, BlockInputMouseMoveClick
Hotkey, *XButton2, BlockInputMouseMoveClick

$MButton::   ; A single click with Middle Mouse Button restores input by closing this AHK file. But because the script for blocking of input and the one for keeping monitor off both use Middle Mouse Button as a fail-safe / turn off, instead of 1 MMB click 2 fast ones are required
*/
    counter++
    if (counter = 1)
    {
        ExitApp
    }
return

BlockInputMouseMoveClick()
{
    return
}
with a turn off switch in form of single Middle Mouse Click. [And thus the D issue I have ditched entirely]
rockitdontstopit
Posts: 107
Joined: 12 Nov 2022, 15:47

Re: Disable all keyboard buttons

09 Jul 2023, 15:06

RunarSF wrote:
01 Jul 2017, 14:04
This is a simple script that disables all keyboard buttons except escape. To end the script, simply press escape and a message box will pop up.
What might be the use for this script? Personally, I used it when I was cleaning my keyboard and had to remove all the keys, so I wouldn't mess up my computer by spamming random buttons (I know, I know, I could have turned off my computer, but who has time for that.) but you are free to use the script as you wish.
Why not just win lock with WIN + L? That's what I do when I want to clean my keyboard with rubbing alcohol when it starts to feel sticky.
User avatar
A Keymaker
Posts: 457
Joined: 31 May 2022, 13:46
Location: Donjon du Château de Mérovingien

Re: Disable all keyboard buttons

09 Jul 2023, 15:14

rockitdontstopit wrote:
09 Jul 2023, 15:06
[...]
Why not just win lock with WIN + L? That's what I do when I want to clean my keyboard with rubbing alcohol when it starts to feel sticky.
Your method forces user later on to enter password for Windows and does not alleviate issue of kids, pets and cockroaches playing with the keyboard

When I clean any computer hardware I disconnect it from power source or at least kill its input mechanically
Last edited by A Keymaker on 06 Jun 2024, 08:16, edited 2 times in total.
X-Knight
Posts: 1
Joined: 22 May 2024, 07:27
Contact:

Re: Disable all keyboard buttons

22 May 2024, 07:37

[Mod edit: Removed empty codebox.]

Can someone make me a code for "Disable all Keyboard Buttons" while I'm holding my "Mouse Button 2" and when I release the "Mouse Button 2", It'll "Enable all Keyboard Buttons"
GEV
Posts: 1005
Joined: 25 Feb 2014, 00:50

Re: Disable all keyboard buttons

06 Jun 2024, 07:52

X-Knight wrote:
22 May 2024, 07:37
Can someone make me a code for "Disable all Keyboard Buttons" while I'm holding my "Mouse Button 2" and when I release the "Mouse Button 2", It'll "Enable all Keyboard Buttons"

Code: Select all

#Requires AutoHotkey v1.1
#InstallKeybdHook

XButton2::
	BlockKeyboard("On")
	KeyWait XButton2
	BlockKeyboard("Off")
return

BlockKeyboard(state){
    ; https://stackoverflow.com/a/61700303/3419297
    Loop, 512
    {
        Key := Format("SC{:X}",A_Index)
        If (state = "On")
            Hotkey, *%Key%, do_nothing, On UseErrorLevel
        else
            Hotkey, *%Key%, do_nothing, Off UseErrorLevel
    }
    do_nothing:
    return
}
Last edited by GEV on 06 Jun 2024, 09:21, edited 1 time in total.
User avatar
A Keymaker
Posts: 457
Joined: 31 May 2022, 13:46
Location: Donjon du Château de Mérovingien

Re: Disable all keyboard buttons

06 Jun 2024, 08:21

If anybody is interested in a feedback, then for me with my Logitech Mouse MX Anywhere 2 Business Travel / Logitech Mouse MX Anywhere 2 For Amazon on Windows 10 Enterprise 20H2 x64 [10.0.19042] the above code does nothing
GEV
Posts: 1005
Joined: 25 Feb 2014, 00:50

Re: Disable all keyboard buttons

06 Jun 2024, 09:23

@A Keymaker
I editet it adding

Code: Select all

#InstallKeybdHook
User avatar
A Keymaker
Posts: 457
Joined: 31 May 2022, 13:46
Location: Donjon du Château de Mérovingien

Re: Disable all keyboard buttons

06 Jun 2024, 12:59

My keys are still not blocked when that side mouse button is being pressed
GEV
Posts: 1005
Joined: 25 Feb 2014, 00:50

Re: Disable all keyboard buttons

06 Jun 2024, 13:39

Works here (Logitech G700).

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: No registered users and 154 guests