Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 369
Phil Willoughby Avatar asked Oct 16 '10 08:10

Phil Willoughby


2 Answers

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.

like image 103
Bryan Avatar answered Jan 02 '23 13:01

Bryan


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.

like image 26
PeqNP Avatar answered Jan 02 '23 11:01

PeqNP