At a WWDC24 consultation with the keyboards and input team, I was referred to the UITextInputContext
class for detecting hardware keyboard status. However, it's not obvious how this class is intended to be used.
I would like to receive notifications when the UITextInputContext
state changes, however, I see no way to accomplish this. I've tried using KVO to observe state changes of the current
property of UITextInputContext.class
or the hardwareKeyboardInputExpected
property of UITextInputContext.current
, but receive no change notifications when a USB hardware keyboard is attached/detached, and hit an assertion when trying to stop observing, indicating that the observer is not registered. So I don't think KVO works.
I am interested in knowing whether a hardware keyboard is attached when the virtual onscreen keyboard is hidden or shown (which occurs when the hardware keyboard is attached/detached), so that I may adjust the state of my inputAccessoryView
accordingly (it should be hidden when a hardware keyboard is attached).
I've had some success checking the state of UITextInputContext.current.hardwareKeyboardInputExpected
during keyboard willHide/willShow notifications (or in a block executed via dispatch_async()
), however, it seems that the property value does not change until the first key press after attaching a hardware keyboard, or after the first virtual onscreen key press after detaching a hardware keyboard. The latter is problematic because there are no more keyboard hide/show notifications in which to perform the needed update to my inputAccessoryView
UI after the UITextInputContext
state change occurs following hardware keyboard disconnection.
Perhaps it's meant that I should monitor key press events and check the status there instead?