Change a key function for x amount of time? - toggle

So thanks to this user i have got this working code, but i want to further tweak it if its posibble. :D
Here is the code:
global s:=0, c:=0, t:=1500, t2:=380
*lbutton::
send % Seqkeys("5","6")
KeyWait, lbutton
If (A_TickCount-s < t2)
c := 0
Send, 7
return
Seqkeys(params*) {
global s, c, t
max := params.MaxIndex()
(A_TickCount-s<=t && (c+=1)<=max) ? c : c:=1
s := A_TickCount
return params[c]
}
What i want to implement is that if i hit the right mouse button (Rbutton), the original lbutton seqkeys code changes to this code for 1 second
*lbutton::
send, 8
KeyWait, lbutton
Send, 7
return
than when the 1 sec is over the code is revert back to the original seqkeys state.
I got this code sample but its not working properly for several reasons, when i hit the Rbutton it does override the original Seqkeys function, but it never resets back to the seqkeys one. I put it here maybe it helps
*rbutton::
toggle:=true
return
#If Toggle
*lbutton::
send, 8
KeyWait, lbutton
Send, 7
return
toggle:=false
Return
#If
Thx again! :)

The reason why it never reset back to Seqkeys(params*) after toggle is that toggle:=false is outside the toggled lbutton hotkey.
Putting it inside toggle lbutton hotkey before the return statement fixes the issue.
#If Toggle
*lbutton::
send, 8
KeyWait, lbutton
Send, 7
toggle:=false
return

Related

Auto HotKey, Toggle: Hold Shift, Click and release LButton every second

I'm trying to make an AutoHotkey script that holds Shift, and while it's holding it I need the mouse to click and release every second. This is what I have come up with so far.
Home::
KeyDown := !KeyDown
If KeyDown
SendInput {Shift down}, Click, Sleep 2000
Else
SendInput {Shift up}
Return
This loop after a certain amount of time behavior is best implemented with a SetTimer function invoking a Subroutine.
Additionally, since your script holds down the Shift key, you would need to also have the hotkey be invoked whenever Shift+Home is pressed as well, so that it can be turned off.
Final Code:
Home::
+Home:: ;Alternative hotkey definition that invokes on Shift+Home
KeyDown := !KeyDown
if (KeyDown){
SendInput {Shift down}
gosub, clickSubroutine ;To trigger the first click immediately
SetTimer, clickSubroutine, 1000 ;To trigger clicks after every 1000 ms (1 second)
}
else{
SendInput {Shift up}
SetTimer, clickSubroutine, Off ;Turn off the clickSubroutine Loop
}
Return
clickSubroutine:
Click
return

AutoHotKey issue with AltTab shortcut

i was trying to create a simple script with AHK to switch between windows (similar to the "recent" button on a samsung mobile) with this code:
XButton2::
Send, {Alt down}{Tab}
Return
the problem is that i don't know how to make the script to release the Alt key when i strike Enter or click the Left mouse button.
Any help please?
XButton2::
AltTabMenu := true ; assign the Boolean value "true" or "1" to this variable
Send, {Alt down}{Tab}
return
; The #If directive creates context-sensitive hotkeys:
#If (AltTabMenu) ; If this variable has the value "true"
; The * prefix fires the hotkey even if extra modifiers (in this case Alt) are being held down
*Enter::
*MButton::
Send {Enter}
Send {Blind}{Alt Up} ; release Alt
MouseCenterInActiveWindow()
AltTabMenu := false
return
~*LButton::
Click
Send {Blind}{Alt Up} ; release Alt
MouseCenterInActiveWindow()
AltTabMenu := false
return
; menu navigation by scrolling the mouse wheel:
*WheelUp:: Send {Right}
*WheelDown:: Send {Left}
#If ; turn off context sensitivity
MouseCenterInActiveWindow(){
WinGetPos,,Y, Width, Height, A ; get active window size
Xcenter := Width/2 ; calculate center of active window
Ycenter := Height/2
MouseMove, Xcenter, Ycenter, 0
}

Q> AHK Add a Rule Disable RMouse and LMouse During MMB click and hold

is there a way to add this rule to this script
r::
send {Mbutton down}
KeyWait, r
send {Mbutton Up}
return
MMB is the default navigation orbit for sketchup i want to swap it to R
and i was able to do it
now i want to add a rule that when i hold and press mmb it will automatically disable Left Mouse Button and Right Mouse Button
so that it will not activate pan or context menu
Thank you in Advance
$r:: ; The $ prefix forces the keyboard hook to be used to implement this hotkey.
send {Mbutton down}
Disable_Left_Right_Button = 1 ; creates the variable "Disable_Left_Right_Button" and sets its value to 1 (= true)
KeyWait, r ; waits until the key is released
send {Mbutton Up}
Disable_Left_Right_Button = 0 ; sets the value of the variable to 0 (= false)
return
;define the context in which the mouse buttons should be inactive:
#If (Disable_Left_Right_Button = 1) ; Code below is only valid if the value of the variable is true.
; That means that if it's true, the mouse buttons are not going to work.
Lbutton::
RButton::
return
#If ; turns off the context sensitivity

Shift Key Training Wheels and Shift Parenthesis Remap

I'm looking to use AutoHotKey to modify the functionality of my shift keys. The functionality is described in Steve Losh's Blog entry here. Specifically, I'd like my shift keys to do the following:
If LShift or RShift is pressed and released in under 300 ms with no other keys being pressed in between, send ( or ), respectively.
If LShift and RShift are "rolled" together (press LShift, press RShift, release LShift, release RShift, etc.) in under 300ms, send () or )(.
If a shift key is used improperly (LShift and S, RShift and K, etc.) then nothing happens.
I've been having issues with the 300ms requirement and the "rolling" functionality. Specifically, I'm having issues with only being able to detect when the key is released due to the hotkey combos such as:
LShift & 0:: return
This is where I'm at so far:
LShift::
Send {LShift Down}
KeyWait, LShift
Send {LShift Up}
if (A_TimeSinceThisHotkey < 300){
if (A_PriorKey = "LShift")
{
Send {)}
}
}
return
I don't see a reason to use a 300 ms timeout anyway, it seems unreliable and unnecessary.
Have a look at this commented code, it is short and efficient, and seems to meet all of your requirements:
LShift::Send, (
RShift::Send, )
LShift & RShift:: Send, ()
RShift & LShift:: Send, )(
/* Put your unwanted combinations here
* and let them do nothing
*/
LShift & q::return
RShift & y::return
Edit:
Since LShift and RShift already are prefix hotkeys, I left out the trick described here.
MCL's answer is close but when I tested it I found that shift-clicking didn't select text. Here's a version with a passthrough to allow shift-clicking to work.
;shift to parens
LShift::Send, (
RShift::Send, )
LShift & RShift:: Send, ()
RShift & LShift:: Send, )(
;passthrough for shift-click
LShift & LButton::
Send, {LShift Down}{LButton}
Send, {LShift Up}
RShift & LButton::
Send, {RShift Down}{LButton}
Send, {RShift Up}
I don't think the 300ms timeout is possible without either very deep understanding of autohotkey's implementation or actual modification to autohotkey. When I tried to get it to work (using http://www.autohotkey.com/board/topic/98742-remapping-shift-key/) I found that A_PriorHotkey was not consistently populated. I don't think that variable was meant to work with modifier keys this way.
I felt compelled to figure this one out. Here you go!
I basically created a hotkey for every Shift + Letter key combination in order to send the correct key case and also set the Abort value. The Abort value is then referenced whenever one of the Shift keys is pressed in order to determine whether or not to send the corresponding ( or ).
The "Rolling" was accomplished by creating a Hotkey for LShift + RShift (and the opposite). It then looks to see which key is released first to determine () or )(.
Accept if this was what you were looking for!
Loop 26
{
Hotkey, % "~LShift & " Chr(A_Index+96), LetterKey ; Hotkeys for A-Z
Hotkey, % "~RShift & " Chr(A_Index+96), LetterKey ; Hotkeys for A-Z
}
Return
RShift::
LShift::
Abort := 0
keyDown := A_TickCount
Keywait, % A_ThisHotkey
duration := A_TickCount - keyDown
If (duration < 200) and (Abort = 0)
SendInput % (A_ThisHotkey = "LShift") ? "(" : ")"
Send {LShift Up}
Return
RShift & LShift::
LShift & RShift::
Keywait, LShift
If GetKeyState("RShift", "P")
{
Keywait, RShift
Send ()
}
Else
Send )(
Return
LetterKey:
SendInput, % "+" SubStr(A_ThisHotKey, 0, 1)
Abort := 1
Return
EDIT:
Hmm, I seem to be having the same problem as you. I always get a duration of 0 due to the hotkeys.
I found and modified this script on the AutoHotKey Forums. (The original script was prone to type "K(" when you meant to type "K" if you type too quickly, so I've modified it so that shouldn't happen any more)
$LShift Up::send, % getkeystate("LShift") ? "{LShift Up}" : ""
$RShift Up::send, % getkeystate("RShift") ? "{RShift Up}" : ""
~$LShift::
KeyWait, LShift, T0.1 ; wait 100ms to check shift state
if (A_PriorKey = "LShift")
{
send, % getkeystate("LShift") ? "{LShift Down}" : "("
}
KeyWait, LShift
return
~$RShift::
KeyWait, RShift, T0.1 ; wait 100ms to check shift state
if (A_PriorKey = "RShift")
{
send, % getkeystate("RShift") ? "{RShift Down}" : ")"
}
KeyWait, RShift
return

Autohotkey: Can't figure out why this is pushing Shift along with the rest of the code

I am trying to make a macro that will repeat hitting O and M as long as i have my side mouse button held down, but all of a sudden now it is also hitting shift. Any idea why or how to fix it?
delaybetweenfkeys:=0
fkeydowndelay:=5
XButton1::
Down := True
Send, {O Down}{m Down}
Loop
{
Send, {O Down}{m Down}
Sleep, 5
Send, {O Up}{m Up}
If !Down
Break
}
Send, {O Up}{m Up}
Return
XButton1 Up::Down := False
You're telling it to send a capital 'O', and to achieve that, it sends Shift+o. So if you have some other key being pressed in the meantime, it will get shifted as well.