Key repeat is handled by the OS, since text input is now decoupled from physical key events.
authorSam Lantinga <slouken@libsdl.org>
Sat, 16 Jun 2007 15:32:04 +0000
changeset 2129047245361002
parent 2128 04e9ad5318dc
child 2130 3ee59c43d784
Key repeat is handled by the OS, since text input is now decoupled from physical key events.
include/SDL_compat.h
include/SDL_keyboard.h
src/SDL_compat.c
src/events/SDL_events.c
src/events/SDL_keyboard.c
src/events/SDL_keyboard_c.h
src/video/cocoa/SDL_cocoakeyboard.m
     1.1 --- a/include/SDL_compat.h	Sat Jun 16 05:52:34 2007 +0000
     1.2 +++ b/include/SDL_compat.h	Sat Jun 16 15:32:04 2007 +0000
     1.3 @@ -64,6 +64,9 @@
     1.4  #define SDL_BUTTON_WHEELUP	4
     1.5  #define SDL_BUTTON_WHEELDOWN	5
     1.6  
     1.7 +#define SDL_DEFAULT_REPEAT_DELAY	500
     1.8 +#define SDL_DEFAULT_REPEAT_INTERVAL	30
     1.9 +
    1.10  typedef struct SDL_VideoInfo
    1.11  {
    1.12      Uint32 hw_available:1;
    1.13 @@ -173,6 +176,8 @@
    1.14                                                    SDL_Rect * dstrect);
    1.15  extern DECLSPEC void SDLCALL SDL_FreeYUVOverlay(SDL_Overlay * overlay);
    1.16  extern DECLSPEC void SDLCALL SDL_GL_SwapBuffers(void);
    1.17 +extern DECLSPEC int SDLCALL SDL_EnableKeyRepeat(int delay, int interval);
    1.18 +extern DECLSPEC void SDLCALL SDL_GetKeyRepeat(int *delay, int *interval);
    1.19  
    1.20  /* Ends C function definitions when using C++ */
    1.21  #ifdef __cplusplus
     2.1 --- a/include/SDL_keyboard.h	Sat Jun 16 05:52:34 2007 +0000
     2.2 +++ b/include/SDL_keyboard.h	Sat Jun 16 15:32:04 2007 +0000
     2.3 @@ -93,32 +93,6 @@
     2.4  extern DECLSPEC int SDLCALL SDL_EnableUNICODE(int enable);
     2.5  
     2.6  /**
     2.7 - * \fn int SDL_EnableKeyRepeat(int delay, int interval)
     2.8 - * 
     2.9 - * \brief Enable keyboard repeat for the selected keyboard.
    2.10 - *
    2.11 - * \param delay The initial delay in milliseconds between the time when a
    2.12 - *              key is pressed and keyboard repeat begins.  Setting a delay
    2.13 - *              of 0 will disable keyboard repeat.
    2.14 - * \param interval The time in milliseconds between keyboard repeat events.
    2.15 - *
    2.16 - * \return 0 on success, or -1 if there was an error.
    2.17 - *
    2.18 - * \note Keyboard repeat defaults to off.
    2.19 - */
    2.20 -#define SDL_DEFAULT_REPEAT_DELAY	500
    2.21 -#define SDL_DEFAULT_REPEAT_INTERVAL	30
    2.22 - /**/
    2.23 -    extern DECLSPEC int SDLCALL SDL_EnableKeyRepeat(int delay, int interval);
    2.24 -
    2.25 -/**
    2.26 - * \fn void SDL_GetKeyRepeat(int *delay, int *interval)
    2.27 - *
    2.28 - * \brief Get the current keyboard repeat setting for the selected keyboard.
    2.29 - */
    2.30 -extern DECLSPEC void SDLCALL SDL_GetKeyRepeat(int *delay, int *interval);
    2.31 -
    2.32 -/**
    2.33   * \fn Uint8 *SDL_GetKeyState(int *numkeys)
    2.34   *
    2.35   * \brief Get a snapshot of the current state of the selected keyboard.
     3.1 --- a/src/SDL_compat.c	Sat Jun 16 05:52:34 2007 +0000
     3.2 +++ b/src/SDL_compat.c	Sat Jun 16 15:32:04 2007 +0000
     3.3 @@ -240,6 +240,12 @@
     3.4              }
     3.5              break;
     3.6          }
     3.7 +    case SDL_TEXTINPUT:
     3.8 +        {
     3.9 +            /* FIXME: Generate an old style key repeat event if needed */
    3.10 +            printf("TEXTINPUT: '%s'\n", event->text.text);
    3.11 +            break;
    3.12 +        }
    3.13      case SDL_MOUSEWHEEL:
    3.14          {
    3.15              Uint8 button;
    3.16 @@ -1447,4 +1453,22 @@
    3.17      SDL_GL_SwapWindow(SDL_VideoWindow);
    3.18  }
    3.19  
    3.20 +
    3.21 +int
    3.22 +SDL_EnableKeyRepeat(int delay, int interval)
    3.23 +{
    3.24 +    return 0;
    3.25 +}
    3.26 +
    3.27 +void
    3.28 +SDL_GetKeyRepeat(int *delay, int *interval)
    3.29 +{
    3.30 +    if (delay) {
    3.31 +        *delay = SDL_DEFAULT_REPEAT_DELAY;
    3.32 +    }
    3.33 +    if (interval) {
    3.34 +        *interval = SDL_DEFAULT_REPEAT_INTERVAL;
    3.35 +    }
    3.36 +}
    3.37 +
    3.38  /* vi: set ts=4 sw=4 expandtab: */
     4.1 --- a/src/events/SDL_events.c	Sat Jun 16 05:52:34 2007 +0000
     4.2 +++ b/src/events/SDL_events.c	Sat Jun 16 15:32:04 2007 +0000
     4.3 @@ -111,10 +111,6 @@
     4.4          if (_this) {
     4.5              _this->PumpEvents(_this);
     4.6          }
     4.7 -
     4.8 -        /* Queue pending key-repeat events */
     4.9 -        SDL_CheckKeyRepeat();
    4.10 -
    4.11  #if !SDL_JOYSTICK_DISABLED
    4.12          /* Check for joystick state change */
    4.13          if (SDL_numjoysticks && (SDL_eventstate & SDL_JOYEVENTMASK)) {
    4.14 @@ -386,10 +382,6 @@
    4.15          if (_this) {
    4.16              _this->PumpEvents(_this);
    4.17          }
    4.18 -
    4.19 -        /* Queue pending key-repeat events */
    4.20 -        SDL_CheckKeyRepeat();
    4.21 -
    4.22  #if !SDL_JOYSTICK_DISABLED
    4.23          /* Check for joystick state change */
    4.24          if (SDL_numjoysticks && (SDL_eventstate & SDL_JOYEVENTMASK)) {
     5.1 --- a/src/events/SDL_keyboard.c	Sat Jun 16 05:52:34 2007 +0000
     5.2 +++ b/src/events/SDL_keyboard.c	Sat Jun 16 15:32:04 2007 +0000
     5.3 @@ -349,7 +349,6 @@
     5.4              SDL_SendKeyboardKey(index, SDL_RELEASED, 0, key);
     5.5          }
     5.6      }
     5.7 -    keyboard->repeat.timestamp = 0;
     5.8  }
     5.9  
    5.10  void
    5.11 @@ -515,7 +514,7 @@
    5.12  SDL_SendKeyboardKey(int index, Uint8 state, Uint8 scancode, SDLKey key)
    5.13  {
    5.14      SDL_Keyboard *keyboard = SDL_GetKeyboard(index);
    5.15 -    int posted, repeatable;
    5.16 +    int posted;
    5.17      Uint16 modstate;
    5.18      Uint8 type;
    5.19  
    5.20 @@ -526,7 +525,6 @@
    5.21      printf("The '%s' key has been %s\n", SDL_GetKeyName(key),
    5.22             state == SDL_PRESSED ? "pressed" : "released");
    5.23  #endif
    5.24 -    repeatable = 0;
    5.25      if (state == SDL_PRESSED) {
    5.26          modstate = keyboard->modstate;
    5.27          switch (key) {
    5.28 @@ -566,7 +564,6 @@
    5.29              keyboard->modstate |= KMOD_MODE;
    5.30              break;
    5.31          default:
    5.32 -            repeatable = 1;
    5.33              break;
    5.34          }
    5.35      } else {
    5.36 @@ -616,13 +613,6 @@
    5.37          break;
    5.38      case SDL_RELEASED:
    5.39          type = SDL_KEYUP;
    5.40 -        /*
    5.41 -         * jk 991215 - Added
    5.42 -         */
    5.43 -        if (keyboard->repeat.timestamp &&
    5.44 -            keyboard->repeat.evt.key.keysym.sym == key) {
    5.45 -            keyboard->repeat.timestamp = 0;
    5.46 -        }
    5.47          break;
    5.48      default:
    5.49          /* Invalid state -- bail */
    5.50 @@ -654,19 +644,6 @@
    5.51          event.key.keysym.mod = modstate;
    5.52          event.key.keysym.unicode = 0;
    5.53          event.key.windowID = keyboard->focus;
    5.54 -        /* FIXME: This doesn't make sense anymore... */
    5.55 -        /*
    5.56 -         * jk 991215 - Added
    5.57 -         */
    5.58 -        if (repeatable && (keyboard->repeat.delay != 0)) {
    5.59 -            Uint32 timestamp = SDL_GetTicks();
    5.60 -            if (!timestamp) {
    5.61 -                timestamp = 1;
    5.62 -            }
    5.63 -            keyboard->repeat.evt = event;
    5.64 -            keyboard->repeat.firsttime = 1;
    5.65 -            keyboard->repeat.timestamp = 1;
    5.66 -        }
    5.67          posted = (SDL_PushEvent(&event) > 0);
    5.68      }
    5.69      return (posted);
    5.70 @@ -695,84 +672,4 @@
    5.71      return (posted);
    5.72  }
    5.73  
    5.74 -/*
    5.75 - * jk 991215 - Added
    5.76 - */
    5.77 -void
    5.78 -SDL_CheckKeyRepeat(void)
    5.79 -{
    5.80 -    int i;
    5.81 -
    5.82 -    for (i = 0; i < SDL_num_keyboards; ++i) {
    5.83 -        SDL_Keyboard *keyboard = SDL_keyboards[i];
    5.84 -
    5.85 -        if (!keyboard) {
    5.86 -            continue;
    5.87 -        }
    5.88 -
    5.89 -        if (keyboard->repeat.timestamp) {
    5.90 -            Uint32 now, interval;
    5.91 -
    5.92 -            now = SDL_GetTicks();
    5.93 -            interval = (now - keyboard->repeat.timestamp);
    5.94 -            if (keyboard->repeat.firsttime) {
    5.95 -                if (interval > (Uint32) keyboard->repeat.delay) {
    5.96 -                    keyboard->repeat.timestamp = now;
    5.97 -                    keyboard->repeat.firsttime = 0;
    5.98 -                }
    5.99 -            } else {
   5.100 -                if (interval > (Uint32) keyboard->repeat.interval) {
   5.101 -                    keyboard->repeat.timestamp = now;
   5.102 -                    SDL_PushEvent(&keyboard->repeat.evt);
   5.103 -                }
   5.104 -            }
   5.105 -        }
   5.106 -    }
   5.107 -}
   5.108 -
   5.109 -int
   5.110 -SDL_EnableKeyRepeat(int delay, int interval)
   5.111 -{
   5.112 -    SDL_Keyboard *keyboard = SDL_GetKeyboard(SDL_current_keyboard);
   5.113 -
   5.114 -    if (!keyboard) {
   5.115 -        SDL_SetError("No keyboard is currently selected");
   5.116 -        return -1;
   5.117 -    }
   5.118 -
   5.119 -    if ((delay < 0) || (interval < 0)) {
   5.120 -        SDL_SetError("keyboard repeat value less than zero");
   5.121 -        return -1;
   5.122 -    }
   5.123 -
   5.124 -    keyboard->repeat.firsttime = 0;
   5.125 -    keyboard->repeat.delay = delay;
   5.126 -    keyboard->repeat.interval = interval;
   5.127 -    keyboard->repeat.timestamp = 0;
   5.128 -
   5.129 -    return 0;
   5.130 -}
   5.131 -
   5.132 -void
   5.133 -SDL_GetKeyRepeat(int *delay, int *interval)
   5.134 -{
   5.135 -    SDL_Keyboard *keyboard = SDL_GetKeyboard(SDL_current_keyboard);
   5.136 -
   5.137 -    if (!keyboard) {
   5.138 -        if (delay) {
   5.139 -            *delay = 0;
   5.140 -        }
   5.141 -        if (interval) {
   5.142 -            *interval = 0;
   5.143 -        }
   5.144 -        return;
   5.145 -    }
   5.146 -    if (delay) {
   5.147 -        *delay = keyboard->repeat.delay;
   5.148 -    }
   5.149 -    if (interval) {
   5.150 -        *interval = keyboard->repeat.interval;
   5.151 -    }
   5.152 -}
   5.153 -
   5.154  /* vi: set ts=4 sw=4 expandtab: */
     6.1 --- a/src/events/SDL_keyboard_c.h	Sat Jun 16 05:52:34 2007 +0000
     6.2 +++ b/src/events/SDL_keyboard_c.h	Sat Jun 16 15:32:04 2007 +0000
     6.3 @@ -39,16 +39,6 @@
     6.4      Uint16 modstate;
     6.5      Uint8 keystate[SDLK_LAST];
     6.6  
     6.7 -    struct
     6.8 -    {
     6.9 -        int firsttime;          /* if we check against the delay or repeat value */
    6.10 -        int delay;              /* the delay before we start repeating */
    6.11 -        int interval;           /* the delay between key repeat events */
    6.12 -        Uint32 timestamp;       /* the time the first keydown event occurred */
    6.13 -
    6.14 -        SDL_Event evt;          /* the event we are supposed to repeat */
    6.15 -    } repeat;
    6.16 -
    6.17      void *driverdata;
    6.18  };
    6.19  
    6.20 @@ -85,9 +75,6 @@
    6.21  /* Send keyboard text input for a keyboard at an index */
    6.22  extern int SDL_SendKeyboardText(int index, const char *text);
    6.23  
    6.24 -/* Used by the event loop to queue pending keyboard repeat events */
    6.25 -extern void SDL_CheckKeyRepeat(void);
    6.26 -
    6.27  /* Shutdown the keyboard subsystem */
    6.28  extern void SDL_KeyboardQuit(void);
    6.29  
     7.1 --- a/src/video/cocoa/SDL_cocoakeyboard.m	Sat Jun 16 05:52:34 2007 +0000
     7.2 +++ b/src/video/cocoa/SDL_cocoakeyboard.m	Sat Jun 16 15:32:04 2007 +0000
     7.3 @@ -532,14 +532,15 @@
     7.4  
     7.5      switch ([event type]) {
     7.6      case NSKeyDown:
     7.7 -        if ([event isARepeat]) {
     7.8 -            break;
     7.9 +        if (![event isARepeat]) {
    7.10 +            SDL_SendKeyboardKey(data->keyboard, SDL_PRESSED, (Uint8)scancode,
    7.11 +                                data->keymap[scancode]);
    7.12          }
    7.13 -        SDL_SendKeyboardKey(data->keyboard, SDL_PRESSED, (Uint8)scancode,
    7.14 -                            data->keymap[scancode]);
    7.15 -        text = [[event characters] UTF8String];
    7.16 -        if(text && *text) {
    7.17 -            SDL_SendKeyboardText(data->keyboard, text);
    7.18 +        if (SDL_EventState(SDL_TEXTINPUT, SDL_QUERY)) {
    7.19 +            text = [[event characters] UTF8String];
    7.20 +            if(text && *text) {
    7.21 +                SDL_SendKeyboardText(data->keyboard, text);
    7.22 +            }
    7.23          }
    7.24          break;
    7.25      case NSKeyUp: