NSTextView - Any way to prevent the "double space to enter period" MacOS feature?

Hi,


I'm working on a Code Editor based on NSTextView; There are many built-in "smart" features of NSTextView that are disable-able (automatic quote/dash/etc.), but I can't find a way to prevent the "double space to enter period" system-wide shortcut from affecting my text view.


Is a some way to make NSTextView ignore that? Otherwise, any tips for implementing a non-fragile workaround?


Thanks

Answered by Ken Thomases in 378866022

If you're willing to disable this throughout your app (rather than for just one class of text view), you can set the user default "NSAutomaticPeriodSubstitutionEnabled" to false somewhere early in startup. I discovered that key name by doing "defaults read -g", toggling that setting in System Preferences, doing "defaults read -g" again, and comparing the output from before and after.

Of course, you can change system preferences > Keyboard > Text and unckeck double space option.


But I understand you want to disable it programmatically, just for inside the app.

Just take care that user may be expecting this behavior if he/she has defined the preferences for it.


You could use

func shouldChangeText(inRanges affectedRanges: [NSValue], replacementStrings: [String]?) -> Bool

and forbid double space.

This is objC, but easy to write in Swift.

https://stackoverflow.com/questions/6854058/two-blank-space-in-uitextview-automatically-inserts-a-fullstop-after-text-in


May also have a look at

https://stackoverflow.com/questions/43679859/shouldchangetext-not-called-for-uitextfield-when-using-hardware-keyboard

Accepted Answer

If you're willing to disable this throughout your app (rather than for just one class of text view), you can set the user default "NSAutomaticPeriodSubstitutionEnabled" to false somewhere early in startup. I discovered that key name by doing "defaults read -g", toggling that setting in System Preferences, doing "defaults read -g" again, and comparing the output from before and after.

I learned something. That's the solution !

It once more shows how poor is Apple documentation.

Yas! Thanks Ken, that's exactly what I need 🙂


I'm still curious if there's a good solution for a specific NSTextView though, my previous hack was to override `keyDown` and `insertText` in the NSTextView, and manually catch/alter attempts to replace a space with ". " following two consecutive space keyDowns... I'm sure (hopeful?) there's a better way!

What is your spec ?

Just avoid double space becoming full stop + space ?

Do you need double space to be accepted ?

If Yes and no, then return false from shouldChangeText when double space should work.


optional func textView(_ textView: NSTextView, shouldChangeTextInRanges affectedRanges: [NSValue], replacementStrings: [String]?) -> Bool

Hey Claude, thanks for replying--


Unfortunately I do need to allow for double spaces (it's a code editor).


The workaround I mentioned above kinda works, as it can differentiate the magically-inserted-period from a similar user action to some extent (I'm sure it's broken in ways I haven't thought of).


At any rate, NSAutomaticPeriodSubstitutionEnabled is good enough for me at this point, I'm just curious whether there's a less intrusive solution.

NSTextView - Any way to prevent the "double space to enter period" MacOS feature?
 
 
Q