how to handle key events in iphone
Asked Answered
B

2

4

Hi I am working on an iphone application and want to handle keyboard events in iphone. In Mac, there is a class NSEvent which handles both keyboard and mouse events, and in ios (iphone/ipad) the counterpart of NSEvent is UIEvent which handles only touch events. I know ios API does not provide this functionality, but how can i handle key events in iphone??? Any good tutorial or sth, to get started...

Blaise answered 7/3, 2011 at 5:40 Comment(0)
O
7

You cant directly code for keyboad;s key and there is no mouse in case of device.

you can make your logics for different kind of charectersets or you can make your logics in textField delgate methods or Textview Delegates method

textView delegate

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text

textField delegate

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string

You can also use Notification for textField and Textview.

For TextField use this

call this register method in viewDidLoad

-(void)registerForTextFieldNotifications {

    NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];

    [notificationCenter addObserver:self
                           selector:@selector (handle_TextFieldTextChanged:)
                               name:UITextFieldTextDidChangeNotification
                             object:self.textField];

}


- (void) handle_TextFieldTextChanged:(id)notification {



    if([iSinAppObj.passCodeString isEqualToString:lockTextField.text])
    {   
        //code here
    }

}

and for text view you need to change only event name like this

[notificationCenter addObserver:self
                               selector:@selector (handle_TextFieldTextChanged:)
                                   name:UITextViewTextDidChangeNotification
                                 object:self.textField];
Outshoot answered 7/3, 2011 at 6:32 Comment(0)
O
2

You can use notifications to handle events as explained here: http://developer.apple.com/library/ios/#documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/KeyboardManagement/KeyboardManagement.html

But the functionality is very limited.

Osborn answered 7/3, 2011 at 5:49 Comment(3)
Thank u so much... but this link does not seem to provide me complete functionality... I want to handle every key as typed by user (on iphone), and send that to another machine over the local networkBlaise
May be use a hidden textfield and capture the inputs through delegates?Osborn
yeah i thought of the same solution... So let me try it Thank u so much... CheersBlaise

© 2022 - 2024 — McMap. All rights reserved.