comparison src/events/SDL_keyboard.c @ 2129:047245361002

Key repeat is handled by the OS, since text input is now decoupled from physical key events.
author Sam Lantinga <slouken@libsdl.org>
date Sat, 16 Jun 2007 15:32:04 +0000
parents 25d6537feea4
children 22342048bcb8
comparison
equal deleted inserted replaced
2128:04e9ad5318dc 2129:047245361002
347 for (key = SDLK_FIRST; key < SDLK_LAST; ++key) { 347 for (key = SDLK_FIRST; key < SDLK_LAST; ++key) {
348 if (keyboard->keystate[key] == SDL_PRESSED) { 348 if (keyboard->keystate[key] == SDL_PRESSED) {
349 SDL_SendKeyboardKey(index, SDL_RELEASED, 0, key); 349 SDL_SendKeyboardKey(index, SDL_RELEASED, 0, key);
350 } 350 }
351 } 351 }
352 keyboard->repeat.timestamp = 0;
353 } 352 }
354 353
355 void 354 void
356 SDL_KeyboardQuit(void) 355 SDL_KeyboardQuit(void)
357 { 356 {
513 512
514 int 513 int
515 SDL_SendKeyboardKey(int index, Uint8 state, Uint8 scancode, SDLKey key) 514 SDL_SendKeyboardKey(int index, Uint8 state, Uint8 scancode, SDLKey key)
516 { 515 {
517 SDL_Keyboard *keyboard = SDL_GetKeyboard(index); 516 SDL_Keyboard *keyboard = SDL_GetKeyboard(index);
518 int posted, repeatable; 517 int posted;
519 Uint16 modstate; 518 Uint16 modstate;
520 Uint8 type; 519 Uint8 type;
521 520
522 if (!keyboard) { 521 if (!keyboard) {
523 return 0; 522 return 0;
524 } 523 }
525 #if 0 524 #if 0
526 printf("The '%s' key has been %s\n", SDL_GetKeyName(key), 525 printf("The '%s' key has been %s\n", SDL_GetKeyName(key),
527 state == SDL_PRESSED ? "pressed" : "released"); 526 state == SDL_PRESSED ? "pressed" : "released");
528 #endif 527 #endif
529 repeatable = 0;
530 if (state == SDL_PRESSED) { 528 if (state == SDL_PRESSED) {
531 modstate = keyboard->modstate; 529 modstate = keyboard->modstate;
532 switch (key) { 530 switch (key) {
533 case SDLK_UNKNOWN: 531 case SDLK_UNKNOWN:
534 break; 532 break;
564 break; 562 break;
565 case SDLK_MODE: 563 case SDLK_MODE:
566 keyboard->modstate |= KMOD_MODE; 564 keyboard->modstate |= KMOD_MODE;
567 break; 565 break;
568 default: 566 default:
569 repeatable = 1;
570 break; 567 break;
571 } 568 }
572 } else { 569 } else {
573 switch (key) { 570 switch (key) {
574 case SDLK_UNKNOWN: 571 case SDLK_UNKNOWN:
614 case SDL_PRESSED: 611 case SDL_PRESSED:
615 type = SDL_KEYDOWN; 612 type = SDL_KEYDOWN;
616 break; 613 break;
617 case SDL_RELEASED: 614 case SDL_RELEASED:
618 type = SDL_KEYUP; 615 type = SDL_KEYUP;
619 /*
620 * jk 991215 - Added
621 */
622 if (keyboard->repeat.timestamp &&
623 keyboard->repeat.evt.key.keysym.sym == key) {
624 keyboard->repeat.timestamp = 0;
625 }
626 break; 616 break;
627 default: 617 default:
628 /* Invalid state -- bail */ 618 /* Invalid state -- bail */
629 return 0; 619 return 0;
630 } 620 }
652 event.key.keysym.scancode = scancode; 642 event.key.keysym.scancode = scancode;
653 event.key.keysym.sym = (Uint16) key; 643 event.key.keysym.sym = (Uint16) key;
654 event.key.keysym.mod = modstate; 644 event.key.keysym.mod = modstate;
655 event.key.keysym.unicode = 0; 645 event.key.keysym.unicode = 0;
656 event.key.windowID = keyboard->focus; 646 event.key.windowID = keyboard->focus;
657 /* FIXME: This doesn't make sense anymore... */
658 /*
659 * jk 991215 - Added
660 */
661 if (repeatable && (keyboard->repeat.delay != 0)) {
662 Uint32 timestamp = SDL_GetTicks();
663 if (!timestamp) {
664 timestamp = 1;
665 }
666 keyboard->repeat.evt = event;
667 keyboard->repeat.firsttime = 1;
668 keyboard->repeat.timestamp = 1;
669 }
670 posted = (SDL_PushEvent(&event) > 0); 647 posted = (SDL_PushEvent(&event) > 0);
671 } 648 }
672 return (posted); 649 return (posted);
673 } 650 }
674 651
693 posted = (SDL_PushEvent(&event) > 0); 670 posted = (SDL_PushEvent(&event) > 0);
694 } 671 }
695 return (posted); 672 return (posted);
696 } 673 }
697 674
698 /*
699 * jk 991215 - Added
700 */
701 void
702 SDL_CheckKeyRepeat(void)
703 {
704 int i;
705
706 for (i = 0; i < SDL_num_keyboards; ++i) {
707 SDL_Keyboard *keyboard = SDL_keyboards[i];
708
709 if (!keyboard) {
710 continue;
711 }
712
713 if (keyboard->repeat.timestamp) {
714 Uint32 now, interval;
715
716 now = SDL_GetTicks();
717 interval = (now - keyboard->repeat.timestamp);
718 if (keyboard->repeat.firsttime) {
719 if (interval > (Uint32) keyboard->repeat.delay) {
720 keyboard->repeat.timestamp = now;
721 keyboard->repeat.firsttime = 0;
722 }
723 } else {
724 if (interval > (Uint32) keyboard->repeat.interval) {
725 keyboard->repeat.timestamp = now;
726 SDL_PushEvent(&keyboard->repeat.evt);
727 }
728 }
729 }
730 }
731 }
732
733 int
734 SDL_EnableKeyRepeat(int delay, int interval)
735 {
736 SDL_Keyboard *keyboard = SDL_GetKeyboard(SDL_current_keyboard);
737
738 if (!keyboard) {
739 SDL_SetError("No keyboard is currently selected");
740 return -1;
741 }
742
743 if ((delay < 0) || (interval < 0)) {
744 SDL_SetError("keyboard repeat value less than zero");
745 return -1;
746 }
747
748 keyboard->repeat.firsttime = 0;
749 keyboard->repeat.delay = delay;
750 keyboard->repeat.interval = interval;
751 keyboard->repeat.timestamp = 0;
752
753 return 0;
754 }
755
756 void
757 SDL_GetKeyRepeat(int *delay, int *interval)
758 {
759 SDL_Keyboard *keyboard = SDL_GetKeyboard(SDL_current_keyboard);
760
761 if (!keyboard) {
762 if (delay) {
763 *delay = 0;
764 }
765 if (interval) {
766 *interval = 0;
767 }
768 return;
769 }
770 if (delay) {
771 *delay = keyboard->repeat.delay;
772 }
773 if (interval) {
774 *interval = keyboard->repeat.interval;
775 }
776 }
777
778 /* vi: set ts=4 sw=4 expandtab: */ 675 /* vi: set ts=4 sw=4 expandtab: */