Add text input to the iOS app
Don't have our View class implement the UIKeyInput protocol any more. It won't work properly anyway. The docs say: "Only a small subset of the available keyboards and languages are available to classes that adopt this protocol". Instead, use a transparent UITextView on top of our View to accept keyboard input. Seems to work as expected. Change-Id: I3093ea7fbfa0ecab0dc5d0a38e5695723e8ed4ad
This commit is contained in:
parent
6b89688829
commit
e3e040f671
@ -10,12 +10,13 @@
|
|||||||
|
|
||||||
#import "View.h"
|
#import "View.h"
|
||||||
|
|
||||||
@interface AppDelegate : UIResponder <UIApplicationDelegate>
|
@interface AppDelegate : UIResponder <UIApplicationDelegate, UITextViewDelegate>
|
||||||
|
|
||||||
@property (strong, nonatomic) UIWindow *window;
|
@property (strong, nonatomic) UIWindow *window;
|
||||||
@property (strong, nonatomic) View *view;
|
@property (strong, nonatomic) View *view;
|
||||||
|
|
||||||
- (void) threadMainMethod: (id) argument;
|
- (void)threadMainMethod: (id) argument;
|
||||||
|
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text;
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|
||||||
|
@ -16,7 +16,7 @@
|
|||||||
|
|
||||||
#import "lo.h"
|
#import "lo.h"
|
||||||
|
|
||||||
static UIView *theView;
|
static View *theView;
|
||||||
|
|
||||||
@implementation AppDelegate
|
@implementation AppDelegate
|
||||||
|
|
||||||
@ -41,6 +41,12 @@ static UIView *theView;
|
|||||||
vc.view = self.view;
|
vc.view = self.view;
|
||||||
theView = self.view;
|
theView = self.view;
|
||||||
|
|
||||||
|
self.view->textView = [[UITextView alloc] initWithFrame: r];
|
||||||
|
self.view->textView.autocapitalizationType = UITextAutocapitalizationTypeNone;
|
||||||
|
self.view->textView.alpha = 0;
|
||||||
|
[self.view addSubview: self.view->textView];
|
||||||
|
self.view->textView.delegate = self;
|
||||||
|
|
||||||
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self.view action:@selector(tapGesture:)];
|
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self.view action:@selector(tapGesture:)];
|
||||||
|
|
||||||
[self.window addGestureRecognizer: tapRecognizer];
|
[self.window addGestureRecognizer: tapRecognizer];
|
||||||
@ -68,6 +74,17 @@ static UIView *theView;
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
|
||||||
|
{
|
||||||
|
NSLog(@"textView: %@ shouldChangeTextInRange:[%u,%u] replacementText:%@", textView, range.location, range.length, text);
|
||||||
|
assert(textView == theView->textView);
|
||||||
|
|
||||||
|
for (NSUInteger i = 0; i < [text length]; i++)
|
||||||
|
lo_keyboard_input([text characterAtIndex: i]);
|
||||||
|
|
||||||
|
return NO;
|
||||||
|
}
|
||||||
|
|
||||||
- (void)applicationWillResignActive:(UIApplication *)application
|
- (void)applicationWillResignActive:(UIApplication *)application
|
||||||
{
|
{
|
||||||
(void) application;
|
(void) application;
|
||||||
@ -101,16 +118,17 @@ static UIView *theView;
|
|||||||
|
|
||||||
[[info objectForKey:UIKeyboardFrameBeginUserInfoKey] getValue:&frameBegin];
|
[[info objectForKey:UIKeyboardFrameBeginUserInfoKey] getValue:&frameBegin];
|
||||||
[[info objectForKey:UIKeyboardFrameEndUserInfoKey] getValue:&frameEnd];
|
[[info objectForKey:UIKeyboardFrameEndUserInfoKey] getValue:&frameEnd];
|
||||||
|
|
||||||
|
NSLog(@"keyboardWillShow: frame:%dx%d@(%d,%d)",
|
||||||
|
(int) frameEnd.size.width, (int) frameEnd.size.height,
|
||||||
|
(int) frameEnd.origin.x, (int) frameEnd.origin.y);
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)keyboardDidHide:(NSNotification *)note
|
- (void)keyboardDidHide:(NSNotification *)note
|
||||||
{
|
{
|
||||||
NSDictionary *info = [note userInfo];
|
(void) note;
|
||||||
CGRect frameBegin;
|
|
||||||
CGRect frameEnd;
|
|
||||||
|
|
||||||
[[info objectForKey:UIKeyboardFrameBeginUserInfoKey] getValue:&frameBegin];
|
NSLog(@"keyboardDidHide");
|
||||||
[[info objectForKey:UIKeyboardFrameEndUserInfoKey] getValue:&frameEnd];
|
|
||||||
|
|
||||||
lo_keyboard_did_hide();
|
lo_keyboard_did_hide();
|
||||||
}
|
}
|
||||||
@ -129,14 +147,14 @@ void lo_damaged(CGRect rect)
|
|||||||
void lo_show_keyboard()
|
void lo_show_keyboard()
|
||||||
{
|
{
|
||||||
dispatch_async(dispatch_get_main_queue(), ^{
|
dispatch_async(dispatch_get_main_queue(), ^{
|
||||||
[theView becomeFirstResponder];
|
[theView->textView becomeFirstResponder];
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
void lo_hide_keyboard()
|
void lo_hide_keyboard()
|
||||||
{
|
{
|
||||||
dispatch_async(dispatch_get_main_queue(), ^{
|
dispatch_async(dispatch_get_main_queue(), ^{
|
||||||
[theView resignFirstResponder];
|
[theView->textView resignFirstResponder];
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -9,8 +9,11 @@
|
|||||||
|
|
||||||
#import <UIKit/UIKit.h>
|
#import <UIKit/UIKit.h>
|
||||||
|
|
||||||
@interface View : UIView <UIKeyInput>
|
@interface View : UIView
|
||||||
|
{
|
||||||
|
@public
|
||||||
|
UITextView* textView;
|
||||||
|
}
|
||||||
- (void)drawRect:(CGRect)rect;
|
- (void)drawRect:(CGRect)rect;
|
||||||
- (void)tapGesture:(UIGestureRecognizer *)gestureRecognizer;
|
- (void)tapGesture:(UIGestureRecognizer *)gestureRecognizer;
|
||||||
|
|
||||||
|
@ -34,33 +34,11 @@
|
|||||||
CGPoint location = [gestureRecognizer locationInView: self];
|
CGPoint location = [gestureRecognizer locationInView: self];
|
||||||
NSLog(@"tapGesture: at: (%d,%d)", (int)location.x, (int)location.y);
|
NSLog(@"tapGesture: at: (%d,%d)", (int)location.x, (int)location.y);
|
||||||
lo_tap(location.x, location.y);
|
lo_tap(location.x, location.y);
|
||||||
|
[self->textView becomeFirstResponder];
|
||||||
} else
|
} else
|
||||||
NSLog(@"tapGesture: %@", gestureRecognizer);
|
NSLog(@"tapGesture: %@", gestureRecognizer);
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)insertText:(NSString *)text
|
|
||||||
{
|
|
||||||
(void) text;
|
|
||||||
// Do something with the typed character
|
|
||||||
}
|
|
||||||
|
|
||||||
- (void)deleteBackward
|
|
||||||
{
|
|
||||||
// Handle the delete key
|
|
||||||
}
|
|
||||||
|
|
||||||
- (BOOL)hasText
|
|
||||||
{
|
|
||||||
// Return whether there's any text present
|
|
||||||
return YES;
|
|
||||||
}
|
|
||||||
|
|
||||||
- (BOOL)canBecomeFirstResponder
|
|
||||||
{
|
|
||||||
return YES;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|
||||||
// vim:set shiftwidth=4 softtabstop=4 expandtab:
|
// vim:set shiftwidth=4 softtabstop=4 expandtab:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user