Within an iOS app how do I detect that the user has pressed one of the arrow keys on their external keyboard?
I have a couple of iOS applications which present inherently linearly organised information, allowing the user to move forward and backward through the data with on-screen controls. I would like to allow users with the keyboard dock or a paired 开发者_运维技巧bluetooth keyboard to use the arrow keys to move back and forth as well. Since the apps are full-screen it is very important to the user experience that there is no risk of displaying an on-screen keyboard as a side-effect.
I cannot find an API that will let me do this in Apple's documentation; but I cannot be sure that I have used the right search terms.
What APIs are there which will allow me to achieve this?
Update: I have raised this as an enhancement on Apple's bug reporting site.
The answer in How can I respond to external keyboard arrow keys? - create a hidden text field and react to selection changes - worked for me.
You can easily determine which arrow keys are pressed by referring to UIKeyInput
and UIKeyInputDelegate
. The method you're looking for specifically is positionFromPosition:inDirection:offset:
.
After that method is called (it is used by iOS to determine the selection range) it will then call setSelectedText:
. Put NSLog
statements in the positionFromPosition:inDirection:offset:
to determine how it works (you're responsible for the plumbing).
Also look at the SimpleTextInput. This will provide you with a template on subclassing UITextInput
.
You could eliminate the keyboard entirely by just adding support for a left/right swipe or drag gesture so the user can essentially treat the app like a photo gallery. No onscreen controls are needed-- it's a device-native navigation paradigm, easy to explain on an app startup splash screen.
If an external keyboard is required in order to support other app data-gathering functionality however, users will experience the "gorilla arm" problem...
Not actually knowing, I suspect the answer is "you can't, not in an official way".
Since a user-friendly UI is top priority for the iPad platform according to Apple, I think they are doing what they can to discourage behavior that would depend on a keyboard being available. Apps should behave the same and provide the same functionality with or without a hardware keyboard. Things like keyboard hotkeys or navigation that doesn't use on-screen elements are probably no-no:s for that reason.
But, like I said, this is just speculation.
精彩评论