What does the code do exactly?
Implements for TCustomEdit and TCustomCombo descendents a large sub-set of the AutoCorrect functionality had by ‘proper’ word processors. Specifically, the following is implemented:
User-defined ‘replace text as you type’ entries, including formatted ones if the active control is a rich edit.
TWo INitial CApitals correction, with an exception list that is automatically added to when the user immediately undoes a correction.
Conversion of ’straight’ quotes into ‘curly’ (‘smart’) quotes.
Conversion of hyphens to dashes: two hyphens in a row (–) turn into an em-dash, a hyphen with a space either side ( – ) becomes an en-dash with a space either side.
Conversion of 1/2 to ½, 1/4 to ¼, and 3/4 to ¾.
Auto-indentation for rich edit controls — in other words, allows the user to set the active paragraph’s left- and first-indent properties by tabbing and backspacing.
How does it work?
In essence, the code works by trapping the KeyPress method (OnKeyPress event) of the active control, testing for a trigger key that will lead to correction testing proper kicking in. The initial trapping, however, is ‘manual’ in the sense that the TAutoCorrectEngine component does not actively ‘seek out’ key press events — it needs to be told about them.
What’s the quickest way to see it in action?
Download the code from here (EDN registration required — it’s free and relatively easy to do though), open up the ‘AutoCorrect demo and design-time package’ project group, and run the last project listed (AutoCorrectDemo.dpr). Since the TAutoCorrectEngine component is created at runtime for the demo, no installation is required.
How do I use the code myself?
Firstly, compile and install the design-time package, dclCCRAutoCorrect.dpk. This should lead to five new components being added to the component palette: TAutoCorrectEngine, TComboBoxWithAutoCorrect, TEditWithAutoCorrect, TMemoWithAutoCorrect and TRichEditWithAutoCorrect. This done, add a TAutoCorrectEngine component to your form, and add some custom ‘replace-as-you-type’ entries to it by double clicking on it or by going to its CustomEntries property in the object inspector. Following that, drop one or more TxxxWithAutoCorrect controls onto the form, and assign their AutoCorrectEngine property to the TAutoCorrectEngine component. That’s it! To actually compile an application that uses one or more of the components, remember to make sure CCR.AutoCorrect.dcu and CCR.AutoCorrect.Consts.dcu end up somewhere in your library paths — the easiest way to do this is to add the component’s directory as a library path itself. In Delphi 2007, you do this by going to Tools|Options, Environment Options -> Delphi Options -> Library – Win32, and clicking on the ellipsis button for ‘Library path’.
But I don’t want to use your custom descendants of TEdit, TRichEdit, etc.!
No problem — you’ll just have to do a bit of manual work, calling as appropriate the KeyDownOccurred, KeyPressOccurred and UndoOccurred methods of the TAutoCorrectEngine component. The last of these is best done by defining an undo action (i.e., a TAction with Ctrl+Z as its shortcut), UndoOccurred being called immediately after the undo operation is performed. As for the other two, you can either handle the OnKeyXXX events of the controls themselves or (if you set KeyPreview to True) the parent form — in the second case, pass ActiveControl as the first parameter to KeyXXXOccurred. See the main demo for an example of all this — the process is simpler than it may sound.