examples/keyboard: Show other keys than 'keyboard' keys

This commit is contained in:
Hajime Hoshi 2018-04-14 04:59:06 +09:00
parent bf304e6ac8
commit fac7651152
3 changed files with 11 additions and 3 deletions

View File

@ -21,8 +21,10 @@ import (
"image"
_ "image/png"
"log"
"strings"
"github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/ebitenutil"
"github.com/hajimehoshi/ebiten/examples/keyboard/keyboard"
rkeyabord "github.com/hajimehoshi/ebiten/examples/resources/images/keyboard"
)
@ -44,7 +46,6 @@ func init() {
}
func update(screen *ebiten.Image) error {
// Collect pressed keys' names.
pressed := []ebiten.Key{}
for k := ebiten.Key(0); k <= ebiten.KeyMax; k++ {
if ebiten.IsKeyPressed(k) {
@ -81,6 +82,12 @@ func update(screen *ebiten.Image) error {
screen.DrawImage(keyboardImage, op)
}
keyStrs := []string{}
for _, p := range pressed {
keyStrs = append(keyStrs, p.String())
}
ebitenutil.DebugPrint(screen, strings.Join(keyStrs, ", "))
return nil
}

View File

@ -35,6 +35,9 @@ func InputChars() []rune {
// IsKeyPressed returns a boolean indicating whether key is pressed.
//
// Known issue: On Edge browser, KeyKPEnter and KeyKPEqual don't work well.
// These keys are recognized as KeyEnter and KeyEqual.
//
// This function is concurrent-safe.
func IsKeyPressed(key Key) bool {
return input.Get().IsKeyPressed(input.Key(key))

View File

@ -28,8 +28,6 @@ import (
type Key int
// Keys.
//
// Known issue: KeyKPEnter and KeyKPEqual don't work on Edge browsers.
const (
Key0 Key = Key(input.Key0)
Key1 Key = Key(input.Key1)