iOS keydown

Hi folks,

I really need to be able to capture keystrokes in an iOS screen.
There is no existing event for this, but I know it’s possible.

Can anyone help with the declares to do this?

So you’re thinking of needing to implement




Have you found any examples of how to do this in objective-c? That would go a long way toward making it easier to implement.

I have not. I’ll look.

This is how I do it in Android Studio.

This seems so straight forward and simple but there is no analogue in Xojo for iOS.
It’s the same as a KeyDown event in a Desktop app. I know when iOS started there wasn’t an anticipation of a physical keyboard so that event never made sense. It does now.

I suspect pressesBegan:withEvent is intended for something else - but I could be wrong for sure.

public boolean onKeyDown(int keyCode, KeyEvent event) {
   if (keyCode==<whatever code is needed>){
       // do something
       return true;
   return false;

What is that? Swift? Kotlin?

Java, from Android Studio. As a point of comparison.

What kind of control are you wanting this with?

It’s also worth noting that the method on iOS doesn’t let you intercept keystrokes as there’s no return value.

Ok, so this isn’t an optimal solution in that I’m adding the pressesBegan:withEvent: method to the parent class of the screen which means that going forward all screens will send keydown events, but if you use it sparingly i think it’ll be okay for what you are trying to do.

iOS KeyDown Handler

I ended up using a messaging pattern in that you apply an Interface (SharedKeyHandler.KeyHandlerTarget) to the screens that you want to receive key events (which in turn creates the KeyDown method for you) and then you register the Screen that you want to do this from with a call like this:


What I don’t know is whether that’ll capture the specific keys that you want as I don’t have a keyboard that works with my phone to try this with.

Note, I did not set up adding to things other than MobileScreens, but if you need that, you just need to add the IsA code to the top of the SharedKeyHandler.Register method where it gets the Handle.

If I add this to a MobileTextArea, how do I pass along keystrokes to the MobileTextArea?

The KeyDown method in a subclassed MobileTextArea works for letting me know if modifier keys are pressed, and what key is pressed with them, but there doesn’t seem to be a way to pass along keystrokes into the MobileTextArea.