Skip to content

Commit

Permalink
Add a keyboard abstraction so Fyne is input-API agnostic
Browse files Browse the repository at this point in the history
  • Loading branch information
andydotxyz committed Nov 25, 2018
1 parent 8695bf8 commit 46e0f06
Show file tree
Hide file tree
Showing 5 changed files with 204 additions and 16 deletions.
23 changes: 22 additions & 1 deletion driver/efl/window.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,28 @@ func onWindowKeyDown(ew C.Ecore_Window, info *C.Ecore_Event_Key) {

ev := new(fyne.KeyEvent)
ev.String = C.GoString(info.string)
ev.Name = C.GoString(info.keyname)
switch C.GoString(info.keyname) {
case "Shift_L":
fallthrough
case "Shift_R":
ev.Name = fyne.KeyShift
case "Control_L":
fallthrough
case "Control_R":
ev.Name = fyne.KeyControl
case "Alt_L":
fallthrough
case "Alt_R":
ev.Name = fyne.KeyAlt
case "Super_L":
fallthrough
case "Super_R":
ev.Name = fyne.KeySuper

default:
ev.Name = fyne.KeyName(C.GoString(info.keyname))
}

if (info.modifiers & C.ECORE_EVENT_MODIFIER_SHIFT) != 0 {
ev.Modifiers |= fyne.ShiftModifier
}
Expand Down
98 changes: 91 additions & 7 deletions driver/gl/window.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,16 +242,97 @@ func (w *window) mouseClicked(viewport *glfw.Window, button glfw.MouseButton, ac
}
}

func keyToName(key glfw.Key) string {
func keyToName(key glfw.Key) fyne.KeyName {
switch key {
// printable
case glfw.KeySpace:
return fyne.KeySpace

// non-printable
case glfw.KeyEscape:
return fyne.KeyEscape
case glfw.KeyEnter:
return fyne.KeyReturn
case glfw.KeyTab:
return fyne.KeyTab
case glfw.KeyBackspace:
return fyne.KeyBackspace
case glfw.KeyInsert:
return fyne.KeyInsert
case glfw.KeyDelete:
return fyne.KeyDelete
case glfw.KeyRight:
return "Right"
return fyne.KeyRight
case glfw.KeyLeft:
return "Left"
case glfw.KeyUp:
return "Up"
return fyne.KeyLeft
case glfw.KeyDown:
return "Down"
return fyne.KeyDown
case glfw.KeyUp:
return fyne.KeyUp
case glfw.KeyPageUp:
return fyne.KeyPageUp
case glfw.KeyPageDown:
return fyne.KeyPageDown
case glfw.KeyHome:
return fyne.KeyHome
case glfw.KeyEnd:
return fyne.KeyEnd

case glfw.KeyF1:
return fyne.KeyF1
case glfw.KeyF2:
return fyne.KeyF2
case glfw.KeyF3:
return fyne.KeyF3
case glfw.KeyF4:
return fyne.KeyF4
case glfw.KeyF5:
return fyne.KeyF5
case glfw.KeyF6:
return fyne.KeyF6
case glfw.KeyF7:
return fyne.KeyF7
case glfw.KeyF8:
return fyne.KeyF8
case glfw.KeyF9:
return fyne.KeyF9
case glfw.KeyF10:
return fyne.KeyF10
case glfw.KeyF11:
return fyne.KeyF11
case glfw.KeyF12:
return fyne.KeyF12

case glfw.KeyLeftShift:
fallthrough
case glfw.KeyRightShift:
return fyne.KeyShift
case glfw.KeyLeftControl:
fallthrough
case glfw.KeyRightControl:
return fyne.KeyControl
case glfw.KeyLeftAlt:
fallthrough
case glfw.KeyRightAlt:
return fyne.KeyAlt
case glfw.KeyLeftSuper:
fallthrough
case glfw.KeyRightSuper:
return fyne.KeySuper
case glfw.KeyMenu:
return fyne.KeyMenu

case glfw.KeyKPEnter:
return fyne.KeyEnter
}
return ""
}

func charToName(char rune) fyne.KeyName {
switch char {
case ' ':
return fyne.KeySpace

}
return ""
}
Expand All @@ -260,6 +341,9 @@ func (w *window) keyPressed(viewport *glfw.Window, key glfw.Key, scancode int, a
if w.canvas.Focused() == nil && w.canvas.onKeyDown == nil {
return
}
if action != glfw.Press { // ignore key up
return
}

if key <= glfw.KeyWorld1 { // filter printable characters handled in charModInput
return
Expand Down Expand Up @@ -291,7 +375,7 @@ func (w *window) charModInput(viewport *glfw.Window, char rune, mods glfw.Modifi
}

ev := new(fyne.KeyEvent)
ev.Name = string(char)
ev.Name = charToName(char)
ev.String = string(char)
if (mods & glfw.ModShift) != 0 {
ev.Modifiers |= fyne.ShiftModifier
Expand Down
2 changes: 1 addition & 1 deletion event.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ package fyne
type KeyEvent struct {
String string

Name string
Name KeyName
Modifiers Modifier
}

Expand Down
83 changes: 83 additions & 0 deletions key.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,88 @@
package fyne

// KeyName represents the name of a key that has been pressed (or released)
type KeyName string

const (
// KeyUnnamed is used when the key does not have a name (check the key string for a printable version)
KeyUnnamed = ""

// Printable keys

// KeySpace name can also be checked with KeyEvent.String == " "
KeySpace = "Space"

// Non-printable keys

// KeyEscape is the "esc" key
KeyEscape = "Escape"
// KeyReturn is the carriage return (main keyboard)
KeyReturn = "Return"
// KeyTab is the tab advance key
KeyTab = "Tab"
// KeyBackspace is the delete-before-cursor key
KeyBackspace = "BackSpace"
// KeyInsert is the insert mode key
KeyInsert = "Insert"
// KeyDelete is the delete-after-cursor key
KeyDelete = "Delete"
// KeyRight is the right arrow key
KeyRight = "Right"
// KeyLeft is the left arrow key
KeyLeft = "Left"
// KeyDown is the down arrow key
KeyDown = "Down"
// KeyUp is the up arrow key
KeyUp = "Up"
// KeyPageUp is the page up num-pad key
KeyPageUp = "Prior"
// KeyPageDown is the page down num-pad key
KeyPageDown = "Next"
// KeyHome is the line-home key
KeyHome = "Home"
// KeyEnd is the line-end key
KeyEnd = "End"

// KeyF1 is the first function key
KeyF1 = "F1"
// KeyF2 is the second function key
KeyF2 = "F2"
// KeyF3 is the third function key
KeyF3 = "F3"
// KeyF4 is the fourth function key
KeyF4 = "F4"
// KeyF5 is the fifth function key
KeyF5 = "F5"
// KeyF6 is the sixth function key
KeyF6 = "F6"
// KeyF7 is the seventh function key
KeyF7 = "F7"
// KeyF8 is the eighth function key
KeyF8 = "F8"
// KeyF9 is the ninth function key
KeyF9 = "F9"
// KeyF10 is the tenth function key
KeyF10 = "F10"
// KeyF11 is the eleventh function key
KeyF11 = "F11"
// KeyF12 is the twelfth function key
KeyF12 = "F12"

// KeyEnter is the enter/ return key (keypad)
KeyEnter = "KP_Enter"

// KeyShift represents the left or right shift key
KeyShift = "Shift"
// KeyControl represents the left or right control key
KeyControl = "Control"
// KeyAlt represents the left or right alt key
KeyAlt = "Alt"
// KeySuper represents the left or right "Windows" key (or "Command" key on macOS)
KeySuper = "Super"
// KeyMenu represents the left or right menu / application key
KeyMenu = "Menu"
)

// Modifier captures any key modifiers (shift etc) pressed during this key event
type Modifier int

Expand Down
14 changes: 7 additions & 7 deletions widget/entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ func (e *Entry) Focused() bool {

// OnKeyDown receives key input events when the Entry widget is focused.
func (e *Entry) OnKeyDown(key *fyne.KeyEvent) {
if key.Name == "BackSpace" {
if key.Name == fyne.KeyBackspace {
if len(e.Text) == 0 || (e.CursorColumn == 0 && e.CursorRow == 0) {
return
}
Expand All @@ -176,7 +176,7 @@ func (e *Entry) OnKeyDown(key *fyne.KeyEvent) {
}

e.SetText(substr)
} else if key.Name == "Delete" {
} else if key.Name == fyne.KeyDelete {
texts := e.label().renderer.(*labelRenderer).texts
if len(e.Text) == 0 || (e.CursorRow == len(texts)-1 && e.CursorColumn == len(texts[e.CursorRow].Text)) {
return
Expand All @@ -187,35 +187,35 @@ func (e *Entry) OnKeyDown(key *fyne.KeyEvent) {
substr := fmt.Sprintf("%s%s", string(runes[:pos]), string(runes[pos+1:]))

e.SetText(substr)
} else if key.Name == "Return" {
} else if key.Name == fyne.KeyReturn {
e.insertAtCursor("\n")

e.CursorColumn = 0
e.CursorRow++
} else if key.Name == "Up" {
} else if key.Name == fyne.KeyUp {
if e.CursorRow > 0 {
e.CursorRow--
}

if e.CursorColumn > e.label().RowLength(e.CursorRow) {
e.CursorColumn = e.label().RowLength(e.CursorRow)
}
} else if key.Name == "Down" {
} else if key.Name == fyne.KeyDown {
if e.CursorRow < e.label().Rows()-1 {
e.CursorRow++
}

if e.CursorColumn > e.label().RowLength(e.CursorRow) {
e.CursorColumn = e.label().RowLength(e.CursorRow)
}
} else if key.Name == "Left" {
} else if key.Name == fyne.KeyLeft {
if e.CursorColumn > 0 {
e.CursorColumn--
} else if e.CursorRow > 0 {
e.CursorRow--
e.CursorColumn = e.label().RowLength(e.CursorRow)
}
} else if key.Name == "Right" {
} else if key.Name == fyne.KeyRight {
if e.CursorColumn < e.label().RowLength(e.CursorRow) {
e.CursorColumn++
} else if e.CursorRow < e.label().Rows()-1 {
Expand Down

0 comments on commit 46e0f06

Please sign in to comment.