ebiten/ui/glfw/keyboard.go
Hajime Hoshi 7fd54f6bb3 Add input
2014-12-07 22:10:04 +09:00

40 lines
763 B
Go

package glfw
import (
glfw "github.com/go-gl/glfw3"
"github.com/hajimehoshi/ebiten/input"
)
type Keyboard struct {
pressedKeys map[input.Key]struct{}
}
func NewKeyboard() *Keyboard {
return &Keyboard{
pressedKeys: map[input.Key]struct{}{},
}
}
func (k *Keyboard) IsKeyPressed(key input.Key) bool {
_, ok := k.pressedKeys[key]
return ok
}
var glfwKeyCodeToKey = map[glfw.Key]input.Key{
glfw.KeySpace: input.KeySpace,
glfw.KeyLeft: input.KeyLeft,
glfw.KeyRight: input.KeyRight,
glfw.KeyUp: input.KeyUp,
glfw.KeyDown: input.KeyDown,
}
func (k *Keyboard) update(window *glfw.Window) {
for g, u := range glfwKeyCodeToKey {
if window.GetKey(g) == glfw.Press {
k.pressedKeys[u] = struct{}{}
} else {
delete(k.pressedKeys, u)
}
}
}