Changed the definition of KeyEvent to be positional. The implementation has not been written, and thus the package will not build yet.

This commit is contained in:
Pietro Gagliardi 2014-03-28 21:10:42 -04:00
parent 359462854b
commit 0d75303dc3
1 changed files with 80 additions and 43 deletions

123
area.go
View File

@ -15,10 +15,11 @@ import (
// See AreaHandler for details. // See AreaHandler for details.
// //
// Do not use an Area if you intend to read text. // Do not use an Area if you intend to read text.
// Due to platform differences regarding text input, // Area reads keys based on their position on a standard
// keyboard events have beem compromised in // 101-key keyboard, and does no character processing.
// such a way that attempting to read Unicode data // Character processing methods differ across operating
// in platform-native ways is painful. // systems; trying ot recreate these yourself is only going
// to lead to trouble.
// [Use TextArea instead, providing a TextAreaHandler.] // [Use TextArea instead, providing a TextAreaHandler.]
// //
// To facilitate development and debugging, for the time being, Areas only work on GTK+. // To facilitate development and debugging, for the time being, Areas only work on GTK+.
@ -106,15 +107,10 @@ func (e MouseEvent) HeldBits() (h uintptr) {
// A KeyEvent represents a keypress in an Area. // A KeyEvent represents a keypress in an Area.
// //
// In a perfect world, KeyEvent would be 100% predictable. // Key presses are based on their positions on a standard
// Despite my best efforts to do this, however, the various // 101-key keyboard found on most computers. The
// differences in input handling between each backend // names chosen for keys here are based on their names
// environment makes this completely impossible (I can // on US English QWERTY keyboards; see Key for details.
// work with two of the three identically, but not all three).
// Keep this in mind, and remember that Areas are not ideal
// for text. For more details, see areaplan.md and the linked
// tweets at the end of that file. If you know a better solution
// than the one I have chosen, please let me know.
// //
// When you are finished processing the incoming event, // When you are finished processing the incoming event,
// return whether or not you did something in response // return whether or not you did something in response
@ -126,42 +122,39 @@ func (e MouseEvent) HeldBits() (h uintptr) {
// unconditionally, which may result in unwanted behavior like // unconditionally, which may result in unwanted behavior like
// global task-switching keystrokes not being processed.) // global task-switching keystrokes not being processed.)
// //
// If a key is pressed that is not supported by ASCII, ExtKey, // If a key is pressed that is not supported by Key, ExtKey,
// or Modifiers, no KeyEvent will be produced, and package // or Modifiers, no KeyEvent will be produced, and package
// ui will act as if false was returned for handled. // ui will act as if false was returned for handled.
type KeyEvent struct { type KeyEvent struct {
// ASCII is a byte representing the character pressed. // Key is a byte representing a character pressed
// Despite my best efforts, this cannot be trivialized // in the typewriter section of the keyboard.
// to produce predictable input rules on all OSs, even if // The value, which is independent of whether the
// I try to handle physical keys instead of equivalent // Shift key is held, is a constant with one of the
// characters. Therefore, what happens when the user // following (case-sensitive) values, drawn according
// inserts a non-ASCII character is undefined (some systems // to the key's position on the keyboard.
// will give package ui the underlying ASCII key and we // ` 1 2 3 4 5 6 7 8 9 0 - =
// return it; other systems do not). This is especially important // q w e r t y u i o p [ ] \
// if the given input method uses Modifiers to enter characters. // a s d f g h j k l ; '
// If the parenthesized rule cannot be followed and the user // z x c v b n m , . /
// enters a non-ASCII character, it will be ignored (package ui // The actual key entered will be the key at the respective
// will act as above regarding keys it cannot handle). // position on the user's keyboard, regardless of the actual
// In general, alphanumeric characters, ',', '.', '+', '-', and the // layout. (Some keyboards move \ to either the row above
// (space) should be available on all keyboards. Other ASCII // or the row below but in roughly the same spot; this is
// whitespace keys mentioned below may be available, but // accounted for. Some keyboards have an additonal key
// mind layout differences. // to the left of 'z' or additional keys to the right of '='; these
// Whether or not alphabetic characters are uppercase or // cannot be read.)
// lowercase is undefined, and cannot be determined solely // In addition, Key will contain
// by examining Modifiers for Shift. Correct code should handle
// both uppercase and lowercase identically.
// In addition, ASCII will contain
// - ' ' (space) if the spacebar was pressed // - ' ' (space) if the spacebar was pressed
// - '\t' if Tab was pressed, regardless of Modifiers // - '\t' if Tab was pressed, regardless of Modifiers
// - '\n' if any Enter/Return key was pressed, regardless of which // - '\n' if the typewriter Enter key was pressed
// - '\b' if the typewriter Backspace key was pressed // - '\b' if the typewriter Backspace key was pressed
// If this value is zero, see ExtKey. // If this value is zero, see ExtKey.
ASCII byte Key byte
// If ASCII is zero, ExtKey contains a predeclared identifier // If Key is zero, ExtKey contains a predeclared identifier
// naming an extended key. See ExtKey for details. // naming an extended key. See ExtKey for details.
// If both ASCII and ExtKey are zero, a Modifier by itself // If both Key and ExtKey are zero, a Modifier by itself
// was pressed. ASCII and ExtKey will not both be nonzero. // was pressed. Key and ExtKey will not both be nonzero.
ExtKey ExtKey ExtKey ExtKey
Modifiers Modifiers Modifiers Modifiers
@ -175,8 +168,7 @@ type KeyEvent struct {
Up bool Up bool
} }
// ExtKey represents keys that do not have an ASCII representation. // ExtKey represents keys that are not in the typewriter section of the keyboard.
// There is no way to differentiate between left and right ExtKeys.
type ExtKey uintptr type ExtKey uintptr
const ( const (
Escape ExtKey = iota + 1 Escape ExtKey = iota + 1
@ -190,7 +182,7 @@ const (
Down Down
Left Left
Right Right
F1 // no guarantee is made that Fn == F1+n in the future F1 // F1..F12 are guaranteed to be consecutive
F2 F2
F3 F3
F4 F4
@ -202,9 +194,54 @@ const (
F10 F10
F11 F11
F12 F12
N0 // numpad keys; independent of Num Lock state
N1 // N0..N9 are guaranteed to be consecutive
N2
N3
N4
N5
N6
N7
N8
N9
NDot
NEnter
NAdd
NSubtract
NMultiply
NDivide
_nextkeys // for sanity check _nextkeys // for sanity check
) )
// EffectiveKey returns e.Key if it is set.
// Otherwise, if e.ExtKey denotes a numpad key,
// EffectiveKey returns the equivalent e.Key value
// ('0'..'9', '.', '\n', '+', '-', '*', or '/').
// Otherwise, EffectiveKey returns zero.
func (e KeyEvent) EffectiveKey() byte {
if e.Key != 0 {
return e.Key
}
k := e.ExtKey
switch {
case k >= N0 && k <= N9:
return byte(k - N0) + '0'
case k == NDot:
return '.'
case k == NEnter:
return '\n'
case k == NAdd:
return '+'
case k == NSubtract:
return '-'
case k == NMultiply:
return '*'
case k == NDivide:
return '/'
}
return 0
}
// Modifiers indicates modifier keys being held during an event. // Modifiers indicates modifier keys being held during an event.
// There is no way to differentiate between left and right modifier keys. // There is no way to differentiate between left and right modifier keys.
type Modifiers uintptr type Modifiers uintptr