From 3a14aae31062a84aa342136dc9264cc978d761b9 Mon Sep 17 00:00:00 2001 From: faiface Date: Wed, 10 May 2017 21:10:10 +0200 Subject: [PATCH] add Window.Typed --- pixelgl/input.go | 14 ++++++++++++++ pixelgl/window.go | 1 + 2 files changed, 15 insertions(+) diff --git a/pixelgl/input.go b/pixelgl/input.go index 1af9977..0a4ed9c 100644 --- a/pixelgl/input.go +++ b/pixelgl/input.go @@ -31,6 +31,11 @@ func (w *Window) MouseScroll() pixel.Vec { return w.currInp.scroll } +// Typed returns the text typed on the keyboard since the last call to Window.Update. +func (w *Window) Typed() string { + return w.currInp.typed +} + // Button is a keyboard or mouse button. Why distinguish? type Button int @@ -350,16 +355,25 @@ func (w *Window) initInput() { w.window.SetScrollCallback(func(_ *glfw.Window, xoff, yoff float64) { w.currInp.scroll += pixel.V(xoff, yoff) }) + + w.window.SetCharCallback(func(_ *glfw.Window, r rune) { + w.currInp.typed += string(r) + }) }) } func (w *Window) updateInput() { + //FIXME: rething this, currInp can be changed outside this function, which may lead to inconsistencies + // copy temp to prev w.prevInp = w.tempInp // zero current scroll (but keep what was added in callbacks outside of this function) w.currInp.scroll -= w.tempInp.scroll + // erase typed string + w.currInp.typed = "" + // get events (usually calls callbacks, but callbacks can be called outside too) mainthread.Call(func() { glfw.PollEvents() diff --git a/pixelgl/window.go b/pixelgl/window.go index 36a8da3..7ad4f1b 100644 --- a/pixelgl/window.go +++ b/pixelgl/window.go @@ -69,6 +69,7 @@ type Window struct { mouse pixel.Vec buttons [KeyLast + 1]bool scroll pixel.Vec + typed string } }