Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Auto capitalize first letter of sentence


  • Please log in to reply
23 replies to this topic
wyw
  • Members
  • 32 posts
  • Last active: Aug 01 2017 10:16 AM
  • Joined: 01 Jul 2015

Hi, can someone tell me a working script that automatically capitalizes the first letter of a sentence? All the scripts I found did not work. Not a single one.



flyingDman
  • Spam Officer
  • 2186 posts
  • Last active: Nov 07 2015 08:15 AM
  • Joined: 27 Feb 2009

If they don't work for you, please tell us which ones you tried and why they did not work.  Any error messages? 


Marine Corps Gen. Joseph Dunford told senators at his Joint Chiefs of Staff confirmation hearing : “If you want to talk about a nation that could pose an existential threat to the United States, I'd have to point to Russia. And if you look at their behavior, it's nothing short of alarming.”


wyw
  • Members
  • 32 posts
  • Last active: Aug 01 2017 10:16 AM
  • Joined: 01 Jul 2015

#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
#Persistent
#SingleInstance, Force
; #NoTrayIcon

SetBatchLines, -1
ListLines, Off
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.


AnyEndKeys =
( LTrim Join
{LWin}{RWin}{AppsKey}
{LShift}{RShift}{LControl}{RControl}{LAlt}{RAlt}
{F1}{F2}{F3}{F4}{F5}{F6}{F7}{F8}{F9}{F10}{F11}{F12}
{Left}{Right}{Up}{Down}
{Insert}{Delete}{Home}{End}{PgUp}{PgDn}
{Space}{Tab}{Enter}{Escape}{Backspace}
{CapsLock}{NumLock}{ScrollLock}
{PrintScreen}{Pause}
{Numpad0}{Numpad1}{Numpad2}{Numpad3}{Numpad4}
{Numpad5}{Numpad6}{Numpad7}{Numpad8}{Numpad9}
{NumpadIns}{NumpadEnd}{NumpadDown}{NumpadPgDn}{NumpadLeft}
{NumpadClear}{NumpadRight}{NumpadHome}{NumpadUp}{NumpadPgUp}
{NumpadDot}{NumpadDel}
{NumpadDiv}{NumpadMult}{NumpadSub}{NumpadAdd}{NumpadEnter}
)

StringReplace, AnyEndKeys, AnyEndKeys, }, , All
StringReplace, AnyEndKeys, AnyEndKeys, {, `, , All

beginning := 0 ; Change this to 1 to only capitalize the fist letter of a word.
keys := "abcdefghijklmnopqrstuvwxyz1234567890,./;'[]\-="
CAPS .= "A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z"

Loop, Parse, Keys
Hotkey, ~%A_LoopField%, checkKeys, On
Loop, Parse, AnyEndKeys, `,
Hotkey, ~%A_LoopField%, checkKeys, On
return

checkKeys:
key := SubStr(A_ThisHotkey, 2)
word .= key

Loop, Parse, CAPS, `, ; Make sure the hotkey isn't a cap.
if (A_LoopField == key)
return
if (InStr(key, "shift"))
return
else if key in %AnyEndKeys% ; Make sure the hotkey isn't an endkey.
{
word := ""
return
}

else if (A_ThisHotkey = A_PriorHotkey) ; if hotkey was pressed twice in a row.
{
if (StrLen(word) > 2) && (beginning)
return

else if (!counter)
{
StringUpper, key, key
if key is alpha
Send, {BackSpace 2}%key%
else
Send, {BackSpace 2}+%key%
counter++
}
else
{
Send, {BackSpace 2}{%key% 2}
counter := 0
}
}
else
counter := 0
return

 

This is one example, the script actually works, but you have to type twice to capitalize it.  What I want is that pressing once should automatically capitalize the first letter instead to type "aa" to get "A". And of course after a sentence ended with a dot.



garry
  • Spam Officer
  • 3219 posts
  • Last active: Sep 20 2018 02:47 PM
  • Joined: 19 Apr 2005

found also this example

;-This will capitalize the first word in each sentence -------------
e4x=
(
This is -1 . this is -2 ? this is -3 !
this is -4 . this is -5 .
)

aa:= % RegExReplace(e4x, "(?<=^)([a-z])|(?<=\.|!|\?)(\s*[a-z])", "$u1$u2")
;fileappend,%aa%,test55.txt
msgbox,%aa%
return



wyw
  • Members
  • 32 posts
  • Last active: Aug 01 2017 10:16 AM
  • Joined: 01 Jul 2015

Yeah, but that is sadly not what I'm looking for. That script just capitalizes the words you've entered in the script.



flyingDman
  • Spam Officer
  • 2186 posts
  • Last active: Nov 07 2015 08:15 AM
  • Joined: 27 Feb 2009

I think there are some other scripts out there, but have a look at the following.  It requires the hotstrings function which can be put in a library or included in the script.

It automatically capitalizes the first letter after a "." is typed. 0 or more spaces are allowed, though it automatically puts 1 spaces between the "." and the letter.

hotstrings("\.\s*(\w)", "label")
return

label:
Stringupper, $1, $1
Sendinput, % ". " $1
return

If you want a lower case 1st letter, you'll need to backspace and type it in,


Marine Corps Gen. Joseph Dunford told senators at his Joint Chiefs of Staff confirmation hearing : “If you want to talk about a nation that could pose an existential threat to the United States, I'd have to point to Russia. And if you look at their behavior, it's nothing short of alarming.”


wyw
  • Members
  • 32 posts
  • Last active: Aug 01 2017 10:16 AM
  • Joined: 01 Jul 2015

Also, I have found a other way:

 

 

#SingleInstance, force
; Half-QWERTY: One-handed Typing - version 3a
; http://www.autohotke...p=228783#228783
;
; HalfKeyboard invented by Matias Corporation between 1992 and 1996
; Originally coded in AutoHotkey by jonny in 2004
; Many thanks to Chris for helping him out with this script.
; Capslock hacks and `~ remap to '" by Watcher
; This implementation was done by mbirth in 2007
;
; version 3a script, mod by hugov:
; 2008-10-31:
; - mixed with "Capitalize letters after 1 second hold" at request of Calibran
;   http://www.autohotke...311.html#228311
;   just tested very briefly so try at your own peril :-)
;
; 2011-02-21
; - Added URLDownloadToFile for missing icon and keyboard layout
;   images downloaded from www.autohotkey.net if not found in %A_ScriptDir%
; - Added menu option to show keyboard layout in an AlwaysOnTop Gui (via Tray)

; 2011-02-21
IfNotExist, %A_ScriptDir%\halfkeyboard.ico
    iICO=0
IfNotExist, %A_ScriptDir%\halfkeyboard_help.gif
    iHELP=0
If (iICO=0) or (iHELP=0)
    {
     MsgBox 4, Some Graphics files are missing, do you wish to download them from www.github.com?
     IfMsgBox, Yes
        {
         URLDownloadToFile, https://raw.githubus...alfkeyboard.ico, %A_ScriptDir%\halfkeyboard.ico ; for tray
         URLDownloadToFile, https://raw.githubus...yboard_help.gif, %A_ScriptDir%\halfkeyboard_help.gif ; for 5 sec help at caret pos, transparent GIF
         URLDownloadToFile, https://raw.githubus...yboard_help.png, %A_ScriptDir%\halfkeyboard_help.png ; for permanent gui transparent PNG
        }
    }
; /2011-02-21    
    
KeyIsDown = 0
UpperDelay = 1000
UpperDelay *= -1

IfExist, %A_ScriptDir%\halfkeyboard.ico ; mod 2011-02-21
    {
     Menu Tray, Icon, %A_ScriptDir%\HalfKeyboard.ico
     Menu Tray, Tip, HalfKeyboard emulator
     Menu Tray, Add, &Show Keyboard layout, MenuShowKeyboardLayout
     Menu Tray, Add, E&xit, MenuExit
     Menu Tray, NoStandard
    }


RegRead KLang, HKEY_CURRENT_USER, Keyboard Layout\Preload, 1
StringRight KLang, KLang, 4
If (!KLang)
  KLang := A_Language

If (KLang = "0407") {
  ; 0407 DE_de QWERTZ mirror set
  original := "^12345qwertasdfgyxcvb"
  mirrored := "ß09876poiuzölkjh-.,mn"
} Else If (KLang = "040c" || KLang = "040C") {
  ; 040c FR_fr AZERTY mirror set         
  original := "²&é" . """" . "'(azertqsdfgwxcvb"   ; split up string for better
  mirrored := ")àç" . "_"  . "è-poiuymlkjh!:;,n"   ; human readability
} Else {
  ; 0409 US_us QWERTY mirror set
  original := "``" . "12345qwertasdfgzxcvb"   ; split up string for better
  mirrored := "'"  . "09876poiuy;lkjh/.,mn"   ; human readability
}


; Now define all hotkeys
Loop % StrLen(original)
{
  c1 := SubStr(original, A_Index, 1)
  c2 := SubStr(mirrored, A_Index, 1)
  Hotkey Space & %c1%, DoHotkey
  Hotkey Space & %c2%, DoHotkey
  Hotkey %c1%, KeyDown
  Hotkey %c1% UP, KeyUP
  Hotkey %c2%, KeyDown ; see post by guest below seems to improve the script haven't tried this myself so comment these two lines if it doesn't work
  Hotkey %c2% UP, KeyUP ;
}

return


; This key may help, as the space-on-up may get annoying, especially if you type fast.
Control & Space::Suspend

; Not exactly mirror but as close as we can get, Capslock enter, Tab backspace.
Space & CapsLock::Send {Enter}
Space & Tab::Send {Backspace}

; If spacebar didn't modify anything, send a real space keystroke upon release.
+Space::Send {Space}
Space::Send {Space}

; Define special key combos here (took them from RG's mod):
^1::Send {Home}
^2::Send {End}
^3::Send {Del}

; General purpose
DoHotkey:
  StartTime := A_TickCount
  StringRight ThisKey, A_ThisHotkey, 1
  i1 := InStr(original, ThisKey)
  i2 := InStr(mirrored, ThisKey)
  If (i1+i2 = 0) {
    MirrorKey := ThisKey
  } Else If (i1 > 0) {
    MirrorKey := SubStr(mirrored, i1, 1)
  } Else {
    MirrorKey := SubStr(original, i2, 1)
  }
 
  Modifiers := ""
  If (GetKeyState("LWin") || GetKeyState("RWin")) {
    Modifiers .= "#"
  }
  If (GetKeyState("Control")) {
    Modifiers .= "^"
  }
  If (GetKeyState("Alt")) {
    Modifiers .= "!"
  }
  If (GetKeyState("Shift") + GetKeyState("CapsLock", "T") = 1) {
    ; only add if Shift is held OR CapsLock is on (XOR) (both held down would result in value of 2)
    Modifiers .= "+"
  }

/*
  KeyWait, %ThisKey%, T1
  Send %Modifiers%{%MirrorKey%}
  If (A_TickCount - StartTime >= 1000)
     {
        StringUpper, MirrorKey, MirrorKey
   Send {Backspace}+%MirrorKey%
*/

If (KeyIsDown < 1 or ThisKey <> LastKey)
          {
               KeyIsDown := True
               LastKey := ThisKey
               Send %Modifiers%{%MirrorKey%}
               SetKeyDelay, 65535
               SetTimer, ReplaceWithUpperMirror, %UpperDelay%
          }

Return

Space & F1::
  ; Help-screen using SplashImage
  CoordMode Caret, Screen
  y := A_CaretY + 20
  If (y > A_ScreenHeight-100)
    y := A_CaretY - 20 - 100
  IfExist, %A_ScriptDir%\HalfKeyboard_help.gif ; 2011-02-21 for safety
      {
      SplashImage %A_ScriptDir%\HalfKeyboard_help.gif, B X%A_CaretX% Y%y%
      Sleep 5000
      SplashImage Off
    }  
return

MenuShowKeyboardLayout: ; 2011-02-21
IfWinNotExist, HalfKeyboard - permanent keyboard layout
    {
      Gui, +Owner +Toolwindow +AlwaysOnTop
     Gui, Add, picture, x0 y0 w310 h104, %A_ScriptDir%\halfkeyboard_help.png
     Gui, Show, w310 h104 NA, HalfKeyboard - permanent keyboard layout
     Menu, Tray, Check, &Show Keyboard layout
    }
Else
    {
     Gosub, GuiClose    
    }
Return

GuiClose:
Menu, Tray, UnCheck, &Show Keyboard layout
Gui, Destroy
Return


MenuExit:
  ExitApp
Return

KeyDown:
   Key:=A_ThisHotkey
        If (KeyIsDown < 1 or Key <> LastKey)
           {
                KeyIsDown := True
                LastKey := Key
                Send %Key%
                SetKeyDelay, 65535
                SetTimer, ReplaceWithUpper, %UpperDelay%
           }
        Return

KeyUp:
   Key:=A_ThisHotkey
        SetTimer, ReplaceWithUpper, Off
        SetTimer, ReplaceWithUpperMirror, Off
        KeyIsDown := False
        Return

ReplaceWithUpper:
SetKeyDelay, -1
Send {Backspace}+%LastKey%
Return

ReplaceWithUpperMirror:
SetKeyDelay, -1
Send {Backspace}+%MirrorKey%
Return

 

 

 

Now all I need is to make this capitalize the first letter of a sentence only one time, til I start a new sentence.

 

You have to hold the key for example "a" for one second to get "A", but after you did this, this should be disabled til the next sentence, or til you pressed enter. Would that be possible?



wyw
  • Members
  • 32 posts
  • Last active: Aug 01 2017 10:16 AM
  • Joined: 01 Jul 2015

No1?



flyingDman
  • Spam Officer
  • 2186 posts
  • Last active: Nov 07 2015 08:15 AM
  • Joined: 27 Feb 2009

Tweaked the one above a bit to allow for multiple spaces and multiple punctuation marks. Did you get the function "hotstrings" working?

hotstrings("([\.\?!])(\s*)(\w)", "label")
return

label:
Stringupper, $3, $3
Sendinput {Raw}%$1%%$2%%$3%
return

Regarding SoLong&Thx4AllTheFish's, aka Hugov,, script. I did not look at it closely, but I do not think you need a 220 line script to capitalize the 1st word of a sentence!.


Marine Corps Gen. Joseph Dunford told senators at his Joint Chiefs of Staff confirmation hearing : “If you want to talk about a nation that could pose an existential threat to the United States, I'd have to point to Russia. And if you look at their behavior, it's nothing short of alarming.”


3rror
  • Members
  • 18 posts
  • Last active:
  • Joined: 29 May 2013
#NoEnv
#Persistent
SetKeyDelay, -1
x := True  ; start off script with an assumed capital (true)

Loop
{	Input, key, C I L1 V
	
	if( x && RegExMatch(key,"[a-z]") )
	{
		Send {Backspace}+%key%
		x:= False
	}
	else if InStr(".?!", key)
		x := True
}


flyingDman
  • Spam Officer
  • 2186 posts
  • Last active: Nov 07 2015 08:15 AM
  • Joined: 27 Feb 2009

@3rror: Nice!


Marine Corps Gen. Joseph Dunford told senators at his Joint Chiefs of Staff confirmation hearing : “If you want to talk about a nation that could pose an existential threat to the United States, I'd have to point to Russia. And if you look at their behavior, it's nothing short of alarming.”


3rror
  • Members
  • 18 posts
  • Last active:
  • Joined: 29 May 2013

Thanks. Here's a version which inserts spaces if needed before the capitals. I  Forgot #persistent wasn't necessary with Input so replaced it with a tray icon.

#NoEnv
Menu Tray, Icon, imageRes.dll, 118
SetKeyDelay, -1
x := True        ; start off script with an assumed capital (true)
R := True        ; always capitalize after typing return
E := "{Return}"  ; list of endkeys to trigger capitalizing


Loop
{   Input, key, C I L1 V, %E%
    on_endkey := R ? InStr(ErrorLevel,"EndKey") : false
    
    ; when triggered and lower case:
    if( x && RegExMatch(key,"[a-z]") )
    {
        ; initialized, or there was a whitespace or return typed before this key
        if( !key_prev || key_prev = "EndKey" )
        {    
            Send {Backspace}+%key%
        
        }  ; or, if a number was not typed (don't interrupt typing float numbers)
        else if key is not integer
        {
            Send {Backspace}{Space}+%key%
        }
        x := False
    }
    else if( key != "" )  ; a non-EndKey
    {    
        if InStr(".?!", key)
            x := True  ; trigger set
        else if key is upper
            x := False  ; Don't capitalize after a capital!
        else if key is integer
            x := False  ; Don't capitalize after a number
        
        key_prev := key
    }
    else if on_endkey  ; Key is blank due to Endkey matching
    {
        x := True  ; trigger set
        key_prev := "EndKey"
    }
}


wyw
  • Members
  • 32 posts
  • Last active: Aug 01 2017 10:16 AM
  • Joined: 01 Jul 2015

Wow, this is 99.98% done! I only have one problem. I have a Autocorrect script running at the same time, and when I type "its", it gets automatically capitalized to "Its" but the Autocorrect script changes it back to "its".

 

And I tryed to add the enter key as well so it starts capitalizing the first letter once enter is pressed, but it doesn't work.

 

This are the only two things I need to fix. You don't know how grateful I would be if this gets fixed, for real.

 

 

 

 

 

 

I'm a noob.



flyingDman
  • Spam Officer
  • 2186 posts
  • Last active: Nov 07 2015 08:15 AM
  • Joined: 27 Feb 2009

If you manually capitalize, it types the capital, then a space then capitalize the following letter (T Est).


Marine Corps Gen. Joseph Dunford told senators at his Joint Chiefs of Staff confirmation hearing : “If you want to talk about a nation that could pose an existential threat to the United States, I'd have to point to Russia. And if you look at their behavior, it's nothing short of alarming.”


wyw
  • Members
  • 32 posts
  • Last active: Aug 01 2017 10:16 AM
  • Joined: 01 Jul 2015

I know. If I do  shift myself "Its" stays. If the script capitalizes the letter from alone, it goes back to "its" which is very annoying. That has to be solved.