Cocoa Keyboard Shortcuts in Dialog without an Edit Menu

前端 未结 16 1252
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:57

    Here's Travis' answer as C# for use with Xamarin.Mac:

        public override bool PerformKeyEquivalent (AppKit.NSEvent e)
        {
            if (e.Type == NSEventType.KeyDown) {
                var inputKey = e.CharactersIgnoringModifiers.ToLower ();
                if (   (e.ModifierFlags & NSEventModifierMask.DeviceIndependentModifierFlagsMask) == NSEventModifierMask.CommandKeyMask
                    || (e.ModifierFlags & NSEventModifierMask.DeviceIndependentModifierFlagsMask) == (NSEventModifierMask.CommandKeyMask | NSEventModifierMask.AlphaShiftKeyMask)) {
                    switch (inputKey) {
                    case "x":
                        NSApplication.SharedApplication.SendAction (new Selector ("cut:"), null, this);
                        return true;
                    case "c":
                        NSApplication.SharedApplication.SendAction (new Selector ("copy:"), null, this);
                        return true;
                    case "v":
                        NSApplication.SharedApplication.SendAction (new Selector ("paste:"), null, this);
                        return true;
                    case "z":
                        NSApplication.SharedApplication.SendAction (new Selector ("undo:"), null, this);
                        return true;
                    case "a":
                        NSApplication.SharedApplication.SendAction (new Selector ("selectAll:"), null, this);
                        return true;
                    }
                } else if (   (e.ModifierFlags & NSEventModifierMask.DeviceIndependentModifierFlagsMask) == (NSEventModifierMask.CommandKeyMask | NSEventModifierMask.ShiftKeyMask)
                           || (e.ModifierFlags & NSEventModifierMask.DeviceIndependentModifierFlagsMask) == (NSEventModifierMask.CommandKeyMask | NSEventModifierMask.ShiftKeyMask | NSEventModifierMask.AlphaShiftKeyMask)) {
                    switch (inputKey) {
                    case "z":
                        NSApplication.SharedApplication.SendAction (new Selector ("redo:"), null, this);
                        return true;
                    }
                }
            }
            return base.PerformKeyEquivalent(e);
        }
    

提交回复
热议问题