gioui.org/ui@v0.0.0-20190926171558-ce74bc0cbaea/key/key.go (about)

     1  // SPDX-License-Identifier: Unlicense OR MIT
     2  
     3  /*
     4  Package key implements key and text events and operations.
     5  
     6  The InputOp operations is used for declaring key input handlers. Use
     7  an implementation of the Queue interface from package ui to receive
     8  events.
     9  */
    10  package key
    11  
    12  import (
    13  	"gioui.org/ui"
    14  	"gioui.org/ui/internal/opconst"
    15  )
    16  
    17  // InputOp declares a handler ready for key events.
    18  // Key events are in general only delivered to the
    19  // focused key handler. Set the Focus flag to request
    20  // the focus.
    21  type InputOp struct {
    22  	Key   ui.Key
    23  	Focus bool
    24  }
    25  
    26  // HideInputOp request that any on screen text input
    27  // be hidden.
    28  type HideInputOp struct{}
    29  
    30  // A FocusEvent is generated when a handler gains or loses
    31  // focus.
    32  type FocusEvent struct {
    33  	Focus bool
    34  }
    35  
    36  // An Event is generated when a key is pressed. For text input
    37  // use EditEvent.
    38  type Event struct {
    39  	// Name is the rune character that most closely
    40  	// match the key. For letters, the upper case form
    41  	// is used.
    42  	Name rune
    43  	// Modifiers is the set of active modifiers when
    44  	// the key was pressed.
    45  	Modifiers Modifiers
    46  }
    47  
    48  // An EditEvent is generated when text is input.
    49  type EditEvent struct {
    50  	Text string
    51  }
    52  
    53  // Modifiers
    54  type Modifiers uint32
    55  
    56  const (
    57  	// ModCommand is the command modifier. On macOS
    58  	// it is the Cmd key, on other platforms the Ctrl
    59  	// key.
    60  	ModCommand Modifiers = 1 << iota
    61  	// THe shift key.
    62  	ModShift
    63  )
    64  
    65  const (
    66  	// Runes for special keys.
    67  	NameLeftArrow      = '←'
    68  	NameRightArrow     = '→'
    69  	NameUpArrow        = '↑'
    70  	NameDownArrow      = '↓'
    71  	NameReturn         = '⏎'
    72  	NameEnter          = '⌤'
    73  	NameEscape         = '⎋'
    74  	NameHome           = '⇱'
    75  	NameEnd            = '⇲'
    76  	NameDeleteBackward = '⌫'
    77  	NameDeleteForward  = '⌦'
    78  	NamePageUp         = '⇞'
    79  	NamePageDown       = '⇟'
    80  )
    81  
    82  // Contain reports whether m contains all modifiers
    83  // in m2.
    84  func (m Modifiers) Contain(m2 Modifiers) bool {
    85  	return m&m2 == m2
    86  }
    87  
    88  func (h InputOp) Add(o *ui.Ops) {
    89  	data := make([]byte, opconst.TypeKeyInputLen)
    90  	data[0] = byte(opconst.TypeKeyInput)
    91  	if h.Focus {
    92  		data[1] = 1
    93  	}
    94  	o.Write(data, h.Key)
    95  }
    96  
    97  func (h HideInputOp) Add(o *ui.Ops) {
    98  	data := make([]byte, opconst.TypeHideInputLen)
    99  	data[0] = byte(opconst.TypeHideInput)
   100  	o.Write(data)
   101  }
   102  
   103  func (EditEvent) ImplementsEvent()  {}
   104  func (Event) ImplementsEvent()      {}
   105  func (FocusEvent) ImplementsEvent() {}