Requirement
I'd like to detect not a single but a sequence of characters to trigger an action, e.g when the user types 'cat' into a NSView
subclass - but not for a 'c'.
Is there anything smarter other than maintaining something like a NSMutableArray
with the last 3 characters entered in my keyDown:
method, assembling strings from that array (via NSData
?) and checking that string?
Sounds a bit awkward..but I can't figure an easier way (or built in functionality in NSResponder
/NSView
)
I’d just keep an NSString of the last ’n’ characters. Append to the end in keyDown:, snip from the beginning when you want (when it grows larger than ’n’), check the string to see if it contains ‘cat’ every time you get new keys.
If you’re checking less than, like, a THOUSAND characters a string is a perfectly efficient way to do it.