0

I'm developing an ios app which may use external bluetooth wireless keyboard. I need to detect when the control keys are pressed down and when are up. I already know how to detect such control keys when the key is pressed by implementing function keyCommands as shown in iOS: How to detect the escape/control keys on a hardware bluetooth keyboard?

- (NSArray *) keyCommands {
    UIKeyCommand *esc = [UIKeyCommand keyCommandWithInput: UIKeyInputEscape modifierFlags: 0 action: @selector(esc:)];
    return [[NSArray alloc] initWithObjects: esc, nil];
}

- (void) esc: (UIKeyCommand *) keyCommand {
    // Your custom code goes here.
}

However, this does not differentiate when the key is actually pressed down and when the key are actually pressed up? My app needs to differentiate this.

Could anyone help with this? Thanks.

Community
  • 1
  • 1
Van Yu
  • 129
  • 1
  • 11

1 Answers1

0

iOS 9 added pressesBegan and pressesEnded.

Here is a sample in C#. Other languages work likewise.

public override void PressesBegan(NSSet<UIPress> presses, UIPressesEvent evt)
{
    var handled = false;

    foreach (UIPress press in presses)
    {
        if(press.Key is UIKey key)
        {
            //Debug.WriteLine($"PressesBegan: Characters='{key.Characters}', KeyCode={key.KeyCode}, ModifierFlags={key.ModifierFlags}");
            switch (key.KeyCode)
            {
                case UIKeyboardHidUsage.KeyboardLeftControl:
                    Debug.WriteLine("Left Control pressed");
                    handled = true;
                    break;
                case UIKeyboardHidUsage.KeyboardRightControl:
                    Debug.WriteLine("Right Control pressed");
                    handled = true;
                    break;
                case UIKeyboardHidUsage.KeyboardLeftAlt:
                    Debug.WriteLine("Left Alt pressed");
                    handled = true;
                    break;
                case UIKeyboardHidUsage.KeyboardRightAlt:
                    Debug.WriteLine("Right Alt pressed");
                    handled = true;
                    break;
                case UIKeyboardHidUsage.KeyboardLeftShift:
                    Debug.WriteLine("Left Shift pressed");
                    handled = true;
                    break;
                case UIKeyboardHidUsage.KeyboardRightShift:
                    Debug.WriteLine("Right Shift pressed");
                    handled = true;
                    break;
            }
        }
    }

    if(!handled)
        base.PressesBegan(presses, evt);
}

public override void PressesEnded(NSSet<UIPress> presses, UIPressesEvent evt)
{
    base.PressesEnded(presses, evt);
}
Michael Rumpler
  • 305
  • 2
  • 17
  • Whilst this may theoretically answer the question, [it would be preferable](//meta.stackexchange.com/q/8259) to include the essential parts of the answer here, and provide the link for reference. Please [edit] the answer with all relevant information. Make sure to use your own words, [answers comprised entirely of a quote (sourced or not) will often be deleted since they do not contain any original content](/help/referencing). – Adriaan Mar 15 '23 at 14:00