How to make "BlockInput On/Off" only block keyboard inputs not mouse - autohotkey

As the title says, I've had trouble finding a way for my script to only block keyboard inputs during my script. Is there a method that I've overlooked to do this? Here's my code for reference:
toggle = 0
*xbutton1::
{
if GetKeyState("d", "P")
{
if GetKeyState("w", "P")
{
BlockInput On ;enabled
Send, {d up}
Send, {w up}
Send, {a down}
Send, {s down}
Send, {K}
BlockInput Off ;disabled when completed with the above actions ^ so no key inputs interfere
Sleep, -1
Send, {a up}
Send, {s up}
Send, {d down}
Send, {w down}
return
}
Thanks! I'd appreciate any info or tips.

You can create a function to only block keyboard input:
; Press F1 to block keyboard input for 10 seconds:
$F1::
BlockKeyboard("On")
Sleep, 10000
BlockKeyboard("Off")
return
BlockKeyboard(state){
Loop, 512
{
Key := Format("SC{:X}",A_Index)
If (state = "On")
Hotkey, *%Key%, KeyboardKey, On UseErrorLevel
else
Hotkey, *%Key%, KeyboardKey, Off UseErrorLevel
}
KeyboardKey:
return
}

Related

I'm having trouble with GetKeyState in AHK

Below is an alt+tab program I wrote that, for some reason, won't work.
while x = 1
{
mb1 := GetKeyState(j)
mb2 := GetKeyState(k)
if (mb1 = 1) and (mb2 = 1)
{
Send, {Alt Down}
Send, {Tab Down}
sleep, 50
Send, {Alt Up}
Send, {Tab Up}
}
}
I've tried multiple methods of the loop and key detection to no avail.
You do not need to store the value of the keystate in a variable prior to the if-statement; you can check them during the if-statement itself.
So, you could implement this change with something like this:
Loop
{
if (GetKeyState("j") && GetKeyState("k"))
{
Send, {Alt Down}
Send, {Tab Down}
sleep, 50
Send, {Alt Up}
Send, {Tab Up}
}
}
However, if you need to save the value of the KeyStates for some reason, there are a couple of ways to do this:
Just save the values from the GetKeyStates while you are checking them in the if-statement.
Note: For both variables to always update every iteration, you need to replace the efficient && with the less efficient &, since the && will stop checking variables as soon as it determines the expression will be false.
This would look something like:
Loop
{
if (mb1:=GetKeyState("j") & mb2:=GetKeyState("k"))
{
Send, {Alt Down}
Send, {Tab Down}
sleep, 50
Send, {Alt Up}
Send, {Tab Up}
}
MsgBox During the last check, j was %mb1%, and k was %mb2%!
}
Use the alternative GetKeyState command syntax
Note: Although this version of the command makes it more straightforward to save the output of the command to a variable, it is depreciated and not recommended for use in new scripts.

How do i stop AHK loop when LButton is up

I just want this loop to work when LButton is down and stop when LButton is up.I
The code works when LButton is down but it continues working when i lift my finger off the left click button.
mem:=0
~*LButton::
Sleep, 100
KeyWait, LButton, T0.10
If ErrorLevel = 1
{
While GetKeyState("LButton","P")
Loop {
GetKeyState, state, LButton, P
If state = U
Break
MouseGetPos, xpos, ypos
if (xpos > mem) ;moved right
{
send, {a down} ;send key
mem:=xpos
}
else
send, {a up}
if (xpos < mem) ;moved left
{
send, {d down}
mem:=xpos
}
else
send, {d up}
Sleep, 100
}
}
return
You case is very similar to the example of the usage of the while loop.
If you look at that you can see, you don't need the Loop inside the while. Just have
while GetKeyState("LButton")
{
; Your code here:
MouseGetPos, xpos, ypos
if (xpos > mem) ;moved right
{
send, {a down} ;send key
mem:=xpos
}
else{
send, {a up}
}
if (xpos < mem) ;moved left
{
send, {d down}
mem:=xpos
}
else{
send, {d up}
}
Sleep, 100
}

Autohotkey Mapping Modifiers

I can't use arrow keys from my keyboard so I mapped 1234 to be the arrow keys like that:
*!^1::
Send, {Left down}{Left up}
Return
*!^2::
Send, {Down down}{Down up}
Return
*!^3::
Send, {Up down}{Up up}
Return
*!^4::
Send, {Right down}{Right up}
Return
Now my problem is that in some programs the arrow keys pressed with the shift modifier is associated to a function and with the current settings, when I press Ctrl+Alt+Shift+1 it still outputs me the associated arrow key without considering the 'Shift' modifier.
Do you know how could I solve the problem?
Thanks.
You can just add a list of commands that already includes the shift key, like this:
*+!^1::
Send, {Shift down}{Left down}{Left up}{Shift up}
Return
*+!^2::
Send, {Shift down}{Down down}{Down up}{Shift up}
Return
*+!^3::
Send, {Shift down}{Up down}{Up up}{Shift up}
Return
*+!^4::
Send, {Shift down}{Right down}{Right up}{Shift up}
Return

AutoHotkey - press Ctrl+Alt only, without any characters

I can see that Ctrl+Alt+Delete is not possible with AutoHotkey, but what about just Ctrl+Alt?
Things I've tried:
; 1
#+y::
Send, {Ctrl}{Alt}
return
;2
#+y::
Send, {Ctrl down}
Send, {Alt down}
Send, {Alt up}
Send, {Ctrl up}
return
;3
#+y::
Send, {LCtrl down}
Send, {LAlt down}
Send, {LAlt up}
Send, {LCtrl up}
return
;4
#+y::
Send, !^
return
;5
#+y::
Send, {ctrl down}{alt down}{2}{alt up}{ctrl up}
return
Any ideas?
The below will do what you want. It's also recommended you use SendInput going forward.
#+y::
SendInput, {Ctrl Down}{Alt Down}{Ctrl Up}{Alt Up}
Try to specify the left or right ALT or CTRL keys
#+y::
Send, {LCtrl}{LAlt}
return

AHK Disable Script A when Script B is Executed

hi again my script is almost complete but there is problem in executing hotkeys
Problem : when i hit Alt + Space it is conflicting with Space .. they are overlaping each other or executing both hotkey which makes custom modifier keys unstable or not working as intented
here is the code:
space::
send {shift down}{MButton down}
KeyWait, space
send {shift up}{Mbutton up}
return
!space::
send {Mbutton down}
KeyWait, space
keywait, alt
send {Mbutton Up}
return
#if, GetKeyState("MButton")
Lbutton::
RButton::
return
#if
i want to add a rule if alt + space keycombo is held down this hotkey will not work
meaning i want to disable Script A if Script B is executed
Script A ::
space::
send {shift down}{MButton down}
KeyWait, space
send {shift up}{Mbutton up}
return
Script B ::
!space::
send {Mbutton down}
KeyWait, space
keywait, alt
send {Mbutton Up}
return
thank you in advance
Wrap the space:: part in #If, !GetKeyState("Alt")
!space::
send {Mbutton down}
KeyWait, space
keywait, alt
send {Mbutton Up}
return
#if, !GetKeyState("Alt")
space::
send {shift down}{MButton down}
KeyWait, space
send {shift up}{Mbutton up}
return
#if, GetKeyState("MButton")
Lbutton::
RButton::
return
#if
space::
Hotkey_Alt_Space = 0 ; disabled
send {shift down}{MButton down}
KeyWait, space
send {shift up}{Mbutton up}
Hotkey_Alt_Space = 1
return
!space::
If (Hotkey_Alt_Space != 0) ; enabled
{
send {Mbutton down}
KeyWait, space
keywait, alt
send {Mbutton Up}
}
return
i was help by someone at AHK forum thank you for time the problem is solve i am able to customize my rotate and pan navigation at sketchup