examples: add keyboard-input example
Signed-off-by: Lilis Iskandar <lilis@veand.co>
This commit is contained in:
parent
c29aa61c8c
commit
41bf1cc99f
|
@ -0,0 +1,94 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/veandco/go-sdl2/sdl"
|
||||
)
|
||||
|
||||
func run() (err error) {
|
||||
var window *sdl.Window
|
||||
|
||||
if err = sdl.Init(sdl.INIT_EVERYTHING); err != nil {
|
||||
return
|
||||
}
|
||||
defer sdl.Quit()
|
||||
|
||||
window, err = sdl.CreateWindow("Input", sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED, 800, 600, sdl.WINDOW_SHOWN)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
defer window.Destroy()
|
||||
|
||||
running := true
|
||||
for running {
|
||||
for event := sdl.PollEvent(); event != nil; event = sdl.PollEvent() {
|
||||
switch t := event.(type) {
|
||||
case *sdl.QuitEvent:
|
||||
running = false
|
||||
case *sdl.KeyboardEvent:
|
||||
keyCode := t.Keysym.Sym
|
||||
keys := ""
|
||||
|
||||
// Modifier keys
|
||||
switch t.Keysym.Mod {
|
||||
case sdl.KMOD_LALT:
|
||||
keys += "Left Alt"
|
||||
case sdl.KMOD_LCTRL:
|
||||
keys += "Left Control"
|
||||
case sdl.KMOD_LSHIFT:
|
||||
keys += "Left Shift"
|
||||
case sdl.KMOD_LGUI:
|
||||
keys += "Left Meta or Windows key"
|
||||
case sdl.KMOD_RALT:
|
||||
keys += "Right Alt"
|
||||
case sdl.KMOD_RCTRL:
|
||||
keys += "Right Control"
|
||||
case sdl.KMOD_RSHIFT:
|
||||
keys += "Right Shift"
|
||||
case sdl.KMOD_RGUI:
|
||||
keys += "Right Meta or Windows key"
|
||||
case sdl.KMOD_NUM:
|
||||
keys += "Num Lock"
|
||||
case sdl.KMOD_CAPS:
|
||||
keys += "Caps Lock"
|
||||
case sdl.KMOD_MODE:
|
||||
keys += "AltGr Key"
|
||||
}
|
||||
|
||||
if keyCode < 10000 {
|
||||
if keys != "" {
|
||||
keys += " + "
|
||||
}
|
||||
|
||||
// If the key is held down, this will fire
|
||||
if t.Repeat > 0 {
|
||||
keys += string(keyCode) + " repeating"
|
||||
} else {
|
||||
if t.State == sdl.RELEASED {
|
||||
keys += string(keyCode) + " released"
|
||||
} else if t.State == sdl.PRESSED {
|
||||
keys += string(keyCode) + " pressed"
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if keys != "" {
|
||||
fmt.Println(keys)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
sdl.Delay(16)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func main() {
|
||||
if err := run(); err != nil {
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue