SDL_uikitkeyboard extends SDL_uikitview to add keyboard support. It adds several methods to the SDL_uikitview class for keyboard initialization, showing, and hiding. SDL_uikitkeyboard.m contains the implementation of these methods as well as the implementation of some iPhone specific additions to SDL so that a programmer can access the functionality of hiding and showing the keyboard.
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/src/video/uikit/SDL_uikitkeyboard.h Tue Jul 29 17:21:49 2008 +0000
1.3 @@ -0,0 +1,47 @@
1.4 +/*
1.5 + SDL - Simple DirectMedia Layer
1.6 + Copyright (C) 1997-2006 Sam Lantinga
1.7 +
1.8 + This library is free software; you can redistribute it and/or
1.9 + modify it under the terms of the GNU Lesser General Public
1.10 + License as published by the Free Software Foundation; either
1.11 + version 2.1 of the License, or (at your option) any later version.
1.12 +
1.13 + This library is distributed in the hope that it will be useful,
1.14 + but WITHOUT ANY WARRANTY; without even the implied warranty of
1.15 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1.16 + Lesser General Public License for more details.
1.17 +
1.18 + You should have received a copy of the GNU Lesser General Public
1.19 + License along with this library; if not, write to the Free Software
1.20 + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
1.21 +
1.22 + Sam Lantinga
1.23 + slouken@libsdl.org
1.24 + */
1.25 +
1.26 +#ifndef _SDL_uikitkeyboard_h
1.27 +#define _SDL_uikitkeyboard_h
1.28 +
1.29 +#if SDL_IPHONE_KEYBOARD
1.30 +
1.31 +#import "SDL_uikitview.h"
1.32 +
1.33 +@interface SDL_uikitview (keyboardMethods)
1.34 +
1.35 +- (void)showKeyboard;
1.36 +- (void)hideKeyboard;
1.37 +- (void)initializeKeyboard;
1.38 +
1.39 +@property (readonly) BOOL keyboardVisible;
1.40 +
1.41 +@end
1.42 +
1.43 +#endif /* SDL_IPHONE_KEYBOARD */
1.44 +
1.45 +extern DECLSPEC int SDLCALL SDL_iPhoneKeyboardShow(SDL_WindowID windowID);
1.46 +extern DECLSPEC int SDLCALL SDL_iPhoneKeyboardHide(SDL_WindowID windowID);
1.47 +extern DECLSPEC SDL_bool SDLCALL SDL_iPhoneKeyboardIsShown(SDL_WindowID windowID);
1.48 +extern DECLSPEC int SDLCALL SDL_iPhoneKeyboardToggle(SDL_WindowID windowID);
1.49 +
1.50 +#endif /* _SDL_uikitkeyboard_h */
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
2.2 +++ b/src/video/uikit/SDL_uikitkeyboard.m Tue Jul 29 17:21:49 2008 +0000
2.3 @@ -0,0 +1,263 @@
2.4 +/*
2.5 + SDL - Simple DirectMedia Layer
2.6 + Copyright (C) 1997-2006 Sam Lantinga
2.7 +
2.8 + This library is free software; you can redistribute it and/or
2.9 + modify it under the terms of the GNU Lesser General Public
2.10 + License as published by the Free Software Foundation; either
2.11 + version 2.1 of the License, or (at your option) any later version.
2.12 +
2.13 + This library is distributed in the hope that it will be useful,
2.14 + but WITHOUT ANY WARRANTY; without even the implied warranty of
2.15 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
2.16 + Lesser General Public License for more details.
2.17 +
2.18 + You should have received a copy of the GNU Lesser General Public
2.19 + License along with this library; if not, write to the Free Software
2.20 + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
2.21 +
2.22 + Sam Lantinga
2.23 + slouken@libsdl.org
2.24 + */
2.25 +
2.26 +#include "SDL_video.h"
2.27 +
2.28 +#if SDL_IPHONE_KEYBOARD
2.29 +
2.30 +#import "SDL_uikitkeyboard.h"
2.31 +#import "SDL_uikitview.h"
2.32 +#import "SDL_keyboard_c.h"
2.33 +#import "keyinfotable.h"
2.34 +#import "SDL_uikitappdelegate.h"
2.35 +#import "SDL_uikitwindow.h"
2.36 +
2.37 +@implementation SDL_uikitview (keyboardMethods)
2.38 +
2.39 +- (BOOL)keyboardVisible {
2.40 + return keyboardVisible;
2.41 +}
2.42 +
2.43 +/* UITextFieldDelegate related methods */
2.44 +- (void)initializeKeyboard {
2.45 +
2.46 + NSLog(@"Text field init");
2.47 +
2.48 + textField = [[UITextField alloc] initWithFrame: CGRectZero];
2.49 + textField.delegate = self;
2.50 + /* placeholder so there is something to delete! */
2.51 + textField.text = @" ";
2.52 +
2.53 + /* set UITextInputTrait properties, mostly to defaults */
2.54 + textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
2.55 + textField.autocorrectionType = UITextAutocorrectionTypeNo;
2.56 + textField.enablesReturnKeyAutomatically = NO;
2.57 + textField.keyboardAppearance = UIKeyboardAppearanceDefault;
2.58 + textField.keyboardType = UIKeyboardTypeDefault;
2.59 + textField.returnKeyType = UIReturnKeyDefault;
2.60 + textField.secureTextEntry = NO;
2.61 +
2.62 + textField.hidden = YES;
2.63 + keyboardVisible = NO;
2.64 + [self addSubview: textField];
2.65 +
2.66 + /*
2.67 + SDL makes a copy of our keyboard.
2.68 + */
2.69 +
2.70 + SDL_Keyboard keyboard;
2.71 + SDL_zero(keyboard);
2.72 + //data->keyboard = SDL_AddKeyboard(&keyboard, -1);
2.73 + /*
2.74 + We'll need to delete this keyboard ...
2.75 + */
2.76 + SDL_AddKeyboard(&keyboard, 0);
2.77 + SDLKey keymap[SDL_NUM_SCANCODES];
2.78 + SDL_GetDefaultKeymap(keymap);
2.79 + SDL_SetKeymap(0, 0, keymap, SDL_NUM_SCANCODES);
2.80 +
2.81 +}
2.82 +
2.83 +- (void)showKeyboard {
2.84 + keyboardVisible = YES;
2.85 + [textField becomeFirstResponder];
2.86 +}
2.87 +
2.88 +- (void)hideKeyboard {
2.89 + keyboardVisible = NO;
2.90 + [textField resignFirstResponder];
2.91 +}
2.92 +
2.93 +- (BOOL)textField:(UITextField *)_textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
2.94 +
2.95 + if ([string length] == 0) {
2.96 + /* it wants to replace text with nothing, ie a delete */
2.97 + SDL_SendKeyboardKey( 0, SDL_PRESSED, SDL_SCANCODE_DELETE);
2.98 + SDL_SendKeyboardKey( 0, SDL_RELEASED, SDL_SCANCODE_DELETE);
2.99 + }
2.100 + else {
2.101 +
2.102 + int i;
2.103 + for (i=0; i<[string length]; i++) {
2.104 +
2.105 + unichar c = [string characterAtIndex: i];
2.106 +
2.107 + Uint16 mod = 0;
2.108 + SDL_scancode code;
2.109 +
2.110 + if (0 <= c && c < 127) {
2.111 + code = unicharToUIKeyInfoTable[c].code;
2.112 + mod = unicharToUIKeyInfoTable[c].mod;
2.113 + }
2.114 + else {
2.115 + code = SDL_SCANCODE_UNKNOWN;
2.116 + mod = 0;
2.117 + }
2.118 +
2.119 + if (mod & KMOD_SHIFT) {
2.120 + SDL_SendKeyboardKey( 0, SDL_PRESSED, SDL_SCANCODE_LSHIFT);
2.121 + }
2.122 + SDL_SendKeyboardKey( 0, SDL_PRESSED, code);
2.123 + SDL_SendKeyboardKey( 0, SDL_RELEASED, code);
2.124 + if (mod & KMOD_SHIFT) {
2.125 + SDL_SendKeyboardKey( 0, SDL_RELEASED, SDL_SCANCODE_LSHIFT);
2.126 + }
2.127 +
2.128 + }
2.129 +
2.130 + }
2.131 + return NO; /* don't allow the edit(!) */
2.132 +}
2.133 +
2.134 +/* Terminates the editing session */
2.135 +- (BOOL)textFieldShouldReturn:(UITextField*)_textField {
2.136 + [self hideKeyboard];
2.137 + return YES;
2.138 +}
2.139 +
2.140 +@end
2.141 +
2.142 +/* iPhone keyboard addition functions */
2.143 +
2.144 +int SDL_iPhoneKeyboardShow(SDL_WindowID windowID) {
2.145 +
2.146 + SDL_Window *window = SDL_GetWindowFromID(windowID);
2.147 + SDL_WindowData *data;
2.148 + SDL_uikitview *view;
2.149 +
2.150 + if (NULL == window) {
2.151 + SDL_SetError("Window does not exist");
2.152 + return -1;
2.153 + }
2.154 +
2.155 + data = (SDL_WindowData *)window->driverdata;
2.156 + view = data->view;
2.157 +
2.158 + if (nil == view) {
2.159 + SDL_SetError("Window has no view");
2.160 + return -1;
2.161 + }
2.162 + else {
2.163 + [view showKeyboard];
2.164 + return 0;
2.165 + }
2.166 +}
2.167 +
2.168 +int SDL_iPhoneKeyboardHide(SDL_WindowID windowID) {
2.169 +
2.170 + SDL_Window *window = SDL_GetWindowFromID(windowID);
2.171 + SDL_WindowData *data;
2.172 + SDL_uikitview *view;
2.173 +
2.174 + if (NULL == window) {
2.175 + SDL_SetError("Window does not exist");
2.176 + return -1;
2.177 + }
2.178 +
2.179 + data = (SDL_WindowData *)window->driverdata;
2.180 + view = data->view;
2.181 +
2.182 + if (NULL == view) {
2.183 + SDL_SetError("Window has no view");
2.184 + return -1;
2.185 + }
2.186 + else {
2.187 + [view hideKeyboard];
2.188 + return 0;
2.189 + }
2.190 +}
2.191 +
2.192 +SDL_bool SDL_iPhoneKeyboardIsShown(SDL_WindowID windowID) {
2.193 +
2.194 + SDL_Window *window = SDL_GetWindowFromID(windowID);
2.195 + SDL_WindowData *data;
2.196 + SDL_uikitview *view;
2.197 +
2.198 + if (NULL == window) {
2.199 + SDL_SetError("Window does not exist");
2.200 + return -1;
2.201 + }
2.202 +
2.203 + data = (SDL_WindowData *)window->driverdata;
2.204 + view = data->view;
2.205 +
2.206 + if (NULL == view) {
2.207 + SDL_SetError("Window has no view");
2.208 + return 0;
2.209 + }
2.210 + else {
2.211 + return view.keyboardVisible;
2.212 + }
2.213 +}
2.214 +
2.215 +int SDL_iPhoneKeyboardToggle(SDL_WindowID windowID) {
2.216 +
2.217 + SDL_Window *window = SDL_GetWindowFromID(windowID);
2.218 + SDL_WindowData *data;
2.219 + SDL_uikitview *view;
2.220 +
2.221 + if (NULL == window) {
2.222 + SDL_SetError("Window does not exist");
2.223 + return -1;
2.224 + }
2.225 +
2.226 + data = (SDL_WindowData *)window->driverdata;
2.227 + view = data->view;
2.228 +
2.229 + if (NULL == view) {
2.230 + SDL_SetError("Window has no view");
2.231 + return -1;
2.232 + }
2.233 + else {
2.234 + if (SDL_iPhoneKeyboardIsShown(windowID)) {
2.235 + SDL_iPhoneKeyboardHide(windowID);
2.236 + }
2.237 + else {
2.238 + SDL_iPhoneKeyboardShow(windowID);
2.239 + }
2.240 + return 0;
2.241 + }
2.242 +}
2.243 +
2.244 +#else
2.245 +
2.246 +int SDL_iPhoneKeyboardShow(SDL_WindowID windowID) {
2.247 + SDL_SetError("Not compiled with keyboard support");
2.248 + return -1;
2.249 +}
2.250 +
2.251 +int SDL_iPhoneKeyboardHide(SDL_WindowID windowID) {
2.252 + SDL_SetError("Not compiled with keyboard support");
2.253 + return -1;
2.254 +}
2.255 +
2.256 +SDL_bool SDL_iPhoneKeyboardIsShown(SDL_WindowID windowID) {
2.257 + return 0;
2.258 +}
2.259 +
2.260 +int SDL_iPhoneKeyboardToggle(SDL_WindowID windowID) {
2.261 + SDL_SetError("Not compiled with keyboard support");
2.262 + return -1;
2.263 +}
2.264 +
2.265 +
2.266 +#endif /* SDL_IPHONE_KEYBOARD */