iOS Keyboard or Button button press and release time [duplicate] - iphone

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Detect key press on virtual keyboard
I wanted to know is it possible to capture the exact keyboard key press time and release time in an iOS application development?
For example, let's say I type "Hello". I'd like to know the H-Key pressed time and release time, then E, L etc. I think the answer will be in microseconds but that is what I want.
Any help would be really useful.

You can use -(BOOL) textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string (from UITextFieldDelegate) to intercept typed characters in UITextField and record the timestamps there.
This at least gives you the release times. Press times are probably much harder, particularly when you consider keys that you have to press longer to get to accented versions of the character.

You will need to make your own keyboard and add events for Touch Down and Touch Up Inside on the buttons where you want to have this information.
Once you have your own keyboard, it is simply a matter of storing the NSDate when the button is pressed, and then subtracting that from the NSDate when it is released in order to get the elapsed time. The time will much more likely be in milliseconds (rather than microseconds) since the run-loop doesn't even go that fast.
I have an open-source project (MIT license) on Github which creates a custom numeric keyboard (you will have to add your own events to the buttons) available which should make implementing a keyboard trivially easy, here: https://github.com/lnafziger/Numberpad
You can add more/different buttons if you want to use letters as well (no code changes should be needed) on this keyboard.

Related

Objective-C: Trigger method with computer keyboard stroke

Sorry if the questions is asked/answered already, or if my title is poorly worded.
I am currently writing an iPhone app, and have considered a useful tool for debugging. I would like to write a method that just prints variables and other info that I want. That part is simple and all, but I want this to be called by keystroke.
For now I have been just adding NSLog()'s to viewDidLoad or to other button methods to check if my variables are being set properly, but it's becoming tedious and the code is long so I tend to forget about some and spend a lot of time looking for them haha.
I just want one method that I can go to to write my NSLog()'s, and have that method call whenever I hit 'space' or something of that sort.
Can this be done?
Thanks!
-SF
It's pretty hard to do, you need to have a hidden text field that you keep in focus, hide the software keyboard for it, then listen on its delegate methods for changing text.
An alternative would trigger the code inside applicationWillResignActive: which gets called on application hiding, notification center showing, or double home press.

iPhone SDK Accessing what a user is typing

Is there a way to see what a user is typing in any application while on their iPhone, and then do something based on what they have typed. For example, if a user is typing a text message, is there a way to see what he's typing and hyperlink a word, or display a notification?
Let me know if you require further clarification.
EDIT:
The text monitoring will be happening on different applications; I want to be able to track all typed text, so if the user is typing a note or a text message, I want to be able to track that.
Many of the views associated with text entry have delegate methods, but it does not appear that they will send notifications at the granularity of every character. Of course, you could make your own view and do whatever you want. It's difficult to answer your question without code, but the answer appears to be "yes, but not easily".
Experiment with:
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
You'll need to implement UITextFieldDelegate & set the textField's delegate to whatever (e.g.) viewController you're using so you get the message when any change is made to the textField.
I'm assuming you mean within the app that you're coding, & not any other app which may be running - that would be impossible & probably not desirable!

iPhone/iPad keyboard shortcuts?

There's an action in my program which users need to perform very frequently (potentially hundreds of times per session), and I'd like to add a keyboard shortcut for users with a bluetooth keyboard. Is there any way to do this?
Right now, the closest I've come is triggering the action whenever the U+F8FF character is entered (Shift+Alt+k on an iPad/iPhone/Mac, which prints an apple logo character) in a text view. Of course, this would present an issue if the user actually wanted to input an apple symbol.
Are there any better ways to support keyboard shortcuts (hopefully Cmd+something, rather than Shift+Alt+something)? Thanks!
Under Cocoa for Mac OS X, you would gather modifier key information from the NSEvent passed to the keyDown: method in an NSResponder object. CocoaTouch replaces the NSResponder with UIResponder and NSEvent with UIEvent. The UI versions don't have documented keyboard event support. I'm guessing that Apple has extended UIResponder to handle keydown events, but hasn't publicly documented the changes yet. Unfortunately that means we'll just have to wait for that documentation to be able to read the modifier keys.
I think your current solution is a fine solution until Apple gives us a keyboard supporting UIResponder.

Detecting iPad keyboard hide versus external keyboard connect?

The iPad virtual keyboard will disappear in one of (at least) these 3 circumstances:
If the control (say, a UITextField) programmatically resigns first responder.
If the user taps the "dismiss keyboard" button in the lower right.
If the user connects to the USB/keyboard dock peripheral.
In all cases, I get the UIKeyboardWillHideNotification.
The problem is that the first two cases are generally equivalent-- in other words, the user is done editing the text field. But in the third case, the text field is still being edited, just from another input source.
The problem is detecting the difference between cases 2 and 3. All I get in both cases is UIKeyboardWillHideNotification. In case 2, I generally want to also lock the edit control and commit the value. In case 3, I generally want to do nothing and allow editing to continue.
But how can I tell the difference?
Apple's Pages app seems to be able to distinguish this on document-title renaming.
I would look at the UIKeyboardBoundsUserInfoKey passed with the notification. The physical keyboard probably has empty bounds.
It sounds like you're just trying to figure out when a user is done editing. You could listen for the UITextFieldTextDidEndEditingNotification notification for cases 1 and 2.
A much simpler solution would be couldn't you just check against the editing property of UITextField to determine if it's still supposed to be editing or not? I don't have a physical keyboard, so I have no way to test this. I'm just guessing.
Here's a link to the documentation on that property: http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UITextField_Class/Reference/UITextField.html#//apple_ref/occ/instp/UITextField/editing
I'm very curious to know if this works or not... :)

iPhone numberpad with decimal point

I am writing an iPhone application which requires the user to enter several values that may contain a decimal point (currency values, percentages etc.). The number of decimal places in the values varies. The problem is that the number pad does not contain a key for entering a decimal point.
When I lock my phone, the number pad that comes up to enter a passcode has a custom button to make an emergency call (as seen in the following screenshot):
Numberpad with custom button http://img25.imageshack.us/img25/6426/photoejg.jpg
Does anyone know how to create a number pad with a decimal point button or a custom button (like the emergency call button above)?
Thanks.
There's no Apple-approved way to edit the existing keyboard. If you want them to allow it, file a feature request.
That said, it just so happens that in most applications the keyboard (instance of UIKeyboard) is a separate UIWindow, and you can iterate over the windows in the application and start adding custom subviews that respond to the appropriate touch actions. Find it by iterating over [[UIApplication sharedApplication] windows] and checking to see if the description contains the string UIKeyboard. For more info on this method and some sample code, see this answer.
Another approach is to create your own custom view and build a keyboard from scratch. Be careful if you do this, though, as it requires a lot of manual work, not only in creating the keyboard and getting the touch behavior to match Apple's, but also in any control you add that would bring up the regular keyboard - you'll need to redirect things like becomeFirstResponder to show your own keyboard, rather than Apple's.
Edit: As ZaBlanc pointed out, newer versions of iOS have a way to do this with the inputView and inputAccessoryView properties. See the UIResponder class reference for details.
set UIKeyboardType to UIKeyboardTypeDecimalPad
Available in iOS 4.1 and later.
Create a UIViewController that contains a UIView with a bunch of buttons. Now your keyboard can have whatever you want on it.