comparison src/events/SDL_keyboard.c @ 1724:6c63fc2bd986 SDL-1.3

Proof of concept done - Win32 GDI implementation mostly complete.
author Sam Lantinga <slouken@libsdl.org>
date Thu, 06 Jul 2006 07:17:11 +0000
parents 5daa04d862f1
children 875c3cf1a12c
comparison
equal deleted inserted replaced
1723:4bdbb9b2bd0a 1724:6c63fc2bd986
336 336
337 void 337 void
338 SDL_ResetKeyboard(int index) 338 SDL_ResetKeyboard(int index)
339 { 339 {
340 SDL_Keyboard *keyboard = SDL_GetKeyboard(index); 340 SDL_Keyboard *keyboard = SDL_GetKeyboard(index);
341 SDL_keysym keysym; 341 SDLKey key;
342 Uint16 key;
343 342
344 if (!keyboard) { 343 if (!keyboard) {
345 return; 344 return;
346 } 345 }
347 346
348 SDL_memset(&keysym, 0, (sizeof keysym));
349 for (key = SDLK_FIRST; key < SDLK_LAST; ++key) { 347 for (key = SDLK_FIRST; key < SDLK_LAST; ++key) {
350 if (keyboard->keystate[key] == SDL_PRESSED) { 348 if (keyboard->keystate[key] == SDL_PRESSED) {
351 keysym.sym = key; 349 SDL_SendKeyboardKey(index, SDL_RELEASED, 0, key);
352 SDL_SendKeyboardKey(index, 0, SDL_RELEASED, &keysym);
353 } 350 }
354 } 351 }
355 keyboard->repeat.timestamp = 0; 352 keyboard->repeat.timestamp = 0;
356 } 353 }
357 354
461 } 458 }
462 } 459 }
463 return keyname; 460 return keyname;
464 } 461 }
465 462
463 void
464 SDL_SetKeyboardFocus(int index, SDL_WindowID windowID)
465 {
466 SDL_Keyboard *keyboard = SDL_GetKeyboard(index);
467 int i;
468 SDL_bool focus;
469
470 if (!keyboard || (keyboard->focus == windowID)) {
471 return;
472 }
473
474 /* See if the current window has lost focus */
475 if (keyboard->focus) {
476 focus = SDL_FALSE;
477 for (i = 0; i < SDL_num_keyboards; ++i) {
478 SDL_Keyboard *check;
479 if (i != index) {
480 check = SDL_GetKeyboard(i);
481 if (check && check->focus == keyboard->focus) {
482 focus = SDL_TRUE;
483 break;
484 }
485 }
486 }
487 if (!focus) {
488 SDL_SendWindowEvent(windowID, SDL_WINDOWEVENT_FOCUS_GAINED, 0, 0);
489 }
490 }
491
492 keyboard->focus = windowID;
493
494 if (keyboard->focus) {
495 focus = SDL_FALSE;
496 for (i = 0; i < SDL_num_keyboards; ++i) {
497 SDL_Keyboard *check;
498 if (i != index) {
499 check = SDL_GetKeyboard(i);
500 if (check && check->focus == keyboard->focus) {
501 focus = SDL_TRUE;
502 break;
503 }
504 }
505 }
506 if (!focus) {
507 SDL_SendWindowEvent(windowID, SDL_WINDOWEVENT_FOCUS_LOST, 0, 0);
508 }
509 }
510 }
511
466 int 512 int
467 SDL_SendKeyboardKey(int index, SDL_WindowID windowID, Uint8 state, 513 SDL_SendKeyboardKey(int index, Uint8 state, Uint8 scancode, SDLKey key)
468 SDL_keysym * keysym)
469 { 514 {
470 SDL_Keyboard *keyboard = SDL_GetKeyboard(index); 515 SDL_Keyboard *keyboard = SDL_GetKeyboard(index);
471 int posted, repeatable; 516 int posted, repeatable;
472 Uint16 modstate; 517 Uint16 modstate;
473 Uint8 type; 518 Uint8 type;
474 519
475 if (!keyboard) { 520 if (!keyboard) {
476 return 0; 521 return 0;
477 } 522 }
478
479 if (windowID) {
480 keyboard->focus = windowID;
481 }
482 #if 0 523 #if 0
483 printf("The '%s' key has been %s\n", SDL_GetKeyName(keysym->sym), 524 printf("The '%s' key has been %s\n", SDL_GetKeyName(key),
484 state == SDL_PRESSED ? "pressed" : "released"); 525 state == SDL_PRESSED ? "pressed" : "released");
485 #endif 526 #endif
486 /* Set up the keysym */
487 modstate = keyboard->modstate;
488
489 repeatable = 0; 527 repeatable = 0;
490
491 if (state == SDL_PRESSED) { 528 if (state == SDL_PRESSED) {
492 keysym->mod = modstate; 529 modstate = keyboard->modstate;
493 switch (keysym->sym) { 530 switch (key) {
494 case SDLK_UNKNOWN: 531 case SDLK_UNKNOWN:
495 break; 532 break;
496 case SDLK_NUMLOCK: 533 case SDLK_NUMLOCK:
497 modstate ^= KMOD_NUM; 534 keyboard->modstate ^= KMOD_NUM;
498 if (!(modstate & KMOD_NUM))
499 state = SDL_RELEASED;
500 keysym->mod = modstate;
501 break; 535 break;
502 case SDLK_CAPSLOCK: 536 case SDLK_CAPSLOCK:
503 modstate ^= KMOD_CAPS; 537 keyboard->modstate ^= KMOD_CAPS;
504 if (!(modstate & KMOD_CAPS))
505 state = SDL_RELEASED;
506 keysym->mod = modstate;
507 break; 538 break;
508 case SDLK_LCTRL: 539 case SDLK_LCTRL:
509 modstate |= KMOD_LCTRL; 540 keyboard->modstate |= KMOD_LCTRL;
510 break; 541 break;
511 case SDLK_RCTRL: 542 case SDLK_RCTRL:
512 modstate |= KMOD_RCTRL; 543 keyboard->modstate |= KMOD_RCTRL;
513 break; 544 break;
514 case SDLK_LSHIFT: 545 case SDLK_LSHIFT:
515 modstate |= KMOD_LSHIFT; 546 keyboard->modstate |= KMOD_LSHIFT;
516 break; 547 break;
517 case SDLK_RSHIFT: 548 case SDLK_RSHIFT:
518 modstate |= KMOD_RSHIFT; 549 keyboard->modstate |= KMOD_RSHIFT;
519 break; 550 break;
520 case SDLK_LALT: 551 case SDLK_LALT:
521 modstate |= KMOD_LALT; 552 keyboard->modstate |= KMOD_LALT;
522 break; 553 break;
523 case SDLK_RALT: 554 case SDLK_RALT:
524 modstate |= KMOD_RALT; 555 keyboard->modstate |= KMOD_RALT;
525 break; 556 break;
526 case SDLK_LMETA: 557 case SDLK_LMETA:
527 modstate |= KMOD_LMETA; 558 keyboard->modstate |= KMOD_LMETA;
528 break; 559 break;
529 case SDLK_RMETA: 560 case SDLK_RMETA:
530 modstate |= KMOD_RMETA; 561 keyboard->modstate |= KMOD_RMETA;
531 break; 562 break;
532 case SDLK_MODE: 563 case SDLK_MODE:
533 modstate |= KMOD_MODE; 564 keyboard->modstate |= KMOD_MODE;
534 break; 565 break;
535 default: 566 default:
536 repeatable = 1; 567 repeatable = 1;
537 break; 568 break;
538 } 569 }
539 } else { 570 } else {
540 switch (keysym->sym) { 571 switch (key) {
541 case SDLK_UNKNOWN: 572 case SDLK_UNKNOWN:
542 break; 573 break;
543 case SDLK_NUMLOCK: 574 case SDLK_NUMLOCK:
544 case SDLK_CAPSLOCK: 575 case SDLK_CAPSLOCK:
545 /* Only send keydown events */ 576 break;
546 return (0);
547 case SDLK_LCTRL: 577 case SDLK_LCTRL:
548 modstate &= ~KMOD_LCTRL; 578 keyboard->modstate &= ~KMOD_LCTRL;
549 break; 579 break;
550 case SDLK_RCTRL: 580 case SDLK_RCTRL:
551 modstate &= ~KMOD_RCTRL; 581 keyboard->modstate &= ~KMOD_RCTRL;
552 break; 582 break;
553 case SDLK_LSHIFT: 583 case SDLK_LSHIFT:
554 modstate &= ~KMOD_LSHIFT; 584 keyboard->modstate &= ~KMOD_LSHIFT;
555 break; 585 break;
556 case SDLK_RSHIFT: 586 case SDLK_RSHIFT:
557 modstate &= ~KMOD_RSHIFT; 587 keyboard->modstate &= ~KMOD_RSHIFT;
558 break; 588 break;
559 case SDLK_LALT: 589 case SDLK_LALT:
560 modstate &= ~KMOD_LALT; 590 keyboard->modstate &= ~KMOD_LALT;
561 break; 591 break;
562 case SDLK_RALT: 592 case SDLK_RALT:
563 modstate &= ~KMOD_RALT; 593 keyboard->modstate &= ~KMOD_RALT;
564 break; 594 break;
565 case SDLK_LMETA: 595 case SDLK_LMETA:
566 modstate &= ~KMOD_LMETA; 596 keyboard->modstate &= ~KMOD_LMETA;
567 break; 597 break;
568 case SDLK_RMETA: 598 case SDLK_RMETA:
569 modstate &= ~KMOD_RMETA; 599 keyboard->modstate &= ~KMOD_RMETA;
570 break; 600 break;
571 case SDLK_MODE: 601 case SDLK_MODE:
572 modstate &= ~KMOD_MODE; 602 keyboard->modstate &= ~KMOD_MODE;
573 break; 603 break;
574 default: 604 default:
575 break; 605 break;
576 } 606 }
577 keysym->mod = modstate; 607 modstate = keyboard->modstate;
578 } 608 }
579 609
580 /* Figure out what type of event this is */ 610 /* Figure out what type of event this is */
581 switch (state) { 611 switch (state) {
582 case SDL_PRESSED: 612 case SDL_PRESSED:
586 type = SDL_KEYUP; 616 type = SDL_KEYUP;
587 /* 617 /*
588 * jk 991215 - Added 618 * jk 991215 - Added
589 */ 619 */
590 if (keyboard->repeat.timestamp && 620 if (keyboard->repeat.timestamp &&
591 keyboard->repeat.evt.key.keysym.sym == keysym->sym) { 621 keyboard->repeat.evt.key.keysym.sym == key) {
592 keyboard->repeat.timestamp = 0; 622 keyboard->repeat.timestamp = 0;
593 } 623 }
594 break; 624 break;
595 default: 625 default:
596 /* Invalid state -- bail */ 626 /* Invalid state -- bail */
597 return 0; 627 return 0;
598 } 628 }
599 629
600 if (keysym->sym != SDLK_UNKNOWN) { 630 if (key != SDLK_UNKNOWN) {
601 /* Drop events that don't change state */ 631 /* Drop events that don't change state */
602 if (keyboard->keystate[keysym->sym] == state) { 632 if (keyboard->keystate[key] == state) {
603 #if 0 633 #if 0
604 printf("Keyboard event didn't change state - dropped!\n"); 634 printf("Keyboard event didn't change state - dropped!\n");
605 #endif 635 #endif
606 return 0; 636 return 0;
607 } 637 }
608 638
609 /* Update internal keyboard state */ 639 /* Update internal keyboard state */
610 keyboard->modstate = modstate; 640 keyboard->keystate[key] = state;
611 keyboard->keystate[keysym->sym] = state;
612 } 641 }
613 642
614 /* Post the event, if desired */ 643 /* Post the event, if desired */
615 posted = 0; 644 posted = 0;
616 if (SDL_ProcessEvents[type] == SDL_ENABLE) { 645 if (SDL_ProcessEvents[type] == SDL_ENABLE) {
617 SDL_Event event; 646 SDL_Event event;
618 event.key.type = type; 647 event.key.type = type;
619 event.key.which = (Uint8) index; 648 event.key.which = (Uint8) index;
620 event.key.state = state; 649 event.key.state = state;
621 event.key.keysym = *keysym; 650 event.key.keysym.scancode = scancode;
651 event.key.keysym.sym = (Uint16) key;
652 event.key.keysym.mod = modstate;
653 event.key.keysym.unicode = 0;
622 event.key.windowID = keyboard->focus; 654 event.key.windowID = keyboard->focus;
623 /* 655 /*
624 * jk 991215 - Added 656 * jk 991215 - Added
625 */ 657 */
626 if (repeatable && (keyboard->repeat.delay != 0)) { 658 if (repeatable && (keyboard->repeat.delay != 0)) {
630 } 662 }
631 keyboard->repeat.evt = event; 663 keyboard->repeat.evt = event;
632 keyboard->repeat.firsttime = 1; 664 keyboard->repeat.firsttime = 1;
633 keyboard->repeat.timestamp = 1; 665 keyboard->repeat.timestamp = 1;
634 } 666 }
667 if ((SDL_EventOK == NULL) || SDL_EventOK(SDL_EventOKParam, &event)) {
668 posted = 1;
669 SDL_PushEvent(&event);
670 }
671 }
672 return (posted);
673 }
674
675 int
676 SDL_SendKeyboardText(int index, const char *text)
677 {
678 SDL_Keyboard *keyboard = SDL_GetKeyboard(index);
679 int posted;
680
681 if (!keyboard) {
682 return 0;
683 }
684
685 /* Post the event, if desired */
686 posted = 0;
687 if (SDL_ProcessEvents[SDL_TEXTINPUT] == SDL_ENABLE) {
688 SDL_Event event;
689 event.text.type = SDL_TEXTINPUT;
690 event.text.which = (Uint8) index;
691 SDL_strlcpy(event.text.text, text, SDL_arraysize(event.text.text));
692 event.key.windowID = keyboard->focus;
635 if ((SDL_EventOK == NULL) || SDL_EventOK(SDL_EventOKParam, &event)) { 693 if ((SDL_EventOK == NULL) || SDL_EventOK(SDL_EventOKParam, &event)) {
636 posted = 1; 694 posted = 1;
637 SDL_PushEvent(&event); 695 SDL_PushEvent(&event);
638 } 696 }
639 } 697 }