3

I have implemented a complicated UITextInput that works quite well on iOS save for one missing feature:

  • when the user has a hardware keyboard connected (at least, in the simulator) and no text is selected, and presses Forward Delete, nothing happens.

Note that I am NOT referring to the key labeled Delete on a Mac, which is just a Backspace key -- that works correctly already, using an implementation of the UIKeyInput deleteBackward method.

Two questions:

  1. Is it possible for iOS to process Forward Delete correctly (for example a Bluetooth Mac hardware keyboard with Fn + Delete aka not Backspace, but Forward Delete). This works on my Mac laptop? (The answer seems like a yes because I just tested Forward Delete on a hardware keyboard (my host Mac dev machine) in the Simulator in Safari.)

  2. If it is possible and common in other apps, how do I intercept this key command? I cannot find what to send as the string to create a UIKeyCommand, nor can I find any API method that makes sense to do this in an easy fashion.

Note also that selecting text and pressing the Forward Delete key works properly already in this custom text editor, so it appears iOS knows about this key.

FYI this code is in C# but answers to part (2) in Swift or Objective-C would be welcome as well.

(My current best guess might be try messing with the code in this question ? -- Seems like a real headache just to get this one fairly common key?)

UPDATE:

Now pressing Delete (right-delete not backspace) somehow works on iOS, at least the simulator. It may have been a bug in the TextInput system since now when I press the right-Delete key, the system calls my code to select a single character to the right of my cursor and then calls my DeleteBackwards code to delete the selection (which code knows how to delete selected text). Strange but happy this is working as expected now, "out of the box".

Jared Updike
  • 7,165
  • 8
  • 46
  • 72
  • 1
    Other useful pointers might include: Which methods that I have already implemented in UITextInput might hit a breakpoint if I set one? I have tried a lot and not seen any breakpoints hit by the debugger which it hit the Forward Delete key. – Jared Updike Aug 24 '19 at 03:15
  • I can also confirm that the following code does not catch the Forward Delete, though it catches loads of keys (such as the letters, numbers, Backspace) with no modifiers: for(int i = 1; i <= 20000; i++) { AddKeyCommand(UIKeyCommand.Create((NSString)("" + Char.ConvertFromUtf32(i)), 0, new ObjCRuntime.Selector("ForwardDeleteAccelerator:"))); } // My method simply prints the keycode (integer) of the key that was pressed. So I think I can safely say UIKeyCommand will never work for this if I simply got the key code right. Maybe my modifiers are wrong (there is no enum for the Fn key...) ?... – Jared Updike Aug 26 '19 at 21:48
  • I can also confirm that the code is 127, because UIKeyCommand fires when I do Ctrl+127, or Cmd+127. Perhaps the system is capturing the Forward Delete and potentially forwarding it to me, but I just don't know where in my code to learn about when or if this is happening. – Jared Updike Sep 05 '19 at 01:30

1 Answers1

1

You can use Unicode code point 0x7f, for example in Swift:

UIKeyCommand(input: "\u{7f}", modifierFlags: [], action: #selector(forwardDelete))

Objective-C:

[UIKeyCommand keyCommandWithInput:[NSString stringWithFormat:@"%C", 0x7f] modifierFlags:0 action:@selector(forwardDelete)]
Nick
  • 3,958
  • 4
  • 32
  • 47
  • I tested this and my selector is not fired :-( and I believe this code is actually equivalent to one of my comments above, below the question, as I said I tried all the Unicode code points from 0 to 20,000 and 127 specifically which == 0x7f). See the Update on my question. – Jared Updike May 29 '21 at 17:54
  • I have tested this, it does work. Here is an example project: https://mega.nz/file/KQpEwJCK#HKm8_m00SYJK9GkPKPRNjPiZJaTA-dqK8NpdinMnBX8 – Nick May 30 '21 at 18:20
  • I DLed and compiled and your test project does work, but the project is ignoring a very important part of the context of my question, which is the custom UITextInput control part. Interfacing with the system in this way is very complicated. See: https://developer.apple.com/documentation/uikit/uitextinput – Jared Updike May 31 '21 at 20:31
  • 1
    Yes I can confirm other key commands do work in a text field but not forward delete. I'm not sure there is a solution to that, sorry. Best bet would be to open a TSI with Apple – Nick Jun 01 '21 at 12:03