How to get the actual hotstring typed with capitalization in AutoHotKey? - autohotkey

I am using hotstrings in AutoHotKey and the output is reliant on if characters are capitalized. Is there a way to determine if characters in the keys typed that triggered the hotkey are capitalized? I tried using A_ThisHotkey, but it does not seem to be case sensitive. Please let me know if you have a solution. Thanks.

I'm not aware of a built in way to do this, but maybe you'd be fine with just creating more hotstrings and using the C option for case sensitivity like this:
:C:hello::
:C:Hello::
:C:HeLLo::
:C:HELLO::
MsgBox, % A_ThisHotkey
return
Example request from comments:
hotstrings := "hello,HELLO,HeLlo,HellO,hElLo,hellO"
for each, hotstring in StrSplit(hotstrings, ",")
Hotstring(":CB0*?:" hotstring, Func("MyFunction"))
return
MyFunction()
{
MsgBox, % "Hotstring triggered!`n" A_ThisHotkey
}

You can activate the case sensitivity option before defining your Hotstrings
#Hotstring c
::Hello::Hi ; will be triggered only if you exactly typed these letters case-sensitive

Related

HotKey multiple characters

Is it possible to define an AutoHotKey HotKey which require multiple characters to fire ?
Example: if I write the AHK script
^j::
Send, my test
return
The Hotkey Control j fires off the string "my test"
What if I wanted to require Control jBAS to fire off the script ...in this case the letters BAS following the Control j ? From the examples I have seen, HotKey does not seem to allow this ...I can accomplish this with a HotString ...problem with HotString is that you must hit Enter and then the original String is replaced with the new HotString definition.
Heres an option:
^j::
Input, matchVal, C L3,, BAS
if (matchVal=="BAS") {
Send, mytest
}
Return
Check out https://www.autohotkey.com/docs/commands/Input.htm for further data and options.

How to manually trigger Autohotkey hotstrings?

In my main Autohotkey script I have several hundred hotstrings like these:
::fe::for example
::f::and
::fi::for instance
::fo::fortunate
::foy::fortunately
::glo::global
::gloy::globally
::ha::have
::hv::however
Fairly often it would be convenient to trigger a hotstring manually (e.g. by pressing ALT-9) rather than pressing and end character. Is there a way to do this? I haven't found anything in my Googling, so maybe there isn't. But it would be useful.
I've read the hotstrings options e.g. :*: but this isn't the same - I want normal hotstring operation, but also the option to manually force them to trigger as needed.
Updated:
So what you are in fact looking for is using ALT+9 as an end character. I'm not sure but you can probably not use key-combinations for that (see hotstring doc). I cannot think of a really clever way of doing that right now. You might try something like
::fe::
Transform, CtrlC, Chr, 3 ; comes from ahk input documentation, I never really understood how this is supposed to work but I guess there is a code for alt 9 as well somehow
input, key, L1 I M ; wait for the next 1 key
if(key==CtrlC)
sendraw forExample
return
Old answer:
You have to outsource the hotstring body:
::fe::gosub forExample
forExample:
send for example
return
, then you can define a hotkey somewhere:
!9::gosub forExample
If you want to be cool, use functions instead of subroutines.
Note:
::fe::something
is just a short form for
::fe::
send something
return
::fe::for example
::f::and
::fi::for instance
::fo::fortunate
::foy::fortunately
::glo::global
::gloy::globally
::ha::have
::hv::however
!8:: trigger_hotstring("fi")
!9:: trigger_hotstring("ha")
trigger_hotstring(hotstring){
Loop, Read, %A_ScriptFullPath%
{
If InStr(A_LoopReadLine, "::"hotstring "::")
{
SendInput, % StrSplit(A_LoopReadLine,"::"hotstring "::").2
break
}
}
}
If you use AutoHotkey v1.1.06+ you can use #InputLevel
::fe::for example
::f::and
; and so on
#InputLevel, 1 ; sending space will trigger hotstrings above this line
!F9::Send {space}
Edit:
I now see you want to omit the end char which needs a bit of extra work
One way would be to duplicate the hotstrings with additional options:
::fe::for example
:*O:fe_::for example ; duplicate the hotstrings like so
::f::and
::fi::for instance
::fo::fortunate
; and so on
#InputLevel, 1
!9::Send _
Another way would be to remove the endchar
::fe::for example
::f::and
::fi::for instance
::fo::fortunate
; and so on
#InputLevel, 1
!9::
Send {space}
Sleep 100 ; give it time to expand the hotstring, experiment with timing
Send {bs} ; now remove trailing space
Return
I generally use TAB as a trigger for all my hotstrings. like
:*:#pm ::mail1#protonmail.com
:*:#G ::mail2#gmail.com
:*:Btw ::By the way,{Left}
Note that the space that you see here is a tab and not a space. you can do this instead of doing Alt+9 to trigger your macro hotstring.
you can even use more than one Tab so you can be sure that you only trigger it when you really intend to.

(AHK) Creating variable hotkeys that gets the key names from a 2 char file name of a script

I'm trying to make something for our employees to use so that they dont have to alter the script itself to define hotkeys. This may only work for hotkeys which can be defined by a single character, but that's fine, as there are so many combinations that can be made with them, and they can be very easy to remember. The script would look only at 2 character AHK files (or 6 if you must include the extension) in the working directory. And the variables it would search for could be defined with RegEx so for the first hotkey, it would look like ^. and then second would look like .(?=.) Once a match is found, it would simply launch that matched file. Has something like this been done before? It seems so simple but I can't seem to find anything on it.
Edit: Elliot brought this to my attention: http://autohotkey.com/board/topic/60630-easy-editmanage-hotkeyshotstrings-plugin-ahk-l/
It's a neat script manager, and very useful, but it's not what I'm looking for.
I dont not want an additional interface. I want to be able to change the hotkeys by using the filename.
Based on the answer of Forvin. Added the execution of the corresponding ahk script.
#Persistent
SetTimer, FindNewHotkeys, 2000
FindNewHotkeys:
Loop, %A_ScriptDir%\*
{
RegExMatch(A_LoopFileName, "^(.)(.).ahk$", hk)
If (hk)
{
Hotkey, ~%hk1% & ~%hk2%, HotkeyLabel
}
}
Return
HotkeyLabel:
RegExMatch(A_ThisHotkey, "~(.) & ~(.)", hk)
run, %hk1%%hk2%.ahk
Return
#Persistent
SetTimer, FindNewHotkeys, 2000
FindNewHotkeys:
Loop, %A_ScriptDir%\*
{
RegExMatch(A_LoopFileName, "^(.)(.).ahk$", hk)
If (hk)
Hotkey, ~%hk1% & ~%hk2%, HotkeyLabel
}
Return
HotkeyLabel:
MsgBox, A hotkey has been pressed!
Return

AHK key combination issue

Full code: http://pastebin.com/AX8iNRE6
Issue: I have various combinations of letters that work as a trigger to switch latin letters to cyrilic ones, for instance:
:*:h::
SendInput, х
FingerPrint(22)
return
:*:s::
SendInput, с
FingerPrint(22)
return
:*:sh::
SendInput, ш
FingerPrint(22)
return
so it would switch 'sh' just to 'ш'. Instead, when I possibly don't type fast enough(or any other reason) it starts proceeding to substitute 's' and 'h' separately, giving me 'сх'
Is there any way I could go around this? maybe a condition that waits for next character or something.
Thanks in advance!
You could either try to build up something yourself using Input. Or, more comfortably, use Polythene's dynamic regEx-Hotstring library:
#persistent
#include hotstrings.ahk
hotstrings("s([^h])", "c%$1%") ; s followed by any non-h-character
return
:*:h::x
:*:sh::?

Replicate ++ operator via AutoHotKey script?

Let's say I'm programming in a text editor and want to iterate a variable:
i++
Now, let's say for whatever reason, this language doesn't ++ but rather:
i = i + 1
this is annoying when you are used to the first syntax. I want a script which when receiving ++ translates this to a series of commands I send (control shift left arrow, etc, this is not the part I'm having trouble with).
However I cannot seem to get this method to execute:
+ & +::
Msgbox test
return
For whatever reason though, this is not fully being called - I do not see the keystroke for + when typing so I know it is somehow getting to that method, but, not registering the second +.
How can I call a method using the keystrokes ++ as a trigger?
There's a really great library called RegEx Powered Dynamic Hotstrings. And replacing someVar++ with someVar = someVar + 1 reeks of RegEx!
This one line will do what you want:
hotstrings("(\w+)\+\+", "%$1% = %$1% {+} 1")
This will work for every variable name that's alphanumeric (plus underscore): [a-zA-Z0-9_]
Of course, this won't work for every language since it strongly depends on the syntax. For example, some languages use := to assign expressions, other languages need a semicolon to complete a statement and so on...
Hotstrings WILL work for this occasion, however, you might need the "?" modifier for it to grab even within a "word" try it like this:
:*?:++::
ClipboardOld := ClipboardAll,Clipboard := ""
Send, +^{Left}^c
Clipwait
Send, ^{Right} = %Clipboard% {+} 1
Clipboard := ClipboardOld,ClipboardOld := ""
return
This also preserves the clipboard.