iOS7以降でも動く、キーイベントを処理するプログラムのサンプルが世の中に無いので作ってみました。
これで、前回ブログに書いたコンソール表示するiphoneアプリと組み合わせていろいろなテストアプリがつくれます。
隠しファイルのmain.mでアプリケーションクラスを継承
main.m
−−−−−−−−−−−−−−−−
//
// main.m
// HelloUITextView
//
// Created by yomei on 2014/09/04.
// Copyright (c) 2014年 yomei. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "AppDelegate.h"
#import "MyUIApplication.h"
int main(int argc, char *argv[])
{
@autoreleasepool {
return UIApplicationMain(argc, argv, @"MyUIApplication", NSStringFromClass([AppDelegate class]));
}
}
−−−−−−−−−−−−−−−アプリケーションクラスのヘッダー
MyUIApplication.h
−−−−−−−−−−−−−−−−
#import <UIKit/UIKit.h>
@interface MyUIApplication : UIApplication
{
NSMutableArray* _commands;
}
@end
−−−−−−−−−−−−−−−−アプリケーションのインプリ
−−−−−−−−−−−−−−−−
#import "MyUIApplication.h"
#include "ViewController.h"
@implementation MyUIApplication
- (NSArray *)keyCommands {
if (!_commands) {
NSMutableArray *cmd = [NSMutableArray array];
NSString* str=@"01234567890-^\xc2\xa5" @"qwertyuiop@[" @"asdfghjkl;:]" @"zxcvbnm,./_"
@"!\"#$%&'()=~`{+*}<>?_| "
@"\r\b";
int i,len;
UIKeyCommand *co;
len=[str length];
for(i=0;i<len;i++){
co = [UIKeyCommand keyCommandWithInput:[str substringWithRange:NSMakeRange(i, 1)] modifierFlags: 0 //UIKeyModifierCommand
action:@selector(handleKomoji:)];
[cmd addObject:co];
}
str=@"abcdefghijklmnopqrstuvwxyz";
len=[str length];
for(i=0;i<len;i++){
co = [UIKeyCommand keyCommandWithInput:[str substringWithRange:NSMakeRange(i, 1)] modifierFlags: UIKeyModifierShift
action:@selector(handleOomoji:)];
[cmd addObject:co];
}
str=@"01234567890-^\xc2\xa5" @"qwertyuiop@[" @"asdfghjkl;:]" @"zxcvbnm,./_"
@"!\"#$%&'()=~`{+*}<>?_| "
@"\r\b";
len=[str length];
for(i=0;i<len;i++){
co = [UIKeyCommand keyCommandWithInput:[str substringWithRange:NSMakeRange(i, 1)] modifierFlags: UIKeyModifierAlphaShift
action:@selector(handleCapsKomoji:)];
[cmd addObject:co];
}
str=@"abcdefghijklmnopqrstuvwxyz";
len=[str length];
for(i=0;i<len;i++){
co = [UIKeyCommand keyCommandWithInput:[str substringWithRange:NSMakeRange(i, 1)] modifierFlags: UIKeyModifierShift | UIKeyModifierAlphaShift
action:@selector(handleCapsOomoji:)];
[cmd addObject:co];
}
_commands = cmd;
}
return _commands;
}
- (void)handleKomoji:(UIKeyCommand *)keyCommand {
NSString* str;
const char* p;
str=keyCommand.input;
if([str isEqualToString:@"\xc2\xa5"])str=@"\\";
p=[str UTF8String];
[self onKey :(*p&255)];
}
- (void)handleOomoji:(UIKeyCommand *)keyCommand {
NSString* str;
char* p;
str=keyCommand.input;
p=[str UTF8String];
if(*p>='a' && *p<='z')*p-=' ';
[self onKey :(*p&255)];
}
- (void)handleCapsKomoji:(UIKeyCommand *)keyCommand {
NSString* str;
char* p;
str=keyCommand.input;
if([str isEqualToString:@"\xc2\xa5"])str=@"\\";
p=[str UTF8String];
if(*p>='a' && *p<='z')*p-=' ';
[self onKey :(*p&255)];
}
- (void)handleCapsOomoji:(UIKeyCommand *)keyCommand {
NSString* str;
char* p;
str=keyCommand.input;
p=[str UTF8String];
[self onKey :(*p&255)];
}
-(void)onKey:(int) keyCode
{
// dispatch keyevent
[ViewController onKey:keyCode];
}
@end
−−−−−−−−−−−−−−−−
0 件のコメント:
コメントを投稿