1

Is there a way to simulate a press to the "return" key programmatically, e.g. call the function(s) that are called by UITextField/UIKeyboard/whatever when the user presses the return key?

I know that somewhere inside the routine textFieldShouldReturn is called, but I assume that's not the only thing happening? I briefly stepped through the debugger and it seems that a press on the "return" key calls far more than one function.

What I'm looking for is a way to have a UITextField subclass, and in this subclass, I can call something like [self.inputview returnKeyPressed], and this function will do exactly the same as would have happened had the user pressed on the return key.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
JiaYow
  • 5,207
  • 3
  • 32
  • 36
  • 1
    What functionality are you trying to achieve though? Just dismissing the keyboard, or programmatically inserting a newline character? – Mick MacCallum Jun 12 '14 at 13:24
  • Actually I have a button as a right-side-view of the textfield and users should be also able to click on that button to trigger the save/search/whatever, in addition to having the option of pressing the return key. Just to clarify, I'm talking about a subclass of UITextField that can be transparently used by any other control. At the moment I just call `[self.delegate textFieldShouldReturn:self]` on button tap, but I'm a little worried about other functions that I need to call that might be important. – JiaYow Jun 12 '14 at 13:26

4 Answers4

4

(For any programmers looking to achieve this in the future)

You can achieve this by adding this in whatever function you wish to "simulate" your enter key press.

    [self textFieldShouldReturn:yourTextFieldName];

You will need to set your textfield delegate like such in your viewdidload:

[youtTextFieldName setDelegate:self];
C-H-E-F
  • 165
  • 11
2

I think what I would do is implement the textFieldDidEndEditing: UITextFieldDelegate method, and implement everything that needs to happen when the text field ends editing (saving etc.) there. Then all you have to do for your button is call a method that implements code to tell the text field to resign first responder.

Do this, and you'll have one method that handles both methods ways that your text field should end editing.

Mick MacCallum
  • 129,200
  • 40
  • 280
  • 281
  • Unfortunately, `textFieldDidEndEditing` is pretty unreliable for me, because for example saving should not occur if the user only changes the input focus. `textFieldShouldReturn` seems to be the correct place to put the custom code, and I'd rather call this method (in addition to whatever else I need to call to simulate a fully-fledged return press?) – JiaYow Jun 12 '14 at 14:23
  • @JiaYow Well either way you would probably want to add a check to see if the content changed, so it probably wouldn't matter which direction you went with. And you shouldn't be calling any of these methods. Left the text field call them as needed. – Mick MacCallum Jun 12 '14 at 15:58
2

As per: This Previous Question

ProcessSerialNumber psn;
GetFrontProcess( &psn );

// make some key events
CGEventRef keyup, keydown;
keydown = CGEventCreateKeyboardEvent (NULL, (CGKeyCode)76, true);
keyup = CGEventCreateKeyboardEvent (NULL, (CGKeyCode)76, false);

// forward them to the frontmost app
CGEventPostToPSN (&psn, keydown); 
CGEventPostToPSN (&psn, keyup); 

// and finally behave friendly
CFRelease(keydown);
CFRelease(keyup);

You can find list of the hex char codes in: /System/Library/Frameworks/Carbon.framework/Versions/A/Frameworks/HIToolbox.framework/Versions/A/Headers/Events.h

Community
  • 1
  • 1
Adrian Sluyters
  • 2,186
  • 1
  • 16
  • 21
  • `CGEventCreateKeyboardEvent` and related APIs are available only for macOS. `UITextField` is an iOS class, so those APIs are unavailable, unfortunately. – BinaryNate May 17 '17 at 04:58
  • You can replace `CGEventPostToPSN` with `CGEventPost`, and replace the first parameter with `0`, so that you won't need to find the serial number of the front process, as in https://stackoverflow.com/a/24074595/6557621. – MCCCS Feb 16 '19 at 15:54
0

This is a very old topic but this scenario is still relevant, so i'm answering it

If, and i stress your attention on the big IF, your need is for the logic behind pressing the return button to be invoked, then i think it is pretty straight forward assuming you have hooked your textfield's delegate somewhere.

All you need, is to call the method yourself on the textfield's delegate

[textfield.delegate textFieldShouldReturn];
TheFuquan
  • 1,737
  • 16
  • 18