Stop period after double space - Declare

Hi Everyone,

Does anyone have the Declare for stopping a period after a double space? I’ve been able to figure out most of the others with help from the forum, but the double space one is eluding me…

This is what I’m using currently for the rest:

#If TargetMacOS Then
  
  //text Area Pointer
  Var myTextArea As Ptr
  
  //Create our functions
  Declare Function NSClassFromString Lib "AppKit" (aClassName As CFStringRef) As Ptr
  Declare Function documentView Lib "AppKit" Selector "documentView" (obj_id As Ptr) As Ptr
  Declare Sub setAutomaticQuoteSubstitutionEnabled Lib "AppKit" Selector "setAutomaticQuoteSubstitutionEnabled:" (Id As Ptr, value As Boolean)
  Declare sub setAutomaticDashSubstitutionEnabled lib "AppKit" selector "setAutomaticDashSubstitutionEnabled:" (Id as ptr, value as Boolean)
  Declare sub setAutomaticTextReplacementEnabled lib "AppKit" selector "setAutomaticTextReplacementEnabled:" (Id as ptr, value as Boolean)
  
  //Set the text area options
  myTextArea = documentView(taSourceCode.Handle)
  setAutomaticQuoteSubstitutionEnabled (myTextArea, false)
  setAutomaticDashSubstitutionEnabled (myTextArea, false)
  setAutomaticTextReplacementEnabled (myTextArea, false)
  
#EndIf

TC

Isn’t there a preference to avoid that (macOS) ?

The TextArea property to change its setting is Allow Text Spelling

image

Unless you need it.

Hi Emile,

I already have Allow Spell Checking turned off on my DesktopTextArea, and yes there is a MacOS system preference to turn off Period after double spaces, but I don’t want every person who uses my program to have to make changes to a system wide preference.

I’ve turned off all the other auto changes in code as I’ve shown in the OP. Just getting the last working in code is eluding me, I’ve even found the property in the Apple AppKit documentation but I can’t quite get it right… :slight_smile:

TC

Sorry.

1 Like

All good… TC

I believe the one you’re looking for is

https://developer.apple.com/documentation/appkit/nstextview/2544655-automatictextcompletionenabled?language=objc

Thanks Greg,

Unfortunately I’ve tried that one but I still get the period after a double space. The only information I’ve found in the AppKit reference relates to a read only property in the spell checker.

https://developer.apple.com/documentation/appkit/nsspellchecker/2869584-automaticperiodsubstitutionenabl?language=objc

TC

I added

Declare Sub setenabledTextCheckingTypes Lib "AppKit" Selector "setEnabledTextCheckingTypes:" (ID As Ptr, value As UInt64)
setenabledTextCheckingTypes(myTextArea, 0)

and do not see any periods added anymore if the TextArea has Spellchecking set off in the IDE.

Hi Ulrich,

Thanks. Unfortunately if I type something then two spaces I still get a period added. I’ve confirmed that spell checking is off on my DeskTopTextArea and Add Period option is on in the System Preferences.

Period addition is not listed as a Text Checking or substitution type:

https://developer.apple.com/documentation/appkit/nstextview/1449529-enabledtextcheckingtypes?language=objc

TC

That’s weird. Maybe differences in localisation? It definitely does not add periods anymore on my German system, and it does without this declare.
Could be you’ll have to work with a custom textview that enables some of the replacement “events” – read “Methods to override” in Apple’s docs.

There is another option, in TextChange:

me.text = me.text.replace("  .", "  ")//replace two spaces followed with period, by two spaces.

Thanks Michel,

When the period addition happens the two spaces are replaced with the period, so "Hello " would become “Hello.” You can’t really pick where the change will have happened if you need period characters elsewhere.

TC

This is a setting on your phone/laptop and needs to be changed from settings and not with a declare I don’t think. For example on my iPhone, under General → Keyboards there is a setting called ‘“.” Shortcut’ which can be changed to disable this. A user would be confused if you took this away from them for just your app

Hi Jason,

Thanks, I’m aware of the system preference, which I’ve disabled for the moment. As my APP is a code editor I think that the user would be more confused with periods being randomly added where not required.

I agree if my editor was for editing normal text then yes the user can make that change but as a code editor requiring specific formatting then it would be best to turn it off.

The declare would only affect my app only. No other running app would be effected…

Regards

TC

This should work:

KeyDown event handler of the TextArea.

Function KeyDown(key As String) Handles KeyDown as Boolean
  if key = Chr(32) then
    var sel_start as integer = me.SelectionStart
    var lpart as string  = me.Text.Left(sel_start)
    var rpart as string  = me.Text.Middle(sel_start + me.SelectionLength)
    
    me.Text = lpart + Chr(32) + rpart
    me.SelectionStart = sel_start + 1
    
    return true
    
  else
    return false
  end
  
End Function