mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2025-01-11 19:48:54 +01:00
Implement keyboard events for js
This commit is contained in:
parent
5c755f2f31
commit
47227139db
@ -33,25 +33,6 @@ func CursorPosition() (x, y int) {
|
|||||||
return current.input.cursorPosition()
|
return current.input.cursorPosition()
|
||||||
}
|
}
|
||||||
|
|
||||||
type input struct {
|
|
||||||
keyPressed [KeyMax]bool
|
|
||||||
mouseButtonPressed [MouseButtonMax]bool
|
|
||||||
cursorX int
|
|
||||||
cursorY int
|
|
||||||
}
|
|
||||||
|
|
||||||
func (i *input) isKeyPressed(key Key) bool {
|
|
||||||
return i.keyPressed[key]
|
|
||||||
}
|
|
||||||
|
|
||||||
func (i *input) isMouseButtonPressed(button MouseButton) bool {
|
|
||||||
return i.mouseButtonPressed[button]
|
|
||||||
}
|
|
||||||
|
|
||||||
func (i *input) cursorPosition() (x, y int) {
|
|
||||||
return i.cursorX, i.cursorY
|
|
||||||
}
|
|
||||||
|
|
||||||
var glfwKeyCodeToKey = map[glfw.Key]Key{
|
var glfwKeyCodeToKey = map[glfw.Key]Key{
|
||||||
glfw.KeySpace: KeySpace,
|
glfw.KeySpace: KeySpace,
|
||||||
glfw.KeyLeft: KeyLeft,
|
glfw.KeyLeft: KeyLeft,
|
||||||
|
@ -16,17 +16,34 @@
|
|||||||
|
|
||||||
package ui
|
package ui
|
||||||
|
|
||||||
|
var keyCodeToKey = map[int]Key{
|
||||||
|
32: KeySpace,
|
||||||
|
37: KeyLeft,
|
||||||
|
39: KeyRight,
|
||||||
|
38: KeyUp,
|
||||||
|
40: KeyDown,
|
||||||
|
}
|
||||||
|
|
||||||
|
var currentInput input
|
||||||
|
|
||||||
func IsKeyPressed(key Key) bool {
|
func IsKeyPressed(key Key) bool {
|
||||||
// TODO: Implement this.
|
return currentInput.isKeyPressed(key)
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func IsMouseButtonPressed(button MouseButton) bool {
|
func IsMouseButtonPressed(button MouseButton) bool {
|
||||||
// TODO: Implement this.
|
return currentInput.isMouseButtonPressed(button)
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func CursorPosition() (x, y int) {
|
func CursorPosition() (x, y int) {
|
||||||
// TODO: Implement this.
|
return currentInput.cursorPosition()
|
||||||
return 0, 0
|
}
|
||||||
|
|
||||||
|
func (i *input) keyDown(key int) {
|
||||||
|
k := keyCodeToKey[key]
|
||||||
|
i.keyPressed[k] = true
|
||||||
|
}
|
||||||
|
|
||||||
|
func (i *input) keyUp(key int) {
|
||||||
|
k := keyCodeToKey[key]
|
||||||
|
i.keyPressed[k] = false
|
||||||
}
|
}
|
||||||
|
@ -33,3 +33,22 @@ const (
|
|||||||
MouseButtonMiddle
|
MouseButtonMiddle
|
||||||
MouseButtonMax
|
MouseButtonMax
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type input struct {
|
||||||
|
keyPressed [KeyMax]bool
|
||||||
|
mouseButtonPressed [MouseButtonMax]bool
|
||||||
|
cursorX int
|
||||||
|
cursorY int
|
||||||
|
}
|
||||||
|
|
||||||
|
func (i *input) isKeyPressed(key Key) bool {
|
||||||
|
return i.keyPressed[key]
|
||||||
|
}
|
||||||
|
|
||||||
|
func (i *input) isMouseButtonPressed(button MouseButton) bool {
|
||||||
|
return i.mouseButtonPressed[button]
|
||||||
|
}
|
||||||
|
|
||||||
|
func (i *input) cursorPosition() (x, y int) {
|
||||||
|
return i.cursorX, i.cursorY
|
||||||
|
}
|
||||||
|
@ -66,6 +66,21 @@ func init() {
|
|||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
context = opengl.NewContext(webglContext)
|
context = opengl.NewContext(webglContext)
|
||||||
|
|
||||||
|
// Make the canvas focusable.
|
||||||
|
canvas.Call("setAttribute", "tabindex", 1)
|
||||||
|
canvas.Get("style").Set("outline", "none")
|
||||||
|
|
||||||
|
canvas.Set("onkeydown", func(e js.Object) {
|
||||||
|
defer e.Call("preventDefault")
|
||||||
|
code := e.Get("keyCode").Int()
|
||||||
|
currentInput.keyDown(code)
|
||||||
|
})
|
||||||
|
canvas.Set("onkeyup", func(e js.Object) {
|
||||||
|
defer e.Call("preventDefault")
|
||||||
|
code := e.Get("keyCode").Int()
|
||||||
|
currentInput.keyUp(code)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func Start(width, height, scale int, title string) (actualScale int, err error) {
|
func Start(width, height, scale int, title string) (actualScale int, err error) {
|
||||||
|
Loading…
Reference in New Issue
Block a user