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
Related
I'm trying to create a paste function that simulate a write "character by character" inside an input field, but my code doesn't work.
Here is my code:
^+V::
Loop, parse, clipboard, `n, `r
{
SendRaw, %clipboard%
}
return
You're trying too hard. Send command will already send key by key of whatever content you may pass into it.
; Use this in case of delaying each key press.
; SetKeyDelay, Delay
^+V::
SendRaw, %clipboard%
Return
^v::
ClipBucket5 := Clipboard
ClipBucket5 := RegExReplace(ClipBucket5, "\r\n?|\n\r?", "`n")
Loop, parse, ClipBucket5
{
SendRaw, %A_Loopfield%
Sleep, 100
}
return
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.
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
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.
Similar to what this question covers, I'm trying to bind two sequences of keys.
Ideally I'd like to bind Alt DOWN,-,-,-,Alt UP to an em-dash (—) and Alt DOWN,-,-,Alt UP to an en-dash (–).
What I have almost works for em-dashes but not quite:
; Em-dash
!-::
Input Key, L1
if Key=-
Input Key, L1
if Key=-
Send {ASC 0151}
return
; En-dash
;!-::
;Input Key, L1
;if Key=-
;Send {ASC 0150}
;return
The em-dash sequence works like Alt+-,-,-, instead of what I'm trying to match. I'm not sure how to only test for Alt DOWN and Alt UP. The en-dash sequence fails altogether to bind because !- has already been bound.
Have a look at this one:
dashCount := 0
!-::
dashCount++
if(dashCount = 1) {
SetTimer, WaitForAlt, -1
}
return
WaitForAlt:
KeyWait, Alt
if(dashCount = 2) {
Send {ASC 0150}
} else if(dashCount = 3) {
Send {ASC 0151}
}
dashCount := 0
return
It seems to do the job well. The code works by counting each time Alt + - gets pressed. Concurrently, a pseudo-thread is spawned that waits for Alt to be released and then sends the appropriate dash, depending on the counter.