AutoHotkey Community

It is currently May 27th, 2012, 7:41 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 53 posts ]  Go to page 1, 2, 3, 4  Next
Author Message
PostPosted: July 26th, 2006, 5:07 pm 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
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 =


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 26th, 2006, 5:28 pm 
Online
User avatar

Joined: December 26th, 2005, 4:40 pm
Posts: 8776
.
This is wondeful coding Laszlo.
I find this useful.
Oh my :shock: ...
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, :D...

Ps: i never used shift to type this post. :D

_________________
URLGet - Internet Explorer based Downloader
StartEx - Portable Shortcut Link


Last edited by SKAN on July 26th, 2006, 6:07 pm, edited 1 time in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 26th, 2006, 5:45 pm 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
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.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 26th, 2006, 7:57 pm 
Offline

Joined: March 28th, 2004, 3:53 pm
Posts: 1870
I'm running it right now... And i think its really good and useful... Thanks for sharing it Laszlo!

_________________
Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 26th, 2006, 8:13 pm 
Offline
User avatar

Joined: August 11th, 2004, 1:47 am
Posts: 5347
Location: UK
You can remove SetBatchLines and Priority and use SendInput instead of Send - it's faster and much less likely to cause system instability.

_________________
GitHubScriptsIronAHK Contact by email not private message.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 26th, 2006, 8:45 pm 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
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.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 18th, 2006, 5:07 am 
Just a suggestion...

The letter "I" between spaces to be in capital too?


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: September 18th, 2006, 5:13 am 
oh anyway.. Mine didnt work properly >.< it wont start with capital unlses i have a "." in front >.< or ! etc..


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: September 18th, 2006, 5:38 am 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
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".


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 18th, 2006, 8:18 am 
Offline

Joined: August 30th, 2006, 8:06 am
Posts: 10
im using it right now and i dont think it is working

im not sure whats wrong maybe its my computer :oops:

_________________
Soldier3570


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 18th, 2006, 4:56 pm 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
In what application, Windows version, AHK version? What happens if you type ". a"?


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 18th, 2006, 5:34 pm 
Offline

Joined: June 8th, 2006, 2:41 am
Posts: 285
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


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 18th, 2006, 9:00 pm 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
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


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 11th, 2007, 12:02 am 
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.


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: August 11th, 2007, 12:40 am 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
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.


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 53 posts ]  Go to page 1, 2, 3, 4  Next

All times are UTC [ DST ]


Who is online

Users browsing this forum: nothing and 10 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group