Quantcast
Channel: ExcellentCruft blogs
Viewing all articles
Browse latest Browse all 38

Case Flipping By Hot-Key

$
0
0

There's this one app I work in that has the annoying feature of activating my Caps Lock key on certain screens. Depending upon how much keyboard work I'm doing, I might not notice until I write one of my associates a text message and find that I'm screaming at them about the time I hit the "Send" key. They are used to the app so they just laugh when they see it. But, in my mind, there is virtually no annoyance too trivial to fix with an overly complex script. Or four of them,

If you do any kind of coding, especially in established apps originally written by people with no interest in consistency, you may encounter stuff like this:

$var1="TheTest";
$Var2="another test";
$VAR3="Doe's this think work?";
$_ACONSTANT="4";
$AnoTherCOnstant="Lots";
echo"<p>I write stuff like this because I hate Dan ".$AnoTherCOnstant." .</P>";

I tend to be very consistent with my coding because it makes it easier to read six months later when I have no idea what I was thinking. Whenever I'm working on one these apps, I spend significant time refactoring the code into something I'd be willing to pass off to another developer with anger management issues and a gun (because you never know). I've compiled a number of AutoHotKey scripts to make the casing parts of the task a bit easier. To use these, you just need the base AHK app locally installed along with a resident start-up script.

The first two are very basic: They convert whatever you've selected into either all caps, or all lower case:

; Convert selected text to UPPERCASE
^+Up::
strConvert := GetSelectedText()
StringUpper strConvert, strConvert
Send%strConvert% 
return

; Convert selected text to lowercase
^+Down::
strConvert := GetSelectedText()
StringLower strConvert, strConvert
Send%strConvert%
return

I've chosen Control + Shift + either the up or down arrow keys to trigger the conversion. So, let's say you start with:

thE quicK browN foX

You select the phrase, and then CTRL+SHFT+UP results instantly in:

THE QUICK BROWN FOX

In fact, I just used it. CTRL+SHFT+DOWN does just the opposite.

In the afore mentioned texting situation, I have a bad habit of looking away from the screen why typing shortmessages. With Caps Lock on, the results tend to look something like this:

dUDE - YOU GUYS DOING LUNCH?  wHAT ABOUT tACO mAC?
If you catch that before hitting send, you can fix it pretty easily with the Invert Case script:
; Invert selected string case
^+i::
 Char_Out:=""
 strInvert := GetSelectedText()
 Loop % Strlen(strInvert){
    Invert_Char:=Substr(strInvert,A_Index,1)
    if Invert_Char is upper
       Char_Out:= Char_Out Chr(Asc(Invert_Char)+32)
    elseif Invert_Char is lower
       Char_Out:= Char_Out Chr(Asc(Invert_Char)-32)
    else
       Char_Out:= Char_Out Invert_Char
 }
 Send%Char_Out%
return
So, here, CTRL+SHFT+ i results in:
Dude - you guys doing lunch?  What about Taco Mac?

The last one I use is a bit more of an edge case. If you need to capitalize the fist charachter, and then lower case the rest, either with one one, or a series of them, you might want something sometimes called "Proper Case". This is what titles look like, or some coding converntions.

; Convert selected string to proper case
^+p::
 Char_Out:=""
 strConvert := GetSelectedText()
 strLast:=""
 Loop % Strlen(strConvert){
    Convert_Char:=Substr(strConvert,A_Index,1)
        if(strLast ==""){
                StringUpper Convert_Char, Convert_Char
                }else{
                        if(strLast ==""){
                                        StringUpper Convert_Char, Convert_Char
                        }else{
                                StringLower Convert_Char, Convert_Char 
                        }       
                        }
        Char_Out:= Char_Out Convert_Char
        strLast:=Convert_Char  
}
Send%Char_Out%
return

So, using it (CTRL+SHFT+ p) on the variables in the fist example, they would become:

$Var1="TheTest";
$Var2="another test";
$Var3="Doe's this think work?";

... or a book title string might change from:

"the sun also rises"

to...

"The Sun Also Rises"

So there you have it. If you find yourself having to go back and fix casing with any regularity, hopefully this will help.


Viewing all articles
Browse latest Browse all 38

Latest Images

Trending Articles





Latest Images