Remapping issue Autohotkey - autohotkey

Please, bear with me, I'm a autohotkey noob. I want left: control and shift to switch from their current key value to arrow: down and up respectively when pressing the F2 key. And when the F2 key is pressed again, I want left: control and shift to return to their original key value. This is my code so far:
F2::
{
if LShift = Up
{
LShift::Send {LShift}
}
else
{
LShift::Send {Up}
}
if LControl = Down
{
LControl::Send {LControl}
}
else
{
LControl::Send {Down}
}
}
Regrettably I haven't been able to make it work. Any help will be truly appreciated.

Hotkeys that go over multiple lines don't use curly braces. They
start with the hotkey label and end with a simple Return.
You can't enclose hotkey labels by normal if statements. You have to use
special #If statements instead.
Nesting hotkeys is also definitely not what you want to do.
To remap a key you can use the single line hotkey syntax.
You can pretty much short your code down to:
F2::(F2Toggle:=!F2Toggle)
#If F2Toggle
LShift::Up
LControl::Down
#If
Pressing F2 will toggle the variable F2Toggle between true and false. #If F2Toggle means if the variable F2Toggle is currently set to True then enable the hotkeys below. #If marks the end of the special #If statement.

Related

Why autohotkey deos not send Ctrl-Space where Space is an input

Consider the following assignment:
When I type - followed by a key, the result is Ctrl-key. This work for ordinary keys.
But when the key is whitespace, it does not work.
Any idea why this happens? And how to fix the code?
-::
Input, key, L1,{LCtrl}
send, ^{%key%}
return
Edit.
Try to run the above script a program which has Ctrl-Space as a shortcut to see that it does not work. In fact, if you press - followed by Space, the script is suppose to call Ctrl-Space but it is not the case. For example:
In Microsoft Excel or in Libreoffice Calc, Ctrl-Space can select the current column.
In Emacs Ctrl-Space is reserved for setting a Mark.
Use SendInput instead.
Tested in Excel to mimic ^a, ^x, ^v, ^space
-::
Input, key, L1,{LCtrl}
SendInput, ^%key%
Return
If you want to handle "special" keys, add those keys to the list of endkeys using this syntax
Input [, OutputVar, Options, EndKeys, MatchList]
And then check to see which endkey was pressed
Tested in Firefox to mimic ^PgDn, ^PgUp
Input, key, L1,{LCtrl}{PgUp}{PgDn}
If (ErrorLevel = "EndKey:PgUp") {
SendInput ^{PgUp}
}
Else If (ErrorLevel = "EndKey:PgDn") {
SendInput ^{PgDn}
}
Else If (ErrorLevel = "EndKey:LCtrl") {
Return ;assumed you just want to abort input with the control key
}
Else {
SendInput, ^%key%
}
Return

Can I send special operators in autohotkey?

I want an AHK script which when I press NumPad7 returns {.
Now, in my keyboard { is Ctrl + Alt + ´
But the script
`NumPad7::
Send, ^!´
return`
does nothing for some reason.
Update: It does work with regular keys like this:
p::
Send; ^!´
return
Edit 2:
NumLock had to be on, now it works fine.
This script gives you { from 7 on numeric keypad regardless of NumLock state:
NumPad7::
NumPadHome::
SendRaw {
Return

Can I list several keys to perform the same action? [AHK]

AHK allows to bind keys, that is us a::z t fire 'z' whenever 'a' is pressed.
What if I want to fire 'z' whenever 'a', 'b', or 'c' is pressed?
I can obviously repeat my code:
a::z
b::z
c::z
I can probably use a Gosub like
a::Gosub, abc
b::Gosub, abc
c::Gosub, abc
abc:
send z
return
Is there a better way to say "if a,b, or c are pressed - fire z"?
You can just use
a::
b::
c::z
i am not sure what is the exact synthax, but this works.
We're at codegolf.stackexchange.com, right?
JFF, here's assigning A-Y to Z with just 61 characters, using the Hotkey command:
loop,25
hotkey,% chr(a_index+64),z
return
z(){
send z
}
Another solution using Hotkey to define hotkeys on the fly, and parse so that the user can directly specify a list of keys:
; Thanks engunneer: autohotkey.com/board/topic/45636-script-to-prevent-double-typing/?p=284048
; Thanks throwaway_ye: https://www.reddit.com/r/AutoHotkey/comments/54g40q/how_can_i_bind_several_keys_to_the_same_command/d81j0we
; The following part must be located in the auto-execute section,
; i.e. the top part of the AHK script.
keylist = 1234567890qwertzuiopasdfghjklyxcvbnm
Loop, parse, keylist
{
Hotkey, $%A_LoopField%, SendGivenKey
}
Return
; This can be located anywhere in the AHK file
SendGivenKey:
StringReplace, key, A_ThisHotkey, $, , All
send %key%
Return

AHK: Assign hotkey only for one specific active window and not for others

I have just done a piece of code that does the following thing. When I make a selection by mouse in Firefox or EndNote, the script sents a Ctrl+c and checks the clipboard for a regex match. If there is a match, it changes the clipboard contents and shows a tooltip. It works fine for these two programs. Adobe Acrobat sometimes shows an error when a Ctrl+c is sent (even if a user presses a ctrl-c Acrobat sometimes shows famous "There was an error while copying to the Clipboard. An internal error occurred). So it decided to assign an F9 hotkey, but it works for all programs and not just for Acrobat. How do I assign an hotkey for only one window – Acrobat? Here's my code. I know it's lame – I am a newbie to programming in general, and in AHK in particular.
#If WinActive("ahk_exe firefox.exe") || WinActive("ahk_exe EndNote.exe") || WinActive("ahk_exe Acrobat.exe")
if WinActive("ahk_exe Acrobat.exe")
F9::
{
Clipboard:=""
send,^c
ClipWait, 1
ToolTip % Clipboard := RegExReplace(Clipboard, "\r\n", " ")
SetTimer, ToolTipOff, -1000
}
return
~LButton::
now := A_TickCount
while GetKeyState("LButton", "P")
continue
if (A_TickCount-now > 500 )
{
Send ^c
if WinActive("ahk_exe firefox.exe")
{
If RegExMatch(Clipboard, "[0-9]\.\s[A-Za-z,]*\s[A-Za-z]*")
{
regex := "[0-9]\.\s*|\s?\([^)]*\)|\."
replace := ""
}
else If RegExMatch(Clipboard,"[0-9]{2}[-\/][0-9]{2}[-\/][0-9]{4}")
{
Clipboard := RegExReplace(Clipboard, "^0", "")
regex := "\/"
replace := "."
}
else return
}
else if WinActive("ahk_exe EndNote.exe")
{
If RegExMatch(Clipboard, "[a-z]+\,\s[A-Z0-9‘“]")
{
regex := "\??!?\:|\?|!"
replace := "."
}
else return
}
ToolTip % Clipboard := RegExReplace(Clipboard, regex, replace)
SetTimer, ToolTipOff, -1000
}
return
#If
ToolTipOff:
ToolTip
return
I see some very fundamental problems in the first few lines. Let me explain...
There are two types of if-statements in AutoHotkey If and #If.
You usually always use the normal If-statements unless you are doing something with hotkeys and you want specific hotkeys to be context-sensitive.
Here are some important rules:
Normal If-statements have to use curly braces {} to mark the area of code that should be executed if the expression is true. If you don't use curly braces, the If-statement will work as if you had put curly braces around the first command directly under the If-statement.
Example:
If WinActive("Firefox") {
Send, Test
MsgBox, The script just typed "Test.
}
Another example:
If WinActive("Firefox")
MsgBox, Firefox is the active window.
Normal If-statements cannot be used around a hotkey definition, but only within it.
This is allowed:
F1::
If (A_OSVersion = "WIN_7") {
MsgBox, Your operating system is Windows 7 and you just pressed F1.
}
Return
This is NOT:
If (A_OSVersion = "WIN_7") {
F1::
MsgBox, Your operating system is Windows 7 and you just pressed F1.
Return
}
But there is a way around that and that is #If-statements.
#If-statements don't use curly braces ever.
They can only be used on hotkey definitions.
And they can only be closed by another #If-statement.
(It's very common to simply use an empty #If to close it.)
Examples:
#If (A_OSVersion = "WIN_7")
F1::
MsgBox, Your operating system is Windows 7 and you just pressed F1.
Return
#If
A more complex example:
#If (A_ScreenWidth >= 1920)
F1::
MsgBox, Your your screen is at least 1920 pixels wide.
Return
F2::
MsgBox, Your operating system is %A_OSVersion%.
Return
#If (A_ScreenWidth < 1920)
F1::
MsgBox, Your your screen width is smaller than 1920 pixels.
Return
#If
As you might have guessed by now, hotkey definitions are always started by a pattern like this hotkey:: and closed by a Return. Although you can define hotkeys on a single line.
Examples:
F1::MsgBox, Hello!
F2::a ;This will remap the F2 key to an a-key.
Hotkeys by themselves do never use curly braces! Though an If-statement within a hotkey still has to use them according to the before mentioned rules.

Konami Code on autohotkey

up up down down left right left right b a enter :: Msgbox, konami code.
is there a way to do this?
yes its actually pretty simple...
comb := "up|down|down|left|right|left|right|b|a|enter"
~up::
Loop, parse, comb, |
{
input, var,T.900,{%a_loopfield%}
if inStr(ErrorLevel, "Timeout")
return
}
msgbox Konami Code!!!
return
The first "up" is the one that will trigger the sequence hence only one "up" in the combination variable.
you can change the combination to whatever you want, but then you would have to change the hotkey to the first "key" that you want to press.