Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Commit

Permalink
Synchronized the on-screen keyboard state with whether we are accepti…
Browse files Browse the repository at this point in the history
…ng text input.

The functions to show/hide/toggle the on-screen keyboard have been folded into the text input state.
Calling SDL_StartTextInput() will automatically show the on-screen keyboard if it's available.
Calling SDL_StopTextInput() will automatically hide the on-screen keyboard if it's available.
There is a new API function SDL_IsTextInputActive() which will return whether text input is currently active.
Text input is disabled by default, you must call SDL_StartTextInput() when you are ready to accept text input.
SDL_HasScreenKeyboardSupport() no longer needs to be passed a window.
The iPhone-specific on-screen keyboard functions have been removed.
  • Loading branch information
slouken committed Nov 5, 2012
1 parent fcbad2e commit b1485e3
Show file tree
Hide file tree
Showing 18 changed files with 97 additions and 219 deletions.
14 changes: 6 additions & 8 deletions README.iOS
Expand Up @@ -80,14 +80,12 @@ Notes -- Keyboard

The SDL keyboard API has been extended to support on-screen keyboards:

int SDL_ShowScreenKeyboard(SDL_Window * window)
-- reveals the onscreen keyboard. Returns 0 on success and -1 on error.
int SDL_HideScreenKeyboard(SDL_Window * window)
-- hides the onscreen keyboard. Returns 0 on success and -1 on error.
SDL_bool SDL_IsScreenKeyboardShown(SDL_Window * window)
-- returns whether or not the onscreen keyboard is currently visible.
int SDL_ToggleScreenKeyboard(SDL_Window * window)
-- toggles the visibility of the onscreen keyboard. Returns 0 on success and -1 on error.
void SDL_StartTextInput()
-- enables text events and reveals the onscreen keyboard.
void SDL_StopTextInput()
-- disables text events and hides the onscreen keyboard.
SDL_bool SDL_IsTextInputActive()
-- returns whether or not text events are enabled (and the onscreen keyboard is visible)

==============================================================================
Notes -- Reading and Writing files
Expand Down
6 changes: 5 additions & 1 deletion Xcode-iOS/Demos/src/keyboard.c
Expand Up @@ -291,7 +291,11 @@ main(int argc, char *argv[])
break;
case SDL_MOUSEBUTTONUP:
/* mouse up toggles onscreen keyboard visibility */
SDL_ToggleScreenKeyboard(window);
if (SDL_IsTextInputActive()) {
SDL_StopTextInput();
} else {
SDL_StartTextInput();
}
break;
}
}
Expand Down
18 changes: 1 addition & 17 deletions android-project/src/org/libsdl/app/SDLActivity.java
Expand Up @@ -119,7 +119,7 @@ protected void onDestroy() {

// Messages from the SDLMain thread
static final int COMMAND_CHANGE_TITLE = 1;
static final int COMMAND_KEYBOARD_SHOW = 2;
static final int COMMAND_UNUSED = 2;
static final int COMMAND_TEXTEDIT_HIDE = 3;

// Handler for the messages
Expand All @@ -130,22 +130,6 @@ public void handleMessage(Message msg) {
case COMMAND_CHANGE_TITLE:
setTitle((String)msg.obj);
break;
case COMMAND_KEYBOARD_SHOW:
InputMethodManager manager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
if (manager != null) {
switch (((Integer)msg.obj).intValue()) {
case 0:
manager.hideSoftInputFromWindow(mSurface.getWindowToken(), 0);
break;
case 1:
manager.showSoftInput(mSurface, 0);
break;
case 2:
manager.toggleSoftInputFromWindow(mSurface.getWindowToken(), 0, 0);
break;
}
}
break;
case COMMAND_TEXTEDIT_HIDE:
if (mTextEdit != null) {
mTextEdit.setVisibility(View.GONE);
Expand Down
66 changes: 14 additions & 52 deletions include/SDL_keyboard.h
Expand Up @@ -151,21 +151,34 @@ extern DECLSPEC SDL_Keycode SDLCALL SDL_GetKeyFromName(const char *name);

/**
* \brief Start accepting Unicode text input events.
* This function will show the on-screen keyboard if supported.
*
* \sa SDL_StopTextInput()
* \sa SDL_SetTextInputRect()
* \sa SDL_HasScreenKeyboardSupport()
*/
extern DECLSPEC void SDLCALL SDL_StartTextInput(void);

/**
* \brief Return whether or not Unicode text input events are enabled.
*
* \sa SDL_StartTextInput()
* \sa SDL_StopTextInput()
*/
extern DECLSPEC SDL_bool SDLCALL SDL_IsTextInputActive(void);

/**
* \brief Stop receiving any text input events.
* This function will hide the on-screen keyboard if supported.
*
* \sa SDL_StartTextInput()
* \sa SDL_HasScreenKeyboardSupport()
*/
extern DECLSPEC void SDLCALL SDL_StopTextInput(void);

/**
* \brief Set the rectangle used to type Unicode text inputs.
* This is used as a hint for IME and on-screen keyboard placement.
*
* \sa SDL_StartTextInput()
*/
Expand All @@ -174,60 +187,13 @@ extern DECLSPEC void SDLCALL SDL_SetTextInputRect(SDL_Rect *rect);
/**
* \brief Returns whether the platform has some screen keyboard support.
*
* \param window The window for which screen keyboard should be checked.
*
* \return SDL_TRUE if some keyboard support is available else SDL_FALSE.
*
* \note Not all screen keyboard functions are supported on all platforms.
*
* \sa SDL_ShowScreenKeyboard()
* \sa SDL_HideScreenKeyboard()
* \sa SDL_IsScreenKeyboardShown()
* \sa SDL_ToggleScreenKeyboard()
*/
extern DECLSPEC SDL_bool SDLCALL SDL_HasScreenKeyboardSupport(SDL_Window *window);

/**
* \brief Requests to show a screen keyboard for given window.
*
* \param window The window for which screen keyboard should be shown.
*
* \return 0 if request will be processed or -1 on error (e.g. no support).
*
* \note Showing screen keyboards is asynchronous on some platforms.
*
* \sa SDL_HasScreenKeyboardSupport()
* \sa SDL_HideScreenKeyboard()
*/
extern DECLSPEC int SDLCALL SDL_ShowScreenKeyboard(SDL_Window *window);

/**
* \brief Requests to hide a screen keyboard for given window.
*
* \param window The window for which screen keyboard should be shown.
*
* \return 0 if request will be processed or -1 on error (e.g. no support).
*
* \note Hiding screen keyboards is asynchronous on some platforms.
*
* \sa SDL_HasScreenKeyboardSupport()
* \sa SDL_ShowScreenKeyboard()
*/
extern DECLSPEC int SDLCALL SDL_HideScreenKeyboard(SDL_Window *window);

/**
* \brief Requests to toggle a screen keyboard for given window.
*
* \param window The window for which screen keyboard should be toggled.
*
* \return 0 if request will be processed or -1 on error (e.g. no support).
*
* \note Showing and hiding screen keyboards is asynchronous on some platforms.
*
* \sa SDL_HasScreenKeyboardSupport()
* \sa SDL_IsScreenKeyboardShown()
*/
extern DECLSPEC int SDLCALL SDL_ToggleScreenKeyboard(SDL_Window * window);
extern DECLSPEC SDL_bool SDLCALL SDL_HasScreenKeyboardSupport();

/**
* \brief Returns whether the screen keyboard is shown for given window.
Expand All @@ -236,11 +202,7 @@ extern DECLSPEC int SDLCALL SDL_ToggleScreenKeyboard(SDL_Window * window);
*
* \return SDL_TRUE if screen keyboard is shown else SDL_FALSE.
*
* \note May always return SDL_FALSE on some platforms (not implemented there).
*
* \sa SDL_HasScreenKeyboardSupport()
* \sa SDL_ShowScreenKeyboard()
* \sa SDL_HideScreenKeyboard()
*/
extern DECLSPEC SDL_bool SDLCALL SDL_IsScreenKeyboardShown(SDL_Window *window);

Expand Down
5 changes: 0 additions & 5 deletions include/SDL_system.h
Expand Up @@ -49,11 +49,6 @@ extern "C" {
extern DECLSPEC int SDLCALL SDL_iPhoneSetAnimationCallback(SDL_Window * window, int interval, void (*callback)(void*), void *callbackParam);
extern DECLSPEC void SDLCALL SDL_iPhoneSetEventPump(SDL_bool enabled);

#define SDL_iPhoneKeyboardShow SDL_ShowScreenKeyboard
#define SDL_iPhoneKeyboardHide SDL_HideScreenKeyboard
#define SDL_iPhoneKeyboardToggle SDL_ToggleScreenKeyboard
#define SDL_iPhoneKeyboardIsShown SDL_IsScreenKeyboardShown

#endif /* __IPHONEOS__ */


Expand Down
25 changes: 8 additions & 17 deletions src/core/android/SDL_android.cpp
Expand Up @@ -957,39 +957,30 @@ extern "C" int Android_JNI_SendMessage(int command, int param)
return 0;
}

extern "C" int Android_JNI_ShowTextInput(SDL_Rect *inputRect)
extern "C" void Android_JNI_ShowTextInput(SDL_Rect *inputRect)
{
JNIEnv *env = Android_JNI_GetEnv();
if (!env) {
return -1;
return;
}

jmethodID mid = env->GetStaticMethodID(mActivityClass, "showTextInput", "(IIII)V");
if (!mid) {
return -1;
return;
}
env->CallStaticVoidMethod( mActivityClass, mid,
inputRect->x,
inputRect->y,
inputRect->w,
inputRect->h );
return 0;
}

/*extern "C" int Android_JNI_HideTextInput()
extern "C" void Android_JNI_HideTextInput()
{
JNIEnv *env = Android_JNI_GetEnv();
if (!env) {
return -1;
}
jmethodID mid = env->GetStaticMethodID(mActivityClass, "hideTextInput", "()V");
if (!mid) {
return -1;
}
env->CallStaticVoidMethod(mActivityClass, mid);
return 0;
}*/
// has to match Activity constant
const int COMMAND_TEXTEDIT_HIDE = 3;
Android_JNI_SendMessage(COMMAND_TEXTEDIT_HIDE, 0);
}

//////////////////////////////////////////////////////////////////////////////
//
Expand Down
3 changes: 2 additions & 1 deletion src/core/android/SDL_android.h
Expand Up @@ -34,7 +34,8 @@ extern SDL_bool Android_JNI_CreateContext(int majorVersion, int minorVersion);
extern void Android_JNI_SwapWindow();
extern void Android_JNI_SetActivityTitle(const char *title);
extern SDL_bool Android_JNI_GetAccelerometerValues(float values[3]);
extern int Android_JNI_ShowTextInput(SDL_Rect *inputRect);
extern void Android_JNI_ShowTextInput(SDL_Rect *inputRect);
extern void Android_JNI_HideTextInput();

// Audio support
extern int Android_JNI_OpenAudioDevice(int sampleRate, int is16Bit, int channelCount, int desiredBufferFrames);
Expand Down
2 changes: 2 additions & 0 deletions src/events/SDL_events.c
Expand Up @@ -125,6 +125,8 @@ SDL_StartEventLoop(void)

/* No filter to start with, process most event types */
SDL_EventOK = NULL;
SDL_EventState(SDL_TEXTINPUT, SDL_DISABLE);
SDL_EventState(SDL_TEXTEDITING, SDL_DISABLE);
SDL_EventState(SDL_SYSWMEVENT, SDL_DISABLE);

/* Create the lock and set ourselves active */
Expand Down
7 changes: 3 additions & 4 deletions src/video/SDL_sysvideo.h
Expand Up @@ -236,10 +236,9 @@ struct SDL_VideoDevice
void (*SetTextInputRect) (_THIS, SDL_Rect *rect);

/* Screen keyboard */
SDL_bool (*SDL_HasScreenKeyboardSupport) (_THIS, SDL_Window *window);
int (*SDL_ShowScreenKeyboard) (_THIS, SDL_Window *window);
int (*SDL_HideScreenKeyboard) (_THIS, SDL_Window *window);
int (*SDL_ToggleScreenKeyboard) (_THIS, SDL_Window *window);
SDL_bool (*SDL_HasScreenKeyboardSupport) (_THIS);
void (*SDL_ShowScreenKeyboard) (_THIS, SDL_Window *window);
void (*SDL_HideScreenKeyboard) (_THIS, SDL_Window *window);
SDL_bool (*SDL_IsScreenKeyboardShown) (_THIS, SDL_Window *window);

/* Clipboard */
Expand Down
65 changes: 33 additions & 32 deletions src/video/SDL_video.c
Expand Up @@ -2771,19 +2771,47 @@ SDL_GetWindowWMInfo(SDL_Window * window, struct SDL_SysWMinfo *info)
void
SDL_StartTextInput(void)
{
SDL_Window *window;

/* First, enable text events */
SDL_EventState(SDL_TEXTINPUT, SDL_ENABLE);
SDL_EventState(SDL_TEXTEDITING, SDL_ENABLE);

/* Then show the on-screen keyboard, if any */
window = SDL_GetFocusWindow();
if (window && _this && _this->SDL_ShowScreenKeyboard) {
_this->SDL_ShowScreenKeyboard(_this, window);
}

/* Finally start the text input system */
if (_this && _this->StartTextInput) {
_this->StartTextInput(_this);
}
SDL_EventState(SDL_TEXTINPUT, SDL_ENABLE);
SDL_EventState(SDL_TEXTEDITING, SDL_ENABLE);
}

SDL_bool
SDL_IsTextInputActive(void)
{
return (SDL_GetEventState(SDL_TEXTINPUT) == SDL_ENABLE);
}

void
SDL_StopTextInput(void)
{
SDL_Window *window;

/* Stop the text input system */
if (_this && _this->StopTextInput) {
_this->StopTextInput(_this);
}

/* Hide the on-screen keyboard, if any */
window = SDL_GetFocusWindow();
if (window && _this && _this->SDL_HideScreenKeyboard) {
_this->SDL_HideScreenKeyboard(_this, window);
}

/* Finally disable text events */
SDL_EventState(SDL_TEXTINPUT, SDL_DISABLE);
SDL_EventState(SDL_TEXTEDITING, SDL_DISABLE);
}
Expand All @@ -2797,41 +2825,14 @@ SDL_SetTextInputRect(SDL_Rect *rect)
}

SDL_bool
SDL_HasScreenKeyboardSupport(SDL_Window *window)
SDL_HasScreenKeyboardSupport(void)
{
if (window && _this && _this->SDL_HasScreenKeyboardSupport) {
return _this->SDL_HasScreenKeyboardSupport(_this, window);
if (_this && _this->SDL_HasScreenKeyboardSupport) {
return _this->SDL_HasScreenKeyboardSupport(_this);
}
return SDL_FALSE;
}

int
SDL_ShowScreenKeyboard(SDL_Window *window)
{
if (window && _this && _this->SDL_ShowScreenKeyboard) {
return _this->SDL_ShowScreenKeyboard(_this, window);
}
return -1;
}

int
SDL_HideScreenKeyboard(SDL_Window *window)
{
if (window && _this && _this->SDL_HideScreenKeyboard) {
return _this->SDL_HideScreenKeyboard(_this, window);
}
return -1;
}

int
SDL_ToggleScreenKeyboard(SDL_Window *window)
{
if (window && _this && _this->SDL_ToggleScreenKeyboard) {
return _this->SDL_ToggleScreenKeyboard(_this, window);
}
return -1;
}

SDL_bool
SDL_IsScreenKeyboardShown(SDL_Window *window)
{
Expand Down

0 comments on commit b1485e3

Please sign in to comment.