Hi, I'm trying to make a looping case changer like the one in MS Word. There if you hold down the Shift key and tap on the F3 key, it will cycle through setting the selection to lower case, upper case and Title case and then back to lower case on the fourth tap of F3.
I have succeeded in doing simple modifications and synthesizing of skrommel's excellent CAPShift, HotKeyIt's RapidHotkey and Laszo's Morse Key, both also excellent.
I have two problems now:
1. Unlike MS Word, the case doesn't change until I lift the shift key. In Word every time you tap the F3 key and change happens right away. I have tried a bunch of things to fix this without success.
2. I was able to move the text insertion point/caret back to the start, selecting along the way by sending the shift key, however, if I'm changing the case of a whole paragraph, the caret takes some time to get back to the start. It would nice if it just jumped and still kept the paragraph selected.
Thanks in advance for the help.
Below is the code:
Code:
Shift & F3::RapidHotkey("LOWER""UPPER""TITLE""INVERT",1,0.2,1)
LOWER:
Gosub,CUT
StringLower,string,string
Gosub,PASTE
Return
UPPER:
Gosub,CUT
StringUpper,string,string
Gosub,PASTE
Return
TITLE:
Gosub,CUT
StringLower,string,string,T
Gosub,PASTE
Return
INVERT:
AutoTrim,Off
StringCaseSense,On
Gosub,CUT
lower=abcdefghijklmnopqrstuvwxyzæøå
upper=ABCDEFGHIJKLMNOPQRSTUVWXYZÆØÅ
StringLen,length,string
Loop,%length%
{
StringLeft,char,string,1
StringGetPos,pos,lower,%char%
pos+=1
If pos<>0
StringMid,char,upper,%pos%,1
Else
{
StringGetPos,pos,upper,%char%
pos+=1
If pos<>0
StringMid,char,lower,%pos%,1
}
StringTrimLeft,string,string,1
string=%string%%char%
}
Gosub,PASTE
Return
CUT:
Send,^x
ClipWait,1
string=%clipboard%
Return
PASTE:
clipboard=%string%
Send,^v
StringLen,length,clipboard
Send,{Shift Down}
Send,{Left %length%}
Send,{Shift Up}
Return
;#####################################
;RapidHotkey() and Morse() functions #
;#####################################
RapidHotkey(keystroke, times="1", delay=0, IsLabel=1)
{
Pattern := Morse(delay*1000)
If (StrLen(Pattern) < 2 and Chr(Asc(times)) != "1")
Return
If (times = "" and InStr(keystroke, """"))
{
Loop, Parse, keystroke,""
If (StrLen(Pattern) = A_Index+1)
continue := A_Index, times := StrLen(Pattern)
}
Else if (RegExMatch(times, "^\d+$") and InStr(keystroke, """"))
{
Loop, Parse, keystroke,""
If (StrLen(Pattern) = A_Index+times-1)
times := StrLen(Pattern), continue := A_Index
}
Else if InStr(times, """")
{
Loop, Parse, times,""
If (StrLen(Pattern) = A_LoopField)
continue := A_Index, times := A_LoopField
}
Else if (times = "")
continue = 1, times = 2
Else if (times = StrLen(Pattern))
continue = 1
If !continue
Return
Loop, Parse, keystroke,""
If (continue = A_Index)
keystr := A_LoopField
Loop, Parse, IsLabel,""
If (continue = A_Index)
IsLabel := A_LoopField
hotkey := RegExReplace(A_ThisHotkey, "[\*\~\$\#\+\!\^]")
IfInString, hotkey, %A_Space%
StringTrimLeft, hotkey,hotkey,% InStr(hotkey,A_Space,1,0)
Loop % times
backspace .= "{Backspace}"
keywait = Ctrl|Alt|Shift|LWin|RWin
Loop, Parse, keywait, |
KeyWait, %A_LoopField%
If ((!IsLabel or (IsLabel and IsLabel(keystr))) and InStr(A_ThisHotkey, "~") and !RegExMatch(A_ThisHotkey
, "i)\^[^\!\d]|![^\d]|#|Control|Ctrl|LCtrl|RCtrl|Shift|RShift|LShift|RWin|LWin|Escape|BackSpace|F\d\d?|"
. "Insert|Esc|Escape|BS|Delete|Home|End|PgDn|PgUp|Up|Down|Left|Right|ScrollLock|CapsLock|NumLock|AppsKey|"
. "PrintScreen|CtrlDown|Pause|Break|Help|Sleep|Browser_Back|Browser_Forward|Browser_Refresh|Browser_Stop|"
. "Browser_Search|Browser_Favorites|Browser_Home|Volume_Mute|Volume_Down|Volume_Up|MButton|RButton|LButton|"
. "Media_Next|Media_Prev|Media_Stop|Media_Play_Pause|Launch_Mail|Launch_Media|Launch_App1|Launch_App2"))
Send % backspace
If (WinExist("AHK_class #32768") and hotkey = "RButton")
WinClose, AHK_class #32768
If !IsLabel
Send % keystr
else if IsLabel(keystr)
Gosub, %keystr%
Return
}
Morse(timeout = 400) { ;by Laszo -> http://www.autohotkey.com/forum/viewtopic.php?t=16951 (Modified to return: KeyWait %key%, T%tout%)
tout := timeout/1000
key := RegExReplace(A_ThisHotKey,"[\*\~\$\#\+\!\^]")
IfInString, key, %A_Space%
StringTrimLeft, key, key,% InStr(key,A_Space,1,0)
Loop {
t := A_TickCount
KeyWait %key%, T%tout%
Pattern .= A_TickCount-t > timeout
If(ErrorLevel)
Return Pattern
KeyWait %key%,DT%tout%
If (ErrorLevel)
Return Pattern
}
}