Cocoa Keyboard Shortcuts in Dialog without an Edit Menu

前端 未结 16 1256
Happy的楠姐
Happy的楠姐 2020-12-04 07:28

I have an LSUIElement application that displays a menubar status item. The application can display a dialog window that contains a text field.

If the u

16条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-04 07:40

    Adrian's solution is good, but better I think to use a switch statement rather than all those string comparisons, e.g.:

        uint const modifierCode = (theEvent.modifierFlags & NSDeviceIndependentModifierFlagsMask);
        BOOL usingModifiers = ( modifierCode != 0 );
        //BOOL const usingShiftKey = ((theEvent.modifierFlags & NSShiftKeyMask) != 0);
        //BOOL const usingCommandKey = ((theEvent.modifierFlags & NSCommandKeyMask) != 0);
        NSString * ch = [theEvent charactersIgnoringModifiers];
        if ( ( usingModifiers ) && ( ch.length == 1 ) ) switch ( [ch characterAtIndex:0] )
        {
            case 'x':
                if ( modifierCode == NSCommandKeyMask ) [m cut]; // <-- m = model
                break;
            case 'c':
                if ( modifierCode == NSCommandKeyMask ) [m copy];
                break;
            case 'v':
                if ( modifierCode == NSCommandKeyMask ) [m paste];
                break;
            case 'z':
                if ( modifierCode == NSCommandKeyMask ) [m undo];
                break;
            case 'Z':
                if ( modifierCode == ( NSCommandKeyMask | NSShiftKeyMask ) ) [m redo];
                break;
            default: // etc.
                break;
        }
        else switch ( theEvent.keyCode ) // <-- for independent keycodes!
        {
            case kVK_Home:
                [m moveToBeginningOfDocument:nil];
                break;
            case kVK_End: // etc!
    

提交回复
热议问题