ebiten/ui/glfw/keyboard.go

40 lines
763 B
Go
Raw Normal View History

2014-12-06 19:27:55 +01:00
package glfw
import (
glfw "github.com/go-gl/glfw3"
2014-12-07 14:10:04 +01:00
"github.com/hajimehoshi/ebiten/input"
2014-12-06 19:27:55 +01:00
)
type Keyboard struct {
2014-12-07 14:10:04 +01:00
pressedKeys map[input.Key]struct{}
2014-12-06 19:27:55 +01:00
}
func NewKeyboard() *Keyboard {
return &Keyboard{
2014-12-07 14:10:04 +01:00
pressedKeys: map[input.Key]struct{}{},
2014-12-06 19:27:55 +01:00
}
}
2014-12-07 14:10:04 +01:00
func (k *Keyboard) IsKeyPressed(key input.Key) bool {
2014-12-06 19:27:55 +01:00
_, ok := k.pressedKeys[key]
return ok
}
2014-12-07 14:10:04 +01:00
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,
2014-12-06 19:27:55 +01:00
}
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)
}
}
}