Iphone keyboard
1 answer
The easiest way is to use TextField. Even your UI doesn't require one, you can set it to zero so it doesn't appear on the screen. Then you can access the keys pressed using the textbox delegate callback methods.
- (void)viewDidLoad {
[super viewDidLoad];
//CGRectZero because we don't want the textfield to be shown onscreen
UITextField *f = [[UITextField alloc] initWithFrame:CGRectZero];
//We set the delegate so we can grab keypressed
f.delegate = self;
[self.view addSubview:f];
[f becomeFirstResponder]; //Show the keyboard
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range
replacementString:(NSString *)string {
if (string.length >0) {
NSLog(@"%@ Pressed",string);
}
else {
NSLog(@"Backspcae pressed");
}
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
NSLog(@"return pressed");
return YES;
}
Note. To avoid a compiler warning, make sure that in your .h file the class explicitly says that it implements the UITextFieldDelegate prototype. i.e:
@interface MyViewController : UIViewController <UITextFieldDelegate>
+1
a source to share