Mx2002
Mx2002

Reputation: 33

Disable mouse using a key in Autohotkey

I'm trying to make a palm-check program (a program that will disable the mouse when i'm typing). and i was wondering if there is a way to assign all letters (capital and small) and numbers to trigger a code to disable the mouse for 300 millisecond (i wrote 5000 to test it) and still be able to use the letters and numbers

Here is the code

lbutton::
rbutton::
WheelUp::
WheelDown::
suspend, on

a::
suspend, off
BlockInput, MouseMove
sleep 5000
suspend, on
BlockInput, MouseMoveoff
return

as you can see, i made the letter (a) trigger the code but i will not be able to use it + i will have to repeat the code again and again for more than 50 characters

can anyone help me solve this problem please?

Upvotes: 2

Views: 1500

Answers (2)

Relax
Relax

Reputation: 10568

Try this:

#NoEnv
#SingleInstance Force
#InstallkeybdHook
#InstallMouseHook
#UseHook

keys:=["a","b","c","d","1","2","3","4"] ; ....
for each, key in keys
{
    hotkey,%key%, BlockMouse, on
    hotkey,+%key%, BlockMouse, on
}
return

BlockMouse:
    ; suspend, off
    Mouse_Blocked := true
    BlockInput, MouseMove
    Send %A_ThisHotkey%
    SetTimer, UnBlockMouse, -300
return

UnBlockMouse:
    ; suspend, on
    BlockInput, MouseMoveoff
    Mouse_Blocked := false
return

#If (Mouse_Blocked)

    lbutton::
    rbutton::
    WheelUp::
    WheelDown::
    ; suspend, on
    return

#If

Upvotes: 1

Robert Ilbrink
Robert Ilbrink

Reputation: 7953

You could try the input command, set it to 1 character length and visible. You will probably need to split the trigger part and the suspend/sleep part as i expect that the trigger part can't be triggered again when it hasn't finished the earlier trigger event due to your sleep commands. I suggest you look into the settimer command to replace the sleep command. Sorry I can't help you with any code, I'm writing this on my phone and making suggestions by heart.

UPDATE:

Checked the Input command, didn't work the way I expected. I suggest you look at defining your trigger keys automatically, like they do here: https://autohotkey.com/board/topic/30294-simple-key-stroke-recorder/page-2.

I created some tinker code for you to play with here. It currently only triggers on the letter q, but with the loop in the keystroke recorder, you should be able to get this going.

SoundBeep, 1000,100  ; Just to check
BlockMouse := 0
return

$q:: ; $ prevents the next command from triggering this again
    SendInput, q
    BlockMouse := 1
    SetTimer, UnBlockMouse, 5000 ; Run UnBlockMouse after 500 ms
Return

UnBlockMouse:
    SetTimer, UnBlockMouse, off ; Turn timer off
    SoundBeep, 1000,100 ; Just to check
    BlockMouse := 0
Return

#If (BlockMouse) ; If statement controls the behaviour based on the status of the variable BlockMouse
    lbutton:: ; Disable button when BlockMouse variable is set to 1
    rbutton:: ; Disable button when BlockMouse variable is set to 1
    WheelUp:: ; Disable button when BlockMouse variable is set to 1
    WheelDown:: ; Disable button when BlockMouse variable is set to 1
#If

Upvotes: 0

Related Questions