 |
AutoHotkey Community Let's help each other out
|
| View previous topic :: View next topic |
| Author |
Message |
Laszlo
Joined: 14 Feb 2005 Posts: 4710 Location: Boulder, CO
|
Posted: Wed Jul 26, 2006 4:07 pm Post subject: Make sentences start with capitals |
|
|
If you finish a sentence with ".", "?" or "!" followed by white space or new line, we know that a new sentence follows, which should start with a capital (uppercase) letter. Here is a simple script, which fixes accidental lower case there, as it was discussed here.
It is a simple state machine, monitoring key presses. If ever a mouse key or special keyboard key is pressed, like {Up}, {RShift} or {F1} we get to the initial state, waiting for a safe sign of a sentence ending. If a sentence terminator is entered (.?!), we move to state 1 (forgetting everything beforehand). If a white space character is entered, it is ignored, unless we are in state 1, when we proceed to state 2. Letters (converted to upper case) are replaced in the text only if we are in state 2. At other than whitespace characters and terminators (like letters) we move back to the initial state, waiting for the end of another sentence.
| Code: | #SingleInstance force
#NoEnv
Process Priority,,High
SetBatchLines -1
Loop {
Input key, I L1 V,
( Join
{ScrollLock}{CapsLock}{NumLock}{Esc}{BS}{PrintScreen}{Pause}{LControl}{RControl}
{LAlt}{RAlt}{LShift}{RShift}{LWin}{RWin}{F1}{F2}{F3}{F4}{F5}{F6}{F7}{F8}{F9}{F10}{F11}{F12}
{Left}{Right}{Up}{Down}{Home}{End}{PgUp}{PgDn}{Del}{Ins}
)
If StrLen(ErrorLevel) > 3 ; NewInput, EndKey
state =
Else If InStr(".!?",key) ; Sentence end
state = 1
Else If InStr("`t `n",key) ; White space
state += (state = 1) ; state1 -> state2
Else {
StringUpper key, key
If state = 2 ; End-Space*-Letter
Send {BS}{%key%} ; Letter -> Upper case
state =
}
}
~LButton::
~RButton::
~MButton::
~WheelDown::
~WheelUp::
~XButton1::
~XButton2::State = |
|
|
| Back to top |
|
 |
SKAN
Joined: 26 Dec 2005 Posts: 8688
|
Posted: Wed Jul 26, 2006 4:28 pm Post subject: |
|
|
.
This is wondeful coding Laszlo.
I find this useful.
Oh my ...
Your code also replaces in the next line..
Absolute beauty.. It even replaces with a line in between..
Thanks a lot.
This code will be very useful to me.
Regards, ...
Ps: i never used shift to type this post.  _________________ URLGet - Internet Explorer based Downloader
Last edited by SKAN on Wed Jul 26, 2006 5:07 pm; edited 1 time in total |
|
| Back to top |
|
 |
Laszlo
Joined: 14 Feb 2005 Posts: 4710 Location: Boulder, CO
|
Posted: Wed Jul 26, 2006 4:45 pm Post subject: |
|
|
| The code demonstrates the power and simplicity of state machines. Try writing a parser for real numbers with optional sign, decimal point, signed or unsigned exponent. With state machines you might succeed with less than 20 lines of code. |
|
| Back to top |
|
 |
Rajat
Joined: 28 Mar 2004 Posts: 1687
|
Posted: Wed Jul 26, 2006 6:57 pm Post subject: |
|
|
I'm running it right now... And i think its really good and useful... Thanks for sharing it Laszlo! _________________
 |
|
| Back to top |
|
 |
polyethene
Joined: 11 Aug 2004 Posts: 5248 Location: UK
|
Posted: Wed Jul 26, 2006 7:13 pm Post subject: |
|
|
You can remove SetBatchLines and Priority and use SendInput instead of Send - it's faster and much less likely to cause system instability. _________________ GitHub • Scripts • IronAHK • Contact by email not private message. |
|
| Back to top |
|
 |
Laszlo
Joined: 14 Feb 2005 Posts: 4710 Location: Boulder, CO
|
Posted: Wed Jul 26, 2006 7:45 pm Post subject: |
|
|
SetBatchLines determines how fast the script runs, that is, how often it sleeps. Because the script has an always running loop, it must finish its job fast, otherwise it will miss keys at fast typing. In slower PC's SetBatchLines -1 looks necessary.
Similarly, "Process Priority,,High" ensures that when other tasks consume CPU cycles, this script still reacts to key presses quickly. You should even consider "Realtime", if you experience keys appearing slowly in the application.
Since Send only sends 2 keys in the beginning of sentences, the speed advantage of SendInput is not noticeable. On the other hand, some applications don't take SendInput keys (like my MultiEdit does not like SendInput ^v), so you have to use Send (= SendEvent) or even SendPlay. I found Send is the best tradeoff between compatibility and speed. If it is different in your PC, just change it. |
|
| Back to top |
|
 |
Kai Guest
|
Posted: Mon Sep 18, 2006 4:07 am Post subject: |
|
|
Just a suggestion...
The letter "I" between spaces to be in capital too? |
|
| Back to top |
|
 |
Kai Guest
|
Posted: Mon Sep 18, 2006 4:13 am Post subject: |
|
|
| oh anyway.. Mine didnt work properly >.< it wont start with capital unlses i have a "." in front >.< or ! etc.. |
|
| Back to top |
|
 |
Laszlo
Joined: 14 Feb 2005 Posts: 4710 Location: Boulder, CO
|
Posted: Mon Sep 18, 2006 4:38 am Post subject: |
|
|
| Kai wrote: | | The letter "I" between spaces to be in capital too? | Not for me! If anyone types program code, math formulae, algorithms, the capitalized variable i drives him crazy. It happens to be the imaginary unit and the most common index variable.
| Kai wrote: | | oh anyway.. Mine didnt work properly >.< it wont start with capital unless i have a "." in front >.< or ! etc.. | This was the purpose. "If you finish a sentence with ".", "?" or "!" followed by white space or new line, a new sentence follows". |
|
| Back to top |
|
 |
Soldier3570
Joined: 30 Aug 2006 Posts: 10
|
Posted: Mon Sep 18, 2006 7:18 am Post subject: |
|
|
im using it right now and i dont think it is working
im not sure whats wrong maybe its my computer  _________________ Soldier3570 |
|
| Back to top |
|
 |
Laszlo
Joined: 14 Feb 2005 Posts: 4710 Location: Boulder, CO
|
Posted: Mon Sep 18, 2006 3:56 pm Post subject: |
|
|
| In what application, Windows version, AHK version? What happens if you type ". a"? |
|
| Back to top |
|
 |
d-man
Joined: 08 Jun 2006 Posts: 285
|
Posted: Mon Sep 18, 2006 4:34 pm Post subject: |
|
|
Here's a version I use where APPsKEY turns it on or off, as sometimes it is annoying. This also changes apostrophe's and quotations.
| Code: | AppsKey::
onn:=!onn
SetTimer, Start, 250
return
Start:
Loop
{
Input key, I L1 M V,{Esc}{BS}{Left}{Right}{Up}{Down}{Home}{End}{PgUp}{PgDn}
StringUpper key, key
If InStr(ErrorLevel,"EndKey")
state =
Else If InStr(".!?",key)
state = 1
Else If InStr("`t `n",key)
{
If state = 1
state = 2
}
Else
{
If state = 2
Send {BS}{%key%}
state =
}
if !onn
{
SetTimer, Start, off
break
}
}
return
$'::
if onn = 1
send ’
else
send '
return
$"::
if onn = 1
{
Send, {ASC 0147}
Send, {ASC 0148}{left}
}
else
send "
return |
|
|
| Back to top |
|
 |
Laszlo
Joined: 14 Feb 2005 Posts: 4710 Location: Boulder, CO
|
Posted: Mon Sep 18, 2006 8:00 pm Post subject: |
|
|
Turning it on or off is a good idea! Here is a somewhat simpler version of your variant. (It does not break out at NewInput of the Input command, Ctrl-H, Alt-Tab, etc.) | Code: | Loop
{
if !onn {
Sleep 100
Continue
}
Input key, I L1 M V,{Esc}{BS}{Left}{Right}{Up}{Down}{Home}{End}{PgUp}{PgDn}
StringUpper key, key
If InStr(ErrorLevel,"EndKey")
state =
Else If InStr(".!?",key)
state = 1
Else If InStr("`t `n",key) {
If state = 1
state = 2
} Else {
If state = 2
Send {BS}{%key%}
state =
}
}
return
AppsKey::onn:=!onn |
|
|
| Back to top |
|
 |
Riley Guest
|
Posted: Fri Aug 10, 2007 11:02 pm Post subject: |
|
|
hi. Im just a beginner and im trying to learn and study your script.
how do i modify the script to capitalize the first letter of every word i type?
Thank you. |
|
| Back to top |
|
 |
Laszlo
Joined: 14 Feb 2005 Posts: 4710 Location: Boulder, CO
|
Posted: Fri Aug 10, 2007 11:40 pm Post subject: |
|
|
Something like this. | Code: | Loop {
if !onn {
Sleep 100
Continue
}
Input key, I L1 M V,{Esc}{BS}{Left}{Right}{Up}{Down}{Home}{End}{PgUp}{PgDn}
if !onn
Continue
StringUpper key, key
If InStr(ErrorLevel,"EndKey")
state =
Else If InStr(".,!? `t`n",key)
state = 1
Else {
If state = 1
Send {BS}{%key%}
state =
}
}
return
AppsKey::
onn:=!onn
state = 1
TrayTip,,% "Capitals correction is now O". (onn ? "N" : "FF")
Return | Don't forget, this is a very simple script: if you move around with the arrow keys, PgDn, End, etc., it cannot tell if at the new caret position a new word starts, or we are in the middle of a word. |
|
| Back to top |
|
 |
|
|
You can post new topics in this forum You can reply to topics in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|