github.com/daidehu6831/wails/v2@v2.2.0/pkg/menu/keys/keys.go (about) 1 package keys 2 3 import ( 4 "fmt" 5 "strings" 6 ) 7 8 // Modifier is actually a string 9 type Modifier string 10 11 const ( 12 // CmdOrCtrlKey represents Command on Mac and Control on other platforms 13 CmdOrCtrlKey Modifier = "cmdorctrl" 14 // OptionOrAltKey represents Option on Mac and Alt on other platforms 15 OptionOrAltKey Modifier = "optionoralt" 16 // ShiftKey represents the shift key on all systems 17 ShiftKey Modifier = "shift" 18 // SuperKey represents Command on Mac and the Windows key on the other platforms 19 //SuperKey Modifier = "super" 20 // ControlKey represents the control key on all systems 21 ControlKey Modifier = "ctrl" 22 ) 23 24 var modifierMap = map[string]Modifier{ 25 "cmdorctrl": CmdOrCtrlKey, 26 "optionoralt": OptionOrAltKey, 27 "shift": ShiftKey, 28 //"super": SuperKey, 29 "ctrl": ControlKey, 30 } 31 32 func parseModifier(text string) (*Modifier, error) { 33 lowertext := strings.ToLower(text) 34 result, valid := modifierMap[lowertext] 35 if !valid { 36 return nil, fmt.Errorf("'%s' is not a valid modifier", text) 37 } 38 39 return &result, nil 40 } 41 42 // Accelerator holds the keyboard shortcut for a menu item 43 type Accelerator struct { 44 Key string 45 Modifiers []Modifier 46 } 47 48 // Key creates a standard key Accelerator 49 func Key(key string) *Accelerator { 50 return &Accelerator{ 51 Key: strings.ToLower(key), 52 } 53 } 54 55 // CmdOrCtrl creates a 'CmdOrCtrl' Accelerator 56 func CmdOrCtrl(key string) *Accelerator { 57 return &Accelerator{ 58 Key: strings.ToLower(key), 59 Modifiers: []Modifier{CmdOrCtrlKey}, 60 } 61 } 62 63 // OptionOrAlt creates a 'OptionOrAlt' Accelerator 64 func OptionOrAlt(key string) *Accelerator { 65 return &Accelerator{ 66 Key: strings.ToLower(key), 67 Modifiers: []Modifier{OptionOrAltKey}, 68 } 69 } 70 71 // Shift creates a 'Shift' Accelerator 72 func Shift(key string) *Accelerator { 73 return &Accelerator{ 74 Key: strings.ToLower(key), 75 Modifiers: []Modifier{ShiftKey}, 76 } 77 } 78 79 // Control creates a 'Control' Accelerator 80 func Control(key string) *Accelerator { 81 return &Accelerator{ 82 Key: strings.ToLower(key), 83 Modifiers: []Modifier{ControlKey}, 84 } 85 } 86 87 // 88 //// Super creates a 'Super' Accelerator 89 //func Super(key string) *Accelerator { 90 // return &Accelerator{ 91 // Key: strings.ToLower(key), 92 // Modifiers: []Modifier{SuperKey}, 93 // } 94 //} 95 96 // Combo creates an Accelerator with multiple Modifiers 97 func Combo(key string, modifier1 Modifier, modifier2 Modifier, rest ...Modifier) *Accelerator { 98 result := &Accelerator{ 99 Key: key, 100 Modifiers: []Modifier{modifier1, modifier2}, 101 } 102 for _, extra := range rest { 103 result.Modifiers = append(result.Modifiers, extra) 104 } 105 return result 106 }