src/events/SDL_keyboard.c
branchSDL-1.3
changeset 1668 4da1ee79c9af
parent 1662 782fd950bd46
child 1670 eef792d31de8
equal deleted inserted replaced
1667:1fddae038bc8 1668:4da1ee79c9af
    49     SDL_Event evt;              /* the event we are supposed to repeat */
    49     SDL_Event evt;              /* the event we are supposed to repeat */
    50 } SDL_KeyRepeat;
    50 } SDL_KeyRepeat;
    51 
    51 
    52 /* Public functions */
    52 /* Public functions */
    53 int
    53 int
    54 SDL_KeyboardInit (void)
    54 SDL_KeyboardInit(void)
    55 {
    55 {
    56     SDL_VideoDevice *_this = SDL_GetVideoDevice ();
    56     SDL_VideoDevice *_this = SDL_GetVideoDevice();
    57 
    57 
    58     /* Set default mode of UNICODE translation */
    58     /* Set default mode of UNICODE translation */
    59     SDL_EnableUNICODE (DEFAULT_UNICODE_TRANSLATION);
    59     SDL_EnableUNICODE(DEFAULT_UNICODE_TRANSLATION);
    60 
    60 
    61     /* Initialize the tables */
    61     /* Initialize the tables */
    62     SDL_ModState = KMOD_NONE;
    62     SDL_ModState = KMOD_NONE;
    63     SDL_memset ((void *) keynames, 0, sizeof (keynames));
    63     SDL_memset((void *) keynames, 0, sizeof(keynames));
    64     SDL_memset (SDL_KeyState, 0, sizeof (SDL_KeyState));
    64     SDL_memset(SDL_KeyState, 0, sizeof(SDL_KeyState));
    65     _this->InitOSKeymap (_this);
    65     _this->InitOSKeymap(_this);
    66 
    66 
    67     SDL_EnableKeyRepeat (0, 0);
    67     SDL_EnableKeyRepeat(0, 0);
    68 
    68 
    69     /* Fill in the blanks in keynames */
    69     /* Fill in the blanks in keynames */
    70     keynames[SDLK_BACKSPACE] = "backspace";
    70     keynames[SDLK_BACKSPACE] = "backspace";
    71     keynames[SDLK_TAB] = "tab";
    71     keynames[SDLK_TAB] = "tab";
    72     keynames[SDLK_CLEAR] = "clear";
    72     keynames[SDLK_CLEAR] = "clear";
   309     /* Done.  Whew. */
   309     /* Done.  Whew. */
   310     return (0);
   310     return (0);
   311 }
   311 }
   312 
   312 
   313 void
   313 void
   314 SDL_KeyboardQuit (void)
   314 SDL_KeyboardQuit(void)
   315 {
   315 {
   316 }
   316 }
   317 
   317 
   318 /* We lost the keyboard, so post key up messages for all pressed keys */
   318 /* We lost the keyboard, so post key up messages for all pressed keys */
   319 void
   319 void
   320 SDL_ResetKeyboard (void)
   320 SDL_ResetKeyboard(void)
   321 {
   321 {
   322     SDL_keysym keysym;
   322     SDL_keysym keysym;
   323     SDLKey key;
   323     SDLKey key;
   324 
   324 
   325     SDL_memset (&keysym, 0, (sizeof keysym));
   325     SDL_memset(&keysym, 0, (sizeof keysym));
   326     for (key = SDLK_FIRST; key < SDLK_LAST; ++key) {
   326     for (key = SDLK_FIRST; key < SDLK_LAST; ++key) {
   327         if (SDL_KeyState[key] == SDL_PRESSED) {
   327         if (SDL_KeyState[key] == SDL_PRESSED) {
   328             keysym.sym = key;
   328             keysym.sym = key;
   329             SDL_PrivateKeyboard (SDL_RELEASED, &keysym);
   329             SDL_PrivateKeyboard(SDL_RELEASED, &keysym);
   330         }
   330         }
   331     }
   331     }
   332     SDL_KeyRepeat.timestamp = 0;
   332     SDL_KeyRepeat.timestamp = 0;
   333 }
   333 }
   334 
   334 
   335 int
   335 int
   336 SDL_EnableUNICODE (int enable)
   336 SDL_EnableUNICODE(int enable)
   337 {
   337 {
   338     int old_mode;
   338     int old_mode;
   339 
   339 
   340     old_mode = SDL_TranslateUNICODE;
   340     old_mode = SDL_TranslateUNICODE;
   341     if (enable >= 0) {
   341     if (enable >= 0) {
   343     }
   343     }
   344     return (old_mode);
   344     return (old_mode);
   345 }
   345 }
   346 
   346 
   347 Uint8 *
   347 Uint8 *
   348 SDL_GetKeyState (int *numkeys)
   348 SDL_GetKeyState(int *numkeys)
   349 {
   349 {
   350     if (numkeys != (int *) 0)
   350     if (numkeys != (int *) 0)
   351         *numkeys = SDLK_LAST;
   351         *numkeys = SDLK_LAST;
   352     return (SDL_KeyState);
   352     return (SDL_KeyState);
   353 }
   353 }
   354 
   354 
   355 SDLMod
   355 SDLMod
   356 SDL_GetModState (void)
   356 SDL_GetModState(void)
   357 {
   357 {
   358     return (SDL_ModState);
   358     return (SDL_ModState);
   359 }
   359 }
   360 
   360 
   361 void
   361 void
   362 SDL_SetModState (SDLMod modstate)
   362 SDL_SetModState(SDLMod modstate)
   363 {
   363 {
   364     SDL_ModState = modstate;
   364     SDL_ModState = modstate;
   365 }
   365 }
   366 
   366 
   367 char *
   367 char *
   368 SDL_GetKeyName (SDLKey key)
   368 SDL_GetKeyName(SDLKey key)
   369 {
   369 {
   370     const char *keyname;
   370     const char *keyname;
   371 
   371 
   372     keyname = NULL;
   372     keyname = NULL;
   373     if (key < SDLK_LAST) {
   373     if (key < SDLK_LAST) {
   380     return (char *) (keyname);
   380     return (char *) (keyname);
   381 }
   381 }
   382 
   382 
   383 /* These are global for SDL_eventloop.c */
   383 /* These are global for SDL_eventloop.c */
   384 int
   384 int
   385 SDL_PrivateKeyboard (Uint8 state, SDL_keysym * keysym)
   385 SDL_PrivateKeyboard(Uint8 state, SDL_keysym * keysym)
   386 {
   386 {
   387     SDL_Event event;
   387     SDL_Event event;
   388     int posted, repeatable;
   388     int posted, repeatable;
   389     Uint16 modstate;
   389     Uint16 modstate;
   390 
   390 
   391     SDL_memset (&event, 0, sizeof (event));
   391     SDL_memset(&event, 0, sizeof(event));
   392 
   392 
   393 #if 0
   393 #if 0
   394     printf ("The '%s' key has been %s\n", SDL_GetKeyName (keysym->sym),
   394     printf("The '%s' key has been %s\n", SDL_GetKeyName(keysym->sym),
   395             state == SDL_PRESSED ? "pressed" : "released");
   395            state == SDL_PRESSED ? "pressed" : "released");
   396 #endif
   396 #endif
   397     /* Set up the keysym */
   397     /* Set up the keysym */
   398     modstate = (Uint16) SDL_ModState;
   398     modstate = (Uint16) SDL_ModState;
   399 
   399 
   400     repeatable = 0;
   400     repeatable = 0;
   510 
   510 
   511     if (keysym->sym != SDLK_UNKNOWN) {
   511     if (keysym->sym != SDLK_UNKNOWN) {
   512         /* Drop events that don't change state */
   512         /* Drop events that don't change state */
   513         if (SDL_KeyState[keysym->sym] == state) {
   513         if (SDL_KeyState[keysym->sym] == state) {
   514 #if 0
   514 #if 0
   515             printf ("Keyboard event didn't change state - dropped!\n");
   515             printf("Keyboard event didn't change state - dropped!\n");
   516 #endif
   516 #endif
   517             return (0);
   517             return (0);
   518         }
   518         }
   519 
   519 
   520         /* Update internal keyboard state */
   520         /* Update internal keyboard state */
   531          * jk 991215 - Added
   531          * jk 991215 - Added
   532          */
   532          */
   533         if (repeatable && (SDL_KeyRepeat.delay != 0)) {
   533         if (repeatable && (SDL_KeyRepeat.delay != 0)) {
   534             SDL_KeyRepeat.evt = event;
   534             SDL_KeyRepeat.evt = event;
   535             SDL_KeyRepeat.firsttime = 1;
   535             SDL_KeyRepeat.firsttime = 1;
   536             SDL_KeyRepeat.timestamp = SDL_GetTicks ();
   536             SDL_KeyRepeat.timestamp = SDL_GetTicks();
   537         }
   537         }
   538         if ((SDL_EventOK == NULL) || SDL_EventOK (&event)) {
   538         if ((SDL_EventOK == NULL) || SDL_EventOK(&event)) {
   539             posted = 1;
   539             posted = 1;
   540             SDL_PushEvent (&event);
   540             SDL_PushEvent(&event);
   541         }
   541         }
   542     }
   542     }
   543     return (posted);
   543     return (posted);
   544 }
   544 }
   545 
   545 
   546 /*
   546 /*
   547  * jk 991215 - Added
   547  * jk 991215 - Added
   548  */
   548  */
   549 void
   549 void
   550 SDL_CheckKeyRepeat (void)
   550 SDL_CheckKeyRepeat(void)
   551 {
   551 {
   552     if (SDL_KeyRepeat.timestamp) {
   552     if (SDL_KeyRepeat.timestamp) {
   553         Uint32 now, interval;
   553         Uint32 now, interval;
   554 
   554 
   555         now = SDL_GetTicks ();
   555         now = SDL_GetTicks();
   556         interval = (now - SDL_KeyRepeat.timestamp);
   556         interval = (now - SDL_KeyRepeat.timestamp);
   557         if (SDL_KeyRepeat.firsttime) {
   557         if (SDL_KeyRepeat.firsttime) {
   558             if (interval > (Uint32) SDL_KeyRepeat.delay) {
   558             if (interval > (Uint32) SDL_KeyRepeat.delay) {
   559                 SDL_KeyRepeat.timestamp = now;
   559                 SDL_KeyRepeat.timestamp = now;
   560                 SDL_KeyRepeat.firsttime = 0;
   560                 SDL_KeyRepeat.firsttime = 0;
   561             }
   561             }
   562         } else {
   562         } else {
   563             if (interval > (Uint32) SDL_KeyRepeat.interval) {
   563             if (interval > (Uint32) SDL_KeyRepeat.interval) {
   564                 SDL_KeyRepeat.timestamp = now;
   564                 SDL_KeyRepeat.timestamp = now;
   565                 if ((SDL_EventOK == NULL)
   565                 if ((SDL_EventOK == NULL)
   566                     || SDL_EventOK (&SDL_KeyRepeat.evt)) {
   566                     || SDL_EventOK(&SDL_KeyRepeat.evt)) {
   567                     SDL_PushEvent (&SDL_KeyRepeat.evt);
   567                     SDL_PushEvent(&SDL_KeyRepeat.evt);
   568                 }
   568                 }
   569             }
   569             }
   570         }
   570         }
   571     }
   571     }
   572 }
   572 }
   573 
   573 
   574 int
   574 int
   575 SDL_EnableKeyRepeat (int delay, int interval)
   575 SDL_EnableKeyRepeat(int delay, int interval)
   576 {
   576 {
   577     if ((delay < 0) || (interval < 0)) {
   577     if ((delay < 0) || (interval < 0)) {
   578         SDL_SetError ("keyboard repeat value less than zero");
   578         SDL_SetError("keyboard repeat value less than zero");
   579         return (-1);
   579         return (-1);
   580     }
   580     }
   581     SDL_KeyRepeat.firsttime = 0;
   581     SDL_KeyRepeat.firsttime = 0;
   582     SDL_KeyRepeat.delay = delay;
   582     SDL_KeyRepeat.delay = delay;
   583     SDL_KeyRepeat.interval = interval;
   583     SDL_KeyRepeat.interval = interval;
   584     SDL_KeyRepeat.timestamp = 0;
   584     SDL_KeyRepeat.timestamp = 0;
   585     return (0);
   585     return (0);
   586 }
   586 }
   587 
   587 
   588 void
   588 void
   589 SDL_GetKeyRepeat (int *delay, int *interval)
   589 SDL_GetKeyRepeat(int *delay, int *interval)
   590 {
   590 {
   591     *delay = SDL_KeyRepeat.delay;
   591     *delay = SDL_KeyRepeat.delay;
   592     *interval = SDL_KeyRepeat.interval;
   592     *interval = SDL_KeyRepeat.interval;
   593 }
   593 }
   594 
   594