src/video/cocoa/SDL_cocoakeyboard.m
changeset 4560 95352c671a6e
parent 4498 3d91e31fcf71
child 4565 e2d46c5c7483
equal deleted inserted replaced
4559:f8c3870af5a2 4560:95352c671a6e
   217         newMask = newMods & bit;
   217         newMask = newMods & bit;
   218 
   218 
   219         if (oldMask && oldMask != newMask) {        /* modifier up event */
   219         if (oldMask && oldMask != newMask) {        /* modifier up event */
   220             /* If this was Caps Lock, we need some additional voodoo to make SDL happy */
   220             /* If this was Caps Lock, we need some additional voodoo to make SDL happy */
   221             if (bit == NSAlphaShiftKeyMask) {
   221             if (bit == NSAlphaShiftKeyMask) {
   222                 SDL_SendKeyboardKey(SDL_PRESSED, mapping[i]);
   222                 SDL_SendKeyboardKey(SDL_PRESSED, mapping[i], SDL_FALSE);
   223             }
   223             }
   224             SDL_SendKeyboardKey(SDL_RELEASED, mapping[i]);
   224             SDL_SendKeyboardKey(SDL_RELEASED, mapping[i], SDL_FALSE);
   225         } else if (newMask && oldMask != newMask) { /* modifier down event */
   225         } else if (newMask && oldMask != newMask) { /* modifier down event */
   226             SDL_SendKeyboardKey(SDL_PRESSED, mapping[i]);
   226             SDL_SendKeyboardKey(SDL_PRESSED, mapping[i], SDL_FALSE);
   227             /* If this was Caps Lock, we need some additional voodoo to make SDL happy */
   227             /* If this was Caps Lock, we need some additional voodoo to make SDL happy */
   228             if (bit == NSAlphaShiftKeyMask) {
   228             if (bit == NSAlphaShiftKeyMask) {
   229                 SDL_SendKeyboardKey(SDL_RELEASED, mapping[i]);
   229                 SDL_SendKeyboardKey(SDL_RELEASED, mapping[i], SDL_FALSE);
   230             }
   230             }
   231         }
   231         }
   232     }
   232     }
   233 }
   233 }
   234 
   234 
   249      */ 
   249      */ 
   250     oldMask = oldMods & device_independent_mask;
   250     oldMask = oldMods & device_independent_mask;
   251     newMask = newMods & device_independent_mask;
   251     newMask = newMods & device_independent_mask;
   252     
   252     
   253     if (oldMask && oldMask != newMask) {
   253     if (oldMask && oldMask != newMask) {
   254         SDL_SendKeyboardKey(SDL_RELEASED, scancode);
   254         SDL_SendKeyboardKey(SDL_RELEASED, scancode, SDL_FALSE);
   255     } else if (newMask && oldMask != newMask) {
   255     } else if (newMask && oldMask != newMask) {
   256         SDL_SendKeyboardKey(SDL_PRESSED, scancode);
   256         SDL_SendKeyboardKey(SDL_PRESSED, scancode, SDL_FALSE);
   257     }
   257     }
   258 }
   258 }
   259 
   259 
   260 /* This is a helper function for HandleModifierSide. 
   260 /* This is a helper function for HandleModifierSide. 
   261  * This function sets the actual SDL_PrivateKeyboard event.
   261  * This function sets the actual SDL_PrivateKeyboard event.
   276     /* We now know that this side bit flipped. But we don't know if
   276     /* We now know that this side bit flipped. But we don't know if
   277      * it went pressed to released or released to pressed, so we must 
   277      * it went pressed to released or released to pressed, so we must 
   278      * find out which it is.
   278      * find out which it is.
   279      */
   279      */
   280     if (new_dep_mask && old_dep_mask != new_dep_mask) {
   280     if (new_dep_mask && old_dep_mask != new_dep_mask) {
   281         SDL_SendKeyboardKey(SDL_PRESSED, scancode);
   281         SDL_SendKeyboardKey(SDL_PRESSED, scancode, SDL_FALSE);
   282     } else {
   282     } else {
   283         SDL_SendKeyboardKey(SDL_RELEASED, scancode);
   283         SDL_SendKeyboardKey(SDL_RELEASED, scancode, SDL_FALSE);
   284     }
   284     }
   285 }
   285 }
   286 
   286 
   287 /* This is a helper function for DoSidedModifiers.
   287 /* This is a helper function for DoSidedModifiers.
   288  * This function will figure out if the modifier key is the left or right side, 
   288  * This function will figure out if the modifier key is the left or right side, 
   349      */
   349      */
   350     if ((device_dependent_mask & oldMods) == 0) {
   350     if ((device_dependent_mask & oldMods) == 0) {
   351         /* In this case, we can't detect the keyboard, so use the left side 
   351         /* In this case, we can't detect the keyboard, so use the left side 
   352          * to represent both, and release it. 
   352          * to represent both, and release it. 
   353          */
   353          */
   354         SDL_SendKeyboardKey(SDL_RELEASED, left_scancode);
   354         SDL_SendKeyboardKey(SDL_RELEASED, left_scancode, SDL_FALSE);
   355         return;
   355         return;
   356     }
   356     }
   357 
   357 
   358     /* 
   358     /* 
   359      * This could have been done in an if-else case because at this point,
   359      * This could have been done in an if-else case because at this point,
   360      * we know that all keys have been released when calling this function. 
   360      * we know that all keys have been released when calling this function. 
   361      * But I'm being paranoid so I want to handle each separately,
   361      * But I'm being paranoid so I want to handle each separately,
   362      * so I hope this doesn't cause other problems.
   362      * so I hope this doesn't cause other problems.
   363      */
   363      */
   364     if ( left_device_dependent_mask & oldMods ) {
   364     if ( left_device_dependent_mask & oldMods ) {
   365         SDL_SendKeyboardKey(SDL_RELEASED, left_scancode);
   365         SDL_SendKeyboardKey(SDL_RELEASED, left_scancode, SDL_FALSE);
   366     }
   366     }
   367     if ( right_device_dependent_mask & oldMods ) {
   367     if ( right_device_dependent_mask & oldMods ) {
   368         SDL_SendKeyboardKey(SDL_RELEASED, right_scancode);
   368         SDL_SendKeyboardKey(SDL_RELEASED, right_scancode, SDL_FALSE);
   369     }
   369     }
   370 }
   370 }
   371 
   371 
   372 /* This is a helper function for DoSidedModifiers.
   372 /* This is a helper function for DoSidedModifiers.
   373  * This function handles the CapsLock case.
   373  * This function handles the CapsLock case.
   380     
   380     
   381     oldMask = oldMods & NSAlphaShiftKeyMask;
   381     oldMask = oldMods & NSAlphaShiftKeyMask;
   382     newMask = newMods & NSAlphaShiftKeyMask;
   382     newMask = newMods & NSAlphaShiftKeyMask;
   383 
   383 
   384     if (oldMask != newMask) {
   384     if (oldMask != newMask) {
   385         SDL_SendKeyboardKey(SDL_PRESSED, SDL_SCANCODE_CAPSLOCK);
   385         SDL_SendKeyboardKey(SDL_PRESSED, SDL_SCANCODE_CAPSLOCK, SDL_FALSE);
   386         SDL_SendKeyboardKey(SDL_RELEASED, SDL_SCANCODE_CAPSLOCK);
   386         SDL_SendKeyboardKey(SDL_RELEASED, SDL_SCANCODE_CAPSLOCK, SDL_FALSE);
   387     }
   387     }
   388 
   388 
   389     oldMask = oldMods & NSNumericPadKeyMask;
   389     oldMask = oldMods & NSNumericPadKeyMask;
   390     newMask = newMods & NSNumericPadKeyMask;
   390     newMask = newMods & NSNumericPadKeyMask;
   391 
   391 
   392     if (oldMask != newMask) {
   392     if (oldMask != newMask) {
   393         SDL_SendKeyboardKey(SDL_PRESSED, SDL_SCANCODE_NUMLOCKCLEAR);
   393         SDL_SendKeyboardKey(SDL_PRESSED, SDL_SCANCODE_NUMLOCKCLEAR, SDL_FALSE);
   394         SDL_SendKeyboardKey(SDL_RELEASED, SDL_SCANCODE_NUMLOCKCLEAR);
   394         SDL_SendKeyboardKey(SDL_RELEASED, SDL_SCANCODE_NUMLOCKCLEAR, SDL_FALSE);
   395     }
   395     }
   396 }
   396 }
   397 
   397 
   398 /* This function will handle the modifier keys and also determine the 
   398 /* This function will handle the modifier keys and also determine the 
   399  * correct side of the key.
   399  * correct side of the key.
   668 Cocoa_HandleKeyEvent(_THIS, NSEvent *event)
   668 Cocoa_HandleKeyEvent(_THIS, NSEvent *event)
   669 {
   669 {
   670     SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
   670     SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
   671     unsigned short scancode = [event keyCode];
   671     unsigned short scancode = [event keyCode];
   672     SDL_scancode code;
   672     SDL_scancode code;
       
   673     SDL_bool repeat;
   673 #if 0
   674 #if 0
   674     const char *text;
   675     const char *text;
   675 #endif
   676 #endif
   676 
   677 
   677     if ((scancode == 10 || scancode == 50) && KBGetLayoutType(LMGetKbdType()) == kKeyboardISO) {
   678     if ((scancode == 10 || scancode == 50) && KBGetLayoutType(LMGetKbdType()) == kKeyboardISO) {
   686         code = SDL_SCANCODE_UNKNOWN;
   687         code = SDL_SCANCODE_UNKNOWN;
   687     }
   688     }
   688 
   689 
   689     switch ([event type]) {
   690     switch ([event type]) {
   690     case NSKeyDown:
   691     case NSKeyDown:
   691         if (![event isARepeat]) {
   692         repeat = [event isARepeat] ? SDL_TRUE : SDL_FALSE;
       
   693         if (!repeat) {
   692             /* See if we need to rebuild the keyboard layout */
   694             /* See if we need to rebuild the keyboard layout */
   693             UpdateKeymap(data);
   695             UpdateKeymap(data);
   694 
   696         }
   695             SDL_SendKeyboardKey(SDL_PRESSED, code);
   697 
       
   698         SDL_SendKeyboardKey(SDL_PRESSED, code, repeat);
   696 #if 1
   699 #if 1
   697             if (code == SDL_SCANCODE_UNKNOWN) {
   700         if (code == SDL_SCANCODE_UNKNOWN) {
   698                 fprintf(stderr, "The key you just pressed is not recognized by SDL. To help get this fixed, report this to the SDL mailing list <sdl@libsdl.org> or to Christian Walther <cwalther@gmx.ch>. Mac virtual key code is %d.\n", scancode);
   701             fprintf(stderr, "The key you just pressed is not recognized by SDL. To help get this fixed, report this to the SDL mailing list <sdl@libsdl.org> or to Christian Walther <cwalther@gmx.ch>. Mac virtual key code is %d.\n", scancode);
   699             }
   702         }
   700 #endif
   703 #endif
   701         }
       
   702         if (SDL_EventState(SDL_TEXTINPUT, SDL_QUERY)) {
   704         if (SDL_EventState(SDL_TEXTINPUT, SDL_QUERY)) {
   703             /* FIXME CW 2007-08-16: only send those events to the field editor for which we actually want text events, not e.g. esc or function keys. Arrow keys in particular seem to produce crashes sometimes. */
   705             /* FIXME CW 2007-08-16: only send those events to the field editor for which we actually want text events, not e.g. esc or function keys. Arrow keys in particular seem to produce crashes sometimes. */
   704             [data->fieldEdit interpretKeyEvents:[NSArray arrayWithObject:event]];
   706             [data->fieldEdit interpretKeyEvents:[NSArray arrayWithObject:event]];
   705 #if 0
   707 #if 0
   706             text = [[event characters] UTF8String];
   708             text = [[event characters] UTF8String];
   710             }
   712             }
   711 #endif
   713 #endif
   712         }
   714         }
   713         break;
   715         break;
   714     case NSKeyUp:
   716     case NSKeyUp:
   715         SDL_SendKeyboardKey(SDL_RELEASED, code);
   717         SDL_SendKeyboardKey(SDL_RELEASED, code, SDL_FALSE);
   716         break;
   718         break;
   717     case NSFlagsChanged:
   719     case NSFlagsChanged:
   718         /* FIXME CW 2007-08-14: check if this whole mess that takes up half of this file is really necessary */
   720         /* FIXME CW 2007-08-14: check if this whole mess that takes up half of this file is really necessary */
   719         HandleModifiers(_this, scancode, [event modifierFlags]);
   721         HandleModifiers(_this, scancode, [event modifierFlags]);
   720         break;
   722         break;