I'm having trouble with GetKeyState in AHK - autohotkey

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.

Related

Autohotkey, problems redefining keypress down state of mousekey

I am using the LButton (mouse left) in a keybind as a prefix key. I got it to work, problem is I now need to redefine the LButton as whatever it was in it's natural state...in Autohotkey's terms.
I read this: https://www.autohotkey.com/docs/commands/GetKeyState.htm.
And cameup with the following code, but it's not working at all the way I thought it would. Simply put, you can use $LButton::Send {Click Left} to emulate the basic mouse click. The problem is when you hold the button/key down, nothing happens. I thought the code to emulate, or define the 'pressed down' behaviour would be readily available, but what I've found isn't working.
$LButton::
if (GetKeyState("LButton", "P"))
Send, {Click Left Down} ;tried variants with {Click Left} etc alrdy
else
Send, {Click Left Up}
return
For person in comments:
LButton & ~RButton::
Send, 1{Click Right}{Click Left}{Click Right}{MButton}
Sleep 130 ;125
Send, 1
return
$LButton:: ;no idea what this shud be
SendInput {Click Left}
;Send, {Click Left Down}
;KeyWait, LButton
;Send, {Click Left Up}
return
RButton::
Send, 1{Click Right}{Click Left}{Click Right}{MButton}
Sleep 130 ;125
Send, 1
return
Not sure if I understood this correctly, but you want holding LButton, then clicking the RButton to run this code:
Send, 1{Click Right}{Click Left}{Click Right}{MButton}
Sleep 130 ;125
Send, 1
And just clicking RButton should run that code as well?
And if LButton is just pressed normally (not in combination with RButton) it should function as normal?
Well this would be it:
~LButton & RButton::
RButton::
Send, 1{Click Right}{Click Left}{Click Right}{MButton}
Sleep, 130
Send, 1
return
Basically just making use of the ~ modifier.
Although I can't speak for Send, 1{Click Right}{Click Left}{Click Right}{MButton}.
I don't know what it's supposed to do, but maybe/hopefully it's doing the right thing.
Pressing w or something that you define should do the job
w::
Send, {Click down}
Send, {Blind}1{Click Right}{Click}{Click Right}{MButton}
Sleep 130 ;125
Send, 1
return
Anyone looking for something similar could try in this direction
#SingleInstance force
#warn
r::Reload
x::ExitApp
w::
Send, {Click down}
Send, {Blind}1{Click Right}{Click}{Click Right}{MButton}
Sleep 130 ;125
Send, 1
return
just Pressing w should do the job
Alternately
, the below one will do the same
LButton::Send, {Click down}
RButton::
if GetKeyState("LButton", "P")
{
Send, {Blind}1{Click Right}{Click}{Click Right}{MButton}
Sleep 130 ;125
Send, 1
}
else
Send, {Click Right}
return

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

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
}

Fixing Keyboard shortcut send in Auto Hot Key

Hi this is a super easy question but I was hoping someone could help me solve two AutoHotKey questions.
I am trying to get the keys Cntrl, J and I to be a keyboard shortcut that sends alt,h,v, and f.
Thus far I have tried these two ways of typing it
^&j&i::Send, !&h&v&f
^ji::Send, !hvf
as well as each of those without the comma after send.
I am also looking to send these keys on a very small delay, such as 5ms between each key
I was thinking of using something like this
^&j&i::
{
Send, {! down}
sleep 5
Send, {h down}
sleep 5
Send, {v down}
sleep 5
Send< {f down}
}
but of course, that is not working either. Any help is appreciated
Refer to https://www.autohotkey.com/docs/Hotkeys.htm#combo
Combinations of three or more keys are not supported. With this in mind we need to check the key state of the control key before defining the hotkey.
A few other things to note: SendInput is the ideal send method, and you likely want those keys back into an up state which I've included in my example.
#if GetKeyState("LControl", "P")
{
j & i::
{
SendInput {Alt down}
sleep 5
SendInput {h down}
sleep 5
SendInput {v down}
sleep 5
SendInput {f down}
sleep 5
SendInput {Alt Up}
sleep 5
SendInput {h Up}
sleep 5
SendInput {v Up}
sleep 5
SendInput {f Up}
return
}
}

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

AHK - How to hold a macro while Physical Key depressed

What I want to do is this:
Numpad3::
if(not GetKeyState("Shift" , "P") and not GetKeyState("RButton" , "P"))
{
SendInput {Shift down}
Sleep, 33
Click down right
}
Return
Numpad3 Up::
Sleep, 100
Click up right
Sleep, 33
SendInput {Shift up}
Return
But for some reason it isn't canceling when I let the button up. :(
I would suggest to use Send {RButton Down} (or Up) to send the right mouse click, instead of Click up right.
Also, you don't want to be sending random Sleep's if they are not really necessary, as it creates lag and makes the script inelegant and potentially unreadable.
Here is code which sends Control instead of RButton but it's only so I can test it within Notepad++.
Just replace Control with RButton and have a go:
*NumpadPgDn::
*Numpad3::
Send {Shift Down}{Control Down}
return
*NumpadPgDn Up::
*Numpad3 Up::
Send {Shift Up}{Control Up}
return