1

I use a bluetooth keyboard to connect into my app in iPad. I use it to type on UITextView or UITextField. And now I need to detect if user tap Command + Z button from that bluetooth keyboard. Is that any event of notification to get detect this? I have read this NSEvent addGlobalMonitorForEventsMatchingMask: Hotkey Intercepting it use NSEvent , but I dont think I can use it in iPad app. Can somebody point me what i have to do?

Thank you :)

Community
  • 1
  • 1
R. Dewi
  • 4,141
  • 9
  • 41
  • 66

1 Answers1

1

excellent question - i was having this huge problem in my recent project. There are two methods which will respond differently when you use a bluetooth keyboard and inbuilt keyboard - uikeyboardwillshownotification and uikeybaordwillhidenotification. Keep some nslogs inside these methods and see the response. The other way is to see the framesize of keyboard when your textfield begins editing - textfielddidbeginediting - try seeing the frame of the keyboard - bluetooth will have height zero - using that you can sort out your issue

Ashwin G
  • 1,570
  • 1
  • 16
  • 23
  • if you have to just realize tapping of cmd + z key - then there is a textfield delegate method - textField:shouldChangeCharactersInRange:replacementString: - here there is a string arguement - all the current charaters you type is this string - so you can do caseinsensitivecompare - with respect to this string and any character - z or cmd key and put your validations correspondingly – Ashwin G Sep 22 '12 at 05:19
  • thank you for your answer, can you give me a sample code to do caseinsensitivecompare? – R. Dewi Sep 24 '12 at 00:00
  • This is the code where i compare a annotation title with current location - ([annotation.title caseInsensitiveCompare:@"Current Location"] == NSOrderedSame) – Ashwin G Sep 24 '12 at 02:45
  • This is the delegate method for single character checking. Here i dont allow user to enter "#". Similarly you can put in CMD or z. For alphabets - caseinsensitivecompare: -(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{ if([string isEqualToString:@"#"] ) { return NO; } else return YES; } – Ashwin G Sep 24 '12 at 02:49
  • hi @Ashwin i have try your suggestion, but i use UITextview, so i try it in uitextviewdelegate..when i press CMD + z, it never called -(BOOL)textView:(UITextView *)textView shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{ but it always called -(void)textViewDidChange:(UITextView *)textView, can you give me another suggestion? – R. Dewi Sep 26 '12 at 09:59
  • Make sure you have added uitextviewdelegate and set it as well. And the delegate method is - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText: (NSString *)text – Ashwin G Sep 26 '12 at 10:35
  • yes i have set it right, but i have typo on my last comment :) – R. Dewi Sep 26 '12 at 11:44
  • So u mean you have set the delgate and used the specified delegate method and still have the issue? If so upload the code of the delegate method – Ashwin G Sep 26 '12 at 12:31