iphone keyboard keydown/keyup events? - iphone

I am wondering why I am not able to find any help regarding the keyboard keyup/keydown events, I actually want to detect which key is been pressed by the user via keyboard on iphone while inputting the text in the UITextField.

You can use this delegate method to get the character that's being entered into a UITextField:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string;
For example:
if ([string isEqualToString:#"z"]){
// z is being typed.
}
Just make sure you've said your UITextField's delegate to the class file where you implement this.

Related

giving custom autosuggestion for uitextfield iphone

I have to implement a custom autosuggestion for UITextField>
I have an array of words and when I start typing in a textfield then all words similar to the typed text should appear as auto suggestion, like normal auto suggestion in ios but more than one word. And When I tap on one of them it should trigger an event. I know how to done this with a tableview anybody know a method to done this with ios's look and feel?
For this purpose I used the project linked below:
http://cocoacontrols.com/platforms/ios/controls/autocompletiontableview
A sexy look for a autocomplete:
UITextField + UITextFieldDelegate
http://www.cocoacontrols.com/search?utf8=%E2%9C%93&q=wepopover
Using UITableView you keep the data and apply an predicate on:
(BOOL) textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
I didn't attach source code because isn't general and might confuse you.
Make an array of NSString, and search the typed string in UITextfield delegate method textChanged:, then print strings in a tableView.
In fact this will work like a UISearchBar.

LostFocus Event of Textfield in xcode

I am creating an application in which i want to add record in the database after focus lost event of the text field.
Which is the lost focus event of textfield in xcode??
The method you need is - (void)textFieldDidEndEditing:(UITextField *)textField.
Check out the UITextFieldDelegate reference for more info >
you can use - (BOOL)textFieldShouldReturn:(UITextField *)theTextField delegate method of UITextField
Also you should consider using event textFieldDidEndEditing:
Hope this helps.

Block keyboard for input

How can I block keyboard for input (something like userInteractionEnabled = NO) in iOS >= 4 ?
Keyboard is used for TextEdit.
If I disable interaction for textedit then keyboard disappears, but I want to see the keyboard.
have you tried this delegate?
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
Le Coeur, I think you need to consider a different approach to whatever you're trying to achieve by restricting the keyboard input for two good reasons:
A UIElement that engages the user to type but doesn't react to any of the user's gestures is sure to be rejected by Apple.
Because this keyboard is on the OS level, it is private API and I am pretty sure there is nothing you can do to the keyboard to block access to the keys.
Hope this helps
You can always disable userInteractionEnabled on the keyboard view, or less-dangerously add an invisible subview to the keyboard that blocks interaction. See iOS: How to access the `UIKeyboard`? on how to get the UIView for the keyboard.

Detect key press on virtual keyboard

I am working on a small children's game for my son that involves timing on the virtual keyboard.
Is possible to detect when a key on the virtual keyboard is pressed? I.e. get a touch notification (touchesBegan)? I do not need to know which key is pressed, just if and when the press action started.
For TextView you can use...
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
- (void)textViewDidChange:(UITextView *)textView
For TextField you can use...
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
See UITextViewDelegate and UITextFieldDelegate on Apple website.
It's not possible to get the touch events directly from the virtual keyboard. However, you could use a UITextField that you place offscreen, so that it's not visible and call becomeFirstResponder to show the keyboard. Then, as Khomsan suggested, you could implement the delegate method textView:shouldChangeTextInRange: to be notified whenever a key is pressed.
A cleaner possibility would be to write a custom UIControl that implements the UIKeyInput protocol. You would only need to provide implementations for insertText:, deleteBackward and hasText (for this one, you can simply always return YES). To make the keyboard visible, you would again have to call becomeFirstResponder for your custom control.
Both of these methods have in common that you only will be notified when the key is released (so that text is entered), not when the touch actually begins. As I said, it's not possible to get the tochesBegan event directly. If you need that, you would probably have to implement your own onscreen keyboard.
Yes you can just use:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
//do what you need
[super touchesBegan:touches withEvent:event];
}

Trap backspace key on the iPhone

On the iPhone, I've tried to find a way to trap and act upon the backspace key pressed event. But cannot find a way to do that!! There sure must be one such way documented in the SDK right?
/John
For your UITextField which is taking the input, set your controller object to be the text field's delegate and implement the
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
delegate method in your controller.
This method should be called for every editing event (keypress). I believe that you should be able to look for a blank replacement string to tell that characters are being removed by the backspace key.