...

Source file src/code.rocketnine.space/tslocum/cview/keys.go

Documentation: code.rocketnine.space/tslocum/cview

     1  package cview
     2  
     3  import (
     4  	"code.rocketnine.space/tslocum/cbind"
     5  	"github.com/gdamore/tcell/v2"
     6  )
     7  
     8  // Key defines the keyboard shortcuts of an application.
     9  // Secondary shortcuts apply when not focusing a text input.
    10  type Key struct {
    11  	Cancel []string
    12  
    13  	Select  []string
    14  	Select2 []string
    15  
    16  	MoveUp     []string
    17  	MoveUp2    []string
    18  	MoveDown   []string
    19  	MoveDown2  []string
    20  	MoveLeft   []string
    21  	MoveLeft2  []string
    22  	MoveRight  []string
    23  	MoveRight2 []string
    24  
    25  	MoveFirst  []string
    26  	MoveFirst2 []string
    27  	MoveLast   []string
    28  	MoveLast2  []string
    29  
    30  	MovePreviousField []string
    31  	MoveNextField     []string
    32  	MovePreviousPage  []string
    33  	MoveNextPage      []string
    34  
    35  	ShowContextMenu []string
    36  }
    37  
    38  // Keys defines the keyboard shortcuts of an application.
    39  // Secondary shortcuts apply when not focusing a text input.
    40  var Keys = Key{
    41  	Cancel: []string{"Escape"},
    42  
    43  	Select:  []string{"Enter", "Ctrl+J"}, // Ctrl+J = keypad enter
    44  	Select2: []string{"Space"},
    45  
    46  	MoveUp:     []string{"Up"},
    47  	MoveUp2:    []string{"k"},
    48  	MoveDown:   []string{"Down"},
    49  	MoveDown2:  []string{"j"},
    50  	MoveLeft:   []string{"Left"},
    51  	MoveLeft2:  []string{"h"},
    52  	MoveRight:  []string{"Right"},
    53  	MoveRight2: []string{"l"},
    54  
    55  	MoveFirst:  []string{"Home", "Ctrl+A"},
    56  	MoveFirst2: []string{"g"},
    57  	MoveLast:   []string{"End", "Ctrl+E"},
    58  	MoveLast2:  []string{"G"},
    59  
    60  	MovePreviousField: []string{"Backtab"},
    61  	MoveNextField:     []string{"Tab"},
    62  	MovePreviousPage:  []string{"PageUp", "Ctrl+B"},
    63  	MoveNextPage:      []string{"PageDown", "Ctrl+F"},
    64  
    65  	ShowContextMenu: []string{"Alt+Enter"},
    66  }
    67  
    68  // HitShortcut returns whether the EventKey provided is present in one or more
    69  // sets of keybindings.
    70  func HitShortcut(event *tcell.EventKey, keybindings ...[]string) bool {
    71  	enc, err := cbind.Encode(event.Modifiers(), event.Key(), event.Rune())
    72  	if err != nil {
    73  		return false
    74  	}
    75  
    76  	for _, binds := range keybindings {
    77  		for _, key := range binds {
    78  			if key == enc {
    79  				return true
    80  			}
    81  		}
    82  	}
    83  
    84  	return false
    85  }
    86  

View as plain text