[NSEvent charactersByApplyingModifiers:] not matching NSEvent.characters/charactersIgnoringModifiers

To my surprise [NSEvent charactersByApplyingModifiers:] does not always produce the same NSString result as the originating NSEvent reflects through characters/charactersIgnoringModifiers, even if passing on the event's own modifierFlags.

For example, when pressing the 'end' key, both characters and charactersIgnoringModifiers return 0x000000000000f72b, which matches the value of NSEndFunctionKey. But calling [NSEvent charactersByApplyingModifiers:] passing in NSEvent.modifierFlags produces 0x0000000000000004, which matches the value of kEndCharCode.

(lldb) po [[((NSApplication*)NSApp).currentEvent characters] characterAtIndex:0]
0x000000000000f72b
(lldb) po [[((NSApplication*)NSApp).currentEvent charactersIgnoringModifiers] characterAtIndex:0]
0x000000000000f72b
(lldb) po [[((NSApplication*)NSApp).currentEvent charactersByApplyingModifiers:[(NSEvent*)((NSApplication*)NSApp).currentEvent modifierFlags]] characterAtIndex:0]
0x0000000000000004

(The same can be observed with UCKeyTranslate directly, which charactersByApplyingModifiers seems to use internally).

I would expect [NSApp.currentEvent charactersByApplyingModifiers:NSApp.currentEvent.modifierFlags] to match either NSApp.currentEvent.characters or NSApp.currentEvent.charactersIgnoringModifiers.

Is there a logic behind this perceived discrepancy that I'm missing? Should I always prefer the content of NSEvent.characters/charactersIgnoringModifiers in this case if the modifier flags I'm applying match those of the event itself?

Thanks for any insights!

(Also reported as FB20591338)

Thank you for your post and for filing a bug. After reviewing it, I believe it would be helpful if you could provide a focused sample project that outputs the difference between the methods to demonstrate the issue promptly. Unfortunately, I did not find any focused project attached.

If so, please share a link to your test project. That'll help us better understand what's going on. If you're not familiar with preparing a test project, take a look at Creating a test project.

Thanks again for all your help!

Albert Pascual
  Worldwide Developer Relations.

Thanks Albert! I've posted a minimal test case here https://gist.github.com/torarnv/88660ddc67652c588eb57dfdc9259046 (also attached to FB).

Pressing most of the typical function keys shows a difference in what charactersByApplyingModifiers reports compared to what the NSEvent comes with "built in":


❯ ./nsevent-charactersByApplyingModifiers.mm

chars: '' [U+F729] | ignoring: '' [U+F729] | applying: '' [U+0001] ⚠️ // Home

chars: '' [U+F72B] | ignoring: '' [U+F72B] | applying: '' [U+0004] ⚠️ // End

chars: ' ' [U+007F] | ignoring: ' ' [U+007F] | applying: ' [U+0008] ⚠️ // Delete

chars: '' [U+F728] | ignoring: '' [U+F728] | applying: ' ' [U+007F] ⚠️ // Fwd-delete

chars: '' [U+F704] | ignoring: '' [U+F704] | applying: '' [U+0010] ⚠️ // F1

chars: '' [U+F705] | ignoring: '' [U+F705] | applying: '' [U+0010] ⚠️ // F2

Thank you for providing all that, I saw you attached an Objective-C file in the FB, I didn't see a project.

You can see the status of your feedback in Feedback Assistant. There, you can track if the report is still being investigated, has a potential identifiable fix, or has been resolved in another way. The status appears beside the label "Resolution." We're unable to share any updates on specific reports on the forums.

For more details on when you'll see updates to your report, please see What to expect after submission.

Albert Pascual
  Worldwide Developer Relations.

[NSEvent charactersByApplyingModifiers:] not matching NSEvent.characters/charactersIgnoringModifiers
 
 
Q