From 71d4b1d05e326a55b1e1a7c689523c0b04744c0f Mon Sep 17 00:00:00 2001 From: Andy Williams Date: Mon, 21 Jun 2021 19:42:04 +0100 Subject: [PATCH 1/4] Make the platform specific scancode available This is a complex topic and the int will vary based on platform (macOS is strange). Some applications need a reference to the source and we can't expose GLFW abstraction --- event.go | 2 ++ internal/driver/glfw/window.go | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/event.go b/event.go index 435b39e30f..5e8c7197f8 100644 --- a/event.go +++ b/event.go @@ -3,6 +3,8 @@ package fyne // KeyEvent describes a keyboard input event. type KeyEvent struct { Name KeyName + // ScanCode is a platform specific field that reports the hardware ID of the key event + ScanCode int } // PointEvent describes a pointer input event. The position is relative to the diff --git a/internal/driver/glfw/window.go b/internal/driver/glfw/window.go index 7062a30ba5..4a189c6311 100644 --- a/internal/driver/glfw/window.go +++ b/internal/driver/glfw/window.go @@ -1118,8 +1118,8 @@ func (w *window) capturesTab(modifier desktop.Modifier) bool { func (w *window) keyPressed(_ *glfw.Window, key glfw.Key, scancode int, action glfw.Action, mods glfw.ModifierKey) { keyName := keyToName(key, scancode) + keyEvent := &fyne.KeyEvent{Name: keyName, ScanCode: scancode} - keyEvent := &fyne.KeyEvent{Name: keyName} keyDesktopModifier := desktopModifier(mods) pendingMenuToggle := w.menuTogglePending pendingMenuDeactivation := w.menuDeactivationPending From 54ae18fe61603e20fd9c7002eadf817f222618ec Mon Sep 17 00:00:00 2001 From: Andy Williams Date: Thu, 8 Jul 2021 15:20:04 +0100 Subject: [PATCH 2/4] Use a type alias for clearer scancode identification And update the docs to make it clear --- event.go | 9 +++++++-- internal/driver/glfw/window.go | 2 +- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/event.go b/event.go index 5e8c7197f8..02a2ff46c1 100644 --- a/event.go +++ b/event.go @@ -1,10 +1,15 @@ package fyne +// ScanCode represents a hardware ID for (normally desktop) keyboard events. +// Most applications should use KeyName for cross-platform compatibility. +type ScanCode int + // KeyEvent describes a keyboard input event. type KeyEvent struct { + // Name describes the keyboard event that is consistent across platforms. Name KeyName - // ScanCode is a platform specific field that reports the hardware ID of the key event - ScanCode int + // HardwareCode is a platform specific field that reports the hardware ID of the keyboard events. + HardwareCode ScanCode } // PointEvent describes a pointer input event. The position is relative to the diff --git a/internal/driver/glfw/window.go b/internal/driver/glfw/window.go index 4a189c6311..f2e8528a71 100644 --- a/internal/driver/glfw/window.go +++ b/internal/driver/glfw/window.go @@ -1118,7 +1118,7 @@ func (w *window) capturesTab(modifier desktop.Modifier) bool { func (w *window) keyPressed(_ *glfw.Window, key glfw.Key, scancode int, action glfw.Action, mods glfw.ModifierKey) { keyName := keyToName(key, scancode) - keyEvent := &fyne.KeyEvent{Name: keyName, ScanCode: scancode} + keyEvent := &fyne.KeyEvent{Name: keyName, HardwareCode: fyne.ScanCode(scancode)} keyDesktopModifier := desktopModifier(mods) pendingMenuToggle := w.menuTogglePending From 093e441258d9df6b64a3d8a4bd80e63189e14253 Mon Sep 17 00:00:00 2001 From: Andy Williams Date: Mon, 19 Jul 2021 20:06:48 +0100 Subject: [PATCH 3/4] Fire event for unknown keys with unknown type Because we now fire on hardware info that will be present on many devices --- internal/driver/glfw/window.go | 5 +---- key.go | 6 ++++++ 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/internal/driver/glfw/window.go b/internal/driver/glfw/window.go index f2e8528a71..23ce65f316 100644 --- a/internal/driver/glfw/window.go +++ b/internal/driver/glfw/window.go @@ -1090,7 +1090,7 @@ func keyToName(code glfw.Key, scancode int) fyne.KeyName { keyName := glfw.GetKeyName(code, scancode) ret, ok = keyNameMap[keyName] if !ok { - return "" + return fyne.KeyUnknown } return ret @@ -1166,9 +1166,6 @@ func (w *window) keyPressed(_ *glfw.Window, key glfw.Key, scancode int, action g // key repeat will fall through to TypedKey and TypedShortcut } - if keyName == "" { // don't emit unknown - return - } if (keyName == fyne.KeyTab && !w.capturesTab(keyDesktopModifier)) || w.triggersShortcut(keyName, keyDesktopModifier) { return } diff --git a/key.go b/key.go index 4d3c4b3d6b..dd9bd53595 100644 --- a/key.go +++ b/key.go @@ -167,4 +167,10 @@ const ( KeyPlus KeyName = "+" // KeyBackTick is the key "`" on a US keyboard KeyBackTick KeyName = "`" + + // KeyUnknown is used for key events where the underlying hardware generated an + // event that Fyne could not decode. + // + // Since: 2.1 + KeyUnknown KeyName = "" ) From 7acba6988e721bf026403fd305411afbcc669f75 Mon Sep 17 00:00:00 2001 From: Andy Williams Date: Thu, 22 Jul 2021 17:07:19 +0100 Subject: [PATCH 4/4] Make the hardware info more open to expansion We will be adding location name somehow soon in another PR --- event.go | 11 +++++++---- internal/driver/glfw/window.go | 2 +- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/event.go b/event.go index 02a2ff46c1..6646e653ec 100644 --- a/event.go +++ b/event.go @@ -1,15 +1,18 @@ package fyne -// ScanCode represents a hardware ID for (normally desktop) keyboard events. +// HardwareKey contains information associated with physical key events // Most applications should use KeyName for cross-platform compatibility. -type ScanCode int +type HardwareKey struct { + // ScanCode represents a hardware ID for (normally desktop) keyboard events. + ScanCode int +} // KeyEvent describes a keyboard input event. type KeyEvent struct { // Name describes the keyboard event that is consistent across platforms. Name KeyName - // HardwareCode is a platform specific field that reports the hardware ID of the keyboard events. - HardwareCode ScanCode + // Physical is a platform specific field that reports the hardware information of physical keyboard events. + Physical HardwareKey } // PointEvent describes a pointer input event. The position is relative to the diff --git a/internal/driver/glfw/window.go b/internal/driver/glfw/window.go index 23ce65f316..0ec1e12a99 100644 --- a/internal/driver/glfw/window.go +++ b/internal/driver/glfw/window.go @@ -1118,7 +1118,7 @@ func (w *window) capturesTab(modifier desktop.Modifier) bool { func (w *window) keyPressed(_ *glfw.Window, key glfw.Key, scancode int, action glfw.Action, mods glfw.ModifierKey) { keyName := keyToName(key, scancode) - keyEvent := &fyne.KeyEvent{Name: keyName, HardwareCode: fyne.ScanCode(scancode)} + keyEvent := &fyne.KeyEvent{Name: keyName, Physical: fyne.HardwareKey{ScanCode: scancode}} keyDesktopModifier := desktopModifier(mods) pendingMenuToggle := w.menuTogglePending