From 06d38790ffbd4410192514566954ed9ee6e10261 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Tue, 3 Sep 2019 00:45:27 +0900 Subject: [PATCH] event: Add Key (#933) This also renames event names from Key* to KeyboardKey*. Updates #926 --- event/event.go | 24 +++++---- event/keys.go | 129 +++++++++++++++++++++++++++++++++++++++++++++++++ genkeys.go | 21 +++++++- 3 files changed, 160 insertions(+), 14 deletions(-) create mode 100644 event/keys.go diff --git a/event/event.go b/event/event.go index 621bb169c..88972d491 100644 --- a/event/event.go +++ b/event/event.go @@ -25,12 +25,11 @@ package event type Event interface { } -// KeyCharacter is an event that occurs when a character is actually typed on +// KeyboardKeyCharacter is an event that occurs when a character is actually typed on // the keyboard. This may be provided by an input method. -type KeyCharacter struct { - // Code is the key code of the key typed. - // TODO: this should change later from an int to an enumeration type. - Code int +type KeyboardKeyCharacter struct { + // Key is the key code of the key typed. + Key Key // Modifiers are the modifiers pressed together with the key. // TODO: this should change later from an int to an enumeration type. Modifiers int @@ -38,19 +37,18 @@ type KeyCharacter struct { Character rune } -// KeyDown is an event that occurs when a key is pressed on the keyboard. -type KeyDown struct { - // Code is the key code of the key pressed or released. - // TODO: this should change later from an int to an enumeration type. - Code int +// KeyboardKeyDown is an event that occurs when a key is pressed on the keyboard. +type KeyboardKeyDown struct { + // Key is the key code of the key pressed or released. + Key Key // Modifiers are the modifiers pressed together with the key. // TODO: this should change later from an int to an enumeration type. Modifiers int } -// KeyUp is an event that occurs when a key is released on the keyboard. -// The data is the same as for a KeyDown event. -type KeyUp KeyDown +// KeyboardKeyUp is an event that occurs when a key is released on the keyboard. +// The data is the same as for a KeyboardKeyDown event. +type KeyboardKeyUp KeyboardKeyDown // GamepadAxis is for event where an axis on a gamepad changes. type GamepadAxis struct { diff --git a/event/keys.go b/event/keys.go new file mode 100644 index 000000000..4d6687a58 --- /dev/null +++ b/event/keys.go @@ -0,0 +1,129 @@ +// Copyright 2013 The Ebiten Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// Code generated by genkeys.go using 'go generate'. DO NOT EDIT. + +package event + +import ( + "github.com/hajimehoshi/ebiten/internal/driver" +) + +type Key int + +const ( + Key0 Key = Key(driver.Key0) + Key1 Key = Key(driver.Key1) + Key2 Key = Key(driver.Key2) + Key3 Key = Key(driver.Key3) + Key4 Key = Key(driver.Key4) + Key5 Key = Key(driver.Key5) + Key6 Key = Key(driver.Key6) + Key7 Key = Key(driver.Key7) + Key8 Key = Key(driver.Key8) + Key9 Key = Key(driver.Key9) + KeyA Key = Key(driver.KeyA) + KeyB Key = Key(driver.KeyB) + KeyC Key = Key(driver.KeyC) + KeyD Key = Key(driver.KeyD) + KeyE Key = Key(driver.KeyE) + KeyF Key = Key(driver.KeyF) + KeyG Key = Key(driver.KeyG) + KeyH Key = Key(driver.KeyH) + KeyI Key = Key(driver.KeyI) + KeyJ Key = Key(driver.KeyJ) + KeyK Key = Key(driver.KeyK) + KeyL Key = Key(driver.KeyL) + KeyM Key = Key(driver.KeyM) + KeyN Key = Key(driver.KeyN) + KeyO Key = Key(driver.KeyO) + KeyP Key = Key(driver.KeyP) + KeyQ Key = Key(driver.KeyQ) + KeyR Key = Key(driver.KeyR) + KeyS Key = Key(driver.KeyS) + KeyT Key = Key(driver.KeyT) + KeyU Key = Key(driver.KeyU) + KeyV Key = Key(driver.KeyV) + KeyW Key = Key(driver.KeyW) + KeyX Key = Key(driver.KeyX) + KeyY Key = Key(driver.KeyY) + KeyZ Key = Key(driver.KeyZ) + KeyApostrophe Key = Key(driver.KeyApostrophe) + KeyBackslash Key = Key(driver.KeyBackslash) + KeyBackspace Key = Key(driver.KeyBackspace) + KeyCapsLock Key = Key(driver.KeyCapsLock) + KeyComma Key = Key(driver.KeyComma) + KeyDelete Key = Key(driver.KeyDelete) + KeyDown Key = Key(driver.KeyDown) + KeyEnd Key = Key(driver.KeyEnd) + KeyEnter Key = Key(driver.KeyEnter) + KeyEqual Key = Key(driver.KeyEqual) + KeyEscape Key = Key(driver.KeyEscape) + KeyF1 Key = Key(driver.KeyF1) + KeyF2 Key = Key(driver.KeyF2) + KeyF3 Key = Key(driver.KeyF3) + KeyF4 Key = Key(driver.KeyF4) + KeyF5 Key = Key(driver.KeyF5) + KeyF6 Key = Key(driver.KeyF6) + KeyF7 Key = Key(driver.KeyF7) + KeyF8 Key = Key(driver.KeyF8) + KeyF9 Key = Key(driver.KeyF9) + KeyF10 Key = Key(driver.KeyF10) + KeyF11 Key = Key(driver.KeyF11) + KeyF12 Key = Key(driver.KeyF12) + KeyGraveAccent Key = Key(driver.KeyGraveAccent) + KeyHome Key = Key(driver.KeyHome) + KeyInsert Key = Key(driver.KeyInsert) + KeyKP0 Key = Key(driver.KeyKP0) + KeyKP1 Key = Key(driver.KeyKP1) + KeyKP2 Key = Key(driver.KeyKP2) + KeyKP3 Key = Key(driver.KeyKP3) + KeyKP4 Key = Key(driver.KeyKP4) + KeyKP5 Key = Key(driver.KeyKP5) + KeyKP6 Key = Key(driver.KeyKP6) + KeyKP7 Key = Key(driver.KeyKP7) + KeyKP8 Key = Key(driver.KeyKP8) + KeyKP9 Key = Key(driver.KeyKP9) + KeyKPAdd Key = Key(driver.KeyKPAdd) + KeyKPDecimal Key = Key(driver.KeyKPDecimal) + KeyKPDivide Key = Key(driver.KeyKPDivide) + KeyKPEnter Key = Key(driver.KeyKPEnter) + KeyKPEqual Key = Key(driver.KeyKPEqual) + KeyKPMultiply Key = Key(driver.KeyKPMultiply) + KeyKPSubtract Key = Key(driver.KeyKPSubtract) + KeyLeft Key = Key(driver.KeyLeft) + KeyLeftAlt Key = Key(driver.KeyLeftAlt) + KeyLeftBracket Key = Key(driver.KeyLeftBracket) + KeyLeftControl Key = Key(driver.KeyLeftControl) + KeyLeftShift Key = Key(driver.KeyLeftShift) + KeyMenu Key = Key(driver.KeyMenu) + KeyMinus Key = Key(driver.KeyMinus) + KeyNumLock Key = Key(driver.KeyNumLock) + KeyPageDown Key = Key(driver.KeyPageDown) + KeyPageUp Key = Key(driver.KeyPageUp) + KeyPause Key = Key(driver.KeyPause) + KeyPeriod Key = Key(driver.KeyPeriod) + KeyPrintScreen Key = Key(driver.KeyPrintScreen) + KeyRight Key = Key(driver.KeyRight) + KeyRightAlt Key = Key(driver.KeyRightAlt) + KeyRightBracket Key = Key(driver.KeyRightBracket) + KeyRightControl Key = Key(driver.KeyRightControl) + KeyRightShift Key = Key(driver.KeyRightShift) + KeyScrollLock Key = Key(driver.KeyScrollLock) + KeySemicolon Key = Key(driver.KeySemicolon) + KeySlash Key = Key(driver.KeySlash) + KeySpace Key = Key(driver.KeySpace) + KeyTab Key = Key(driver.KeyTab) + KeyUp Key = Key(driver.KeyUp) +) diff --git a/genkeys.go b/genkeys.go index 47f5e0486..4832aca87 100644 --- a/genkeys.go +++ b/genkeys.go @@ -306,6 +306,24 @@ const ( ) ` +const eventKeysTmpl = `{{.License}} + +{{.DoNotEdit}} + +package event + +import ( + "github.com/hajimehoshi/ebiten/internal/driver" +) + +type Key int + +const ( +{{range $index, $name := .DriverKeyNames}}Key{{$name}} Key = Key(driver.Key{{$name}}) +{{end}} +) +` + const uidriverGlfwKeysTmpl = `{{.License}} {{.DoNotEdit}} @@ -480,11 +498,12 @@ func main() { sort.Slice(driverKeyNames, keyNamesLess(driverKeyNames)) for path, tmpl := range map[string]string{ - "keys.go": ebitenKeysTmpl, + "event/keys.go": eventKeysTmpl, "internal/driver/keys.go": driverKeysTmpl, "internal/glfw/keys.go": glfwKeysTmpl, "internal/uidriver/glfw/keys.go": uidriverGlfwKeysTmpl, "internal/uidriver/js/keys.go": uidriverJsKeysTmpl, + "keys.go": ebitenKeysTmpl, } { f, err := os.Create(path) if err != nil {