src/events/SDL_keyboard.c
branchSDL-1.3
changeset 1724 6c63fc2bd986
parent 1722 5daa04d862f1
child 1731 875c3cf1a12c
     1.1 --- a/src/events/SDL_keyboard.c	Thu Jul 06 05:53:32 2006 +0000
     1.2 +++ b/src/events/SDL_keyboard.c	Thu Jul 06 07:17:11 2006 +0000
     1.3 @@ -338,18 +338,15 @@
     1.4  SDL_ResetKeyboard(int index)
     1.5  {
     1.6      SDL_Keyboard *keyboard = SDL_GetKeyboard(index);
     1.7 -    SDL_keysym keysym;
     1.8 -    Uint16 key;
     1.9 +    SDLKey key;
    1.10  
    1.11      if (!keyboard) {
    1.12          return;
    1.13      }
    1.14  
    1.15 -    SDL_memset(&keysym, 0, (sizeof keysym));
    1.16      for (key = SDLK_FIRST; key < SDLK_LAST; ++key) {
    1.17          if (keyboard->keystate[key] == SDL_PRESSED) {
    1.18 -            keysym.sym = key;
    1.19 -            SDL_SendKeyboardKey(index, 0, SDL_RELEASED, &keysym);
    1.20 +            SDL_SendKeyboardKey(index, SDL_RELEASED, 0, key);
    1.21          }
    1.22      }
    1.23      keyboard->repeat.timestamp = 0;
    1.24 @@ -463,9 +460,57 @@
    1.25      return keyname;
    1.26  }
    1.27  
    1.28 +void
    1.29 +SDL_SetKeyboardFocus(int index, SDL_WindowID windowID)
    1.30 +{
    1.31 +    SDL_Keyboard *keyboard = SDL_GetKeyboard(index);
    1.32 +    int i;
    1.33 +    SDL_bool focus;
    1.34 +
    1.35 +    if (!keyboard || (keyboard->focus == windowID)) {
    1.36 +        return;
    1.37 +    }
    1.38 +
    1.39 +    /* See if the current window has lost focus */
    1.40 +    if (keyboard->focus) {
    1.41 +        focus = SDL_FALSE;
    1.42 +        for (i = 0; i < SDL_num_keyboards; ++i) {
    1.43 +            SDL_Keyboard *check;
    1.44 +            if (i != index) {
    1.45 +                check = SDL_GetKeyboard(i);
    1.46 +                if (check && check->focus == keyboard->focus) {
    1.47 +                    focus = SDL_TRUE;
    1.48 +                    break;
    1.49 +                }
    1.50 +            }
    1.51 +        }
    1.52 +        if (!focus) {
    1.53 +            SDL_SendWindowEvent(windowID, SDL_WINDOWEVENT_FOCUS_GAINED, 0, 0);
    1.54 +        }
    1.55 +    }
    1.56 +
    1.57 +    keyboard->focus = windowID;
    1.58 +
    1.59 +    if (keyboard->focus) {
    1.60 +        focus = SDL_FALSE;
    1.61 +        for (i = 0; i < SDL_num_keyboards; ++i) {
    1.62 +            SDL_Keyboard *check;
    1.63 +            if (i != index) {
    1.64 +                check = SDL_GetKeyboard(i);
    1.65 +                if (check && check->focus == keyboard->focus) {
    1.66 +                    focus = SDL_TRUE;
    1.67 +                    break;
    1.68 +                }
    1.69 +            }
    1.70 +        }
    1.71 +        if (!focus) {
    1.72 +            SDL_SendWindowEvent(windowID, SDL_WINDOWEVENT_FOCUS_LOST, 0, 0);
    1.73 +        }
    1.74 +    }
    1.75 +}
    1.76 +
    1.77  int
    1.78 -SDL_SendKeyboardKey(int index, SDL_WindowID windowID, Uint8 state,
    1.79 -                    SDL_keysym * keysym)
    1.80 +SDL_SendKeyboardKey(int index, Uint8 state, Uint8 scancode, SDLKey key)
    1.81  {
    1.82      SDL_Keyboard *keyboard = SDL_GetKeyboard(index);
    1.83      int posted, repeatable;
    1.84 @@ -475,106 +520,91 @@
    1.85      if (!keyboard) {
    1.86          return 0;
    1.87      }
    1.88 -
    1.89 -    if (windowID) {
    1.90 -        keyboard->focus = windowID;
    1.91 -    }
    1.92  #if 0
    1.93 -    printf("The '%s' key has been %s\n", SDL_GetKeyName(keysym->sym),
    1.94 +    printf("The '%s' key has been %s\n", SDL_GetKeyName(key),
    1.95             state == SDL_PRESSED ? "pressed" : "released");
    1.96  #endif
    1.97 -    /* Set up the keysym */
    1.98 -    modstate = keyboard->modstate;
    1.99 -
   1.100      repeatable = 0;
   1.101 -
   1.102      if (state == SDL_PRESSED) {
   1.103 -        keysym->mod = modstate;
   1.104 -        switch (keysym->sym) {
   1.105 +        modstate = keyboard->modstate;
   1.106 +        switch (key) {
   1.107          case SDLK_UNKNOWN:
   1.108              break;
   1.109          case SDLK_NUMLOCK:
   1.110 -            modstate ^= KMOD_NUM;
   1.111 -            if (!(modstate & KMOD_NUM))
   1.112 -                state = SDL_RELEASED;
   1.113 -            keysym->mod = modstate;
   1.114 +            keyboard->modstate ^= KMOD_NUM;
   1.115              break;
   1.116          case SDLK_CAPSLOCK:
   1.117 -            modstate ^= KMOD_CAPS;
   1.118 -            if (!(modstate & KMOD_CAPS))
   1.119 -                state = SDL_RELEASED;
   1.120 -            keysym->mod = modstate;
   1.121 +            keyboard->modstate ^= KMOD_CAPS;
   1.122              break;
   1.123          case SDLK_LCTRL:
   1.124 -            modstate |= KMOD_LCTRL;
   1.125 +            keyboard->modstate |= KMOD_LCTRL;
   1.126              break;
   1.127          case SDLK_RCTRL:
   1.128 -            modstate |= KMOD_RCTRL;
   1.129 +            keyboard->modstate |= KMOD_RCTRL;
   1.130              break;
   1.131          case SDLK_LSHIFT:
   1.132 -            modstate |= KMOD_LSHIFT;
   1.133 +            keyboard->modstate |= KMOD_LSHIFT;
   1.134              break;
   1.135          case SDLK_RSHIFT:
   1.136 -            modstate |= KMOD_RSHIFT;
   1.137 +            keyboard->modstate |= KMOD_RSHIFT;
   1.138              break;
   1.139          case SDLK_LALT:
   1.140 -            modstate |= KMOD_LALT;
   1.141 +            keyboard->modstate |= KMOD_LALT;
   1.142              break;
   1.143          case SDLK_RALT:
   1.144 -            modstate |= KMOD_RALT;
   1.145 +            keyboard->modstate |= KMOD_RALT;
   1.146              break;
   1.147          case SDLK_LMETA:
   1.148 -            modstate |= KMOD_LMETA;
   1.149 +            keyboard->modstate |= KMOD_LMETA;
   1.150              break;
   1.151          case SDLK_RMETA:
   1.152 -            modstate |= KMOD_RMETA;
   1.153 +            keyboard->modstate |= KMOD_RMETA;
   1.154              break;
   1.155          case SDLK_MODE:
   1.156 -            modstate |= KMOD_MODE;
   1.157 +            keyboard->modstate |= KMOD_MODE;
   1.158              break;
   1.159          default:
   1.160              repeatable = 1;
   1.161              break;
   1.162          }
   1.163      } else {
   1.164 -        switch (keysym->sym) {
   1.165 +        switch (key) {
   1.166          case SDLK_UNKNOWN:
   1.167              break;
   1.168          case SDLK_NUMLOCK:
   1.169          case SDLK_CAPSLOCK:
   1.170 -            /* Only send keydown events */
   1.171 -            return (0);
   1.172 +            break;
   1.173          case SDLK_LCTRL:
   1.174 -            modstate &= ~KMOD_LCTRL;
   1.175 +            keyboard->modstate &= ~KMOD_LCTRL;
   1.176              break;
   1.177          case SDLK_RCTRL:
   1.178 -            modstate &= ~KMOD_RCTRL;
   1.179 +            keyboard->modstate &= ~KMOD_RCTRL;
   1.180              break;
   1.181          case SDLK_LSHIFT:
   1.182 -            modstate &= ~KMOD_LSHIFT;
   1.183 +            keyboard->modstate &= ~KMOD_LSHIFT;
   1.184              break;
   1.185          case SDLK_RSHIFT:
   1.186 -            modstate &= ~KMOD_RSHIFT;
   1.187 +            keyboard->modstate &= ~KMOD_RSHIFT;
   1.188              break;
   1.189          case SDLK_LALT:
   1.190 -            modstate &= ~KMOD_LALT;
   1.191 +            keyboard->modstate &= ~KMOD_LALT;
   1.192              break;
   1.193          case SDLK_RALT:
   1.194 -            modstate &= ~KMOD_RALT;
   1.195 +            keyboard->modstate &= ~KMOD_RALT;
   1.196              break;
   1.197          case SDLK_LMETA:
   1.198 -            modstate &= ~KMOD_LMETA;
   1.199 +            keyboard->modstate &= ~KMOD_LMETA;
   1.200              break;
   1.201          case SDLK_RMETA:
   1.202 -            modstate &= ~KMOD_RMETA;
   1.203 +            keyboard->modstate &= ~KMOD_RMETA;
   1.204              break;
   1.205          case SDLK_MODE:
   1.206 -            modstate &= ~KMOD_MODE;
   1.207 +            keyboard->modstate &= ~KMOD_MODE;
   1.208              break;
   1.209          default:
   1.210              break;
   1.211          }
   1.212 -        keysym->mod = modstate;
   1.213 +        modstate = keyboard->modstate;
   1.214      }
   1.215  
   1.216      /* Figure out what type of event this is */
   1.217 @@ -588,7 +618,7 @@
   1.218           * jk 991215 - Added
   1.219           */
   1.220          if (keyboard->repeat.timestamp &&
   1.221 -            keyboard->repeat.evt.key.keysym.sym == keysym->sym) {
   1.222 +            keyboard->repeat.evt.key.keysym.sym == key) {
   1.223              keyboard->repeat.timestamp = 0;
   1.224          }
   1.225          break;
   1.226 @@ -597,9 +627,9 @@
   1.227          return 0;
   1.228      }
   1.229  
   1.230 -    if (keysym->sym != SDLK_UNKNOWN) {
   1.231 +    if (key != SDLK_UNKNOWN) {
   1.232          /* Drop events that don't change state */
   1.233 -        if (keyboard->keystate[keysym->sym] == state) {
   1.234 +        if (keyboard->keystate[key] == state) {
   1.235  #if 0
   1.236              printf("Keyboard event didn't change state - dropped!\n");
   1.237  #endif
   1.238 @@ -607,8 +637,7 @@
   1.239          }
   1.240  
   1.241          /* Update internal keyboard state */
   1.242 -        keyboard->modstate = modstate;
   1.243 -        keyboard->keystate[keysym->sym] = state;
   1.244 +        keyboard->keystate[key] = state;
   1.245      }
   1.246  
   1.247      /* Post the event, if desired */
   1.248 @@ -618,7 +647,10 @@
   1.249          event.key.type = type;
   1.250          event.key.which = (Uint8) index;
   1.251          event.key.state = state;
   1.252 -        event.key.keysym = *keysym;
   1.253 +        event.key.keysym.scancode = scancode;
   1.254 +        event.key.keysym.sym = (Uint16) key;
   1.255 +        event.key.keysym.mod = modstate;
   1.256 +        event.key.keysym.unicode = 0;
   1.257          event.key.windowID = keyboard->focus;
   1.258          /*
   1.259           * jk 991215 - Added
   1.260 @@ -640,6 +672,32 @@
   1.261      return (posted);
   1.262  }
   1.263  
   1.264 +int
   1.265 +SDL_SendKeyboardText(int index, const char *text)
   1.266 +{
   1.267 +    SDL_Keyboard *keyboard = SDL_GetKeyboard(index);
   1.268 +    int posted;
   1.269 +
   1.270 +    if (!keyboard) {
   1.271 +        return 0;
   1.272 +    }
   1.273 +
   1.274 +    /* Post the event, if desired */
   1.275 +    posted = 0;
   1.276 +    if (SDL_ProcessEvents[SDL_TEXTINPUT] == SDL_ENABLE) {
   1.277 +        SDL_Event event;
   1.278 +        event.text.type = SDL_TEXTINPUT;
   1.279 +        event.text.which = (Uint8) index;
   1.280 +        SDL_strlcpy(event.text.text, text, SDL_arraysize(event.text.text));
   1.281 +        event.key.windowID = keyboard->focus;
   1.282 +        if ((SDL_EventOK == NULL) || SDL_EventOK(SDL_EventOKParam, &event)) {
   1.283 +            posted = 1;
   1.284 +            SDL_PushEvent(&event);
   1.285 +        }
   1.286 +    }
   1.287 +    return (posted);
   1.288 +}
   1.289 +
   1.290  /*
   1.291   * jk 991215 - Added
   1.292   */