golang调用sdl2,键盘和鼠标事件

    win10 x64下测试成功,其他操作系统下不保证成功。

    采用的是syscall方式,不是cgo方式。

    见地址

    代码用golang编写。代码如下:

    1. package main
    2. import (
    3. "fmt"
    4. "github.com/moonfdd/sdl2-go/sdl"
    5. "github.com/moonfdd/sdl2-go/sdlcommon"
    6. )
    7. func main() {
    8. sdlcommon.SetSDL2Path("SDL2.dll")
    9. sdl.SDL_Init(sdl.SDL_INIT_VIDEO)
    10. window := sdl.SDL_CreateWindow("微信公众号:福大大架构师每日一题", sdl.SDL_WINDOWPOS_UNDEFINED, sdl.SDL_WINDOWPOS_UNDEFINED, 640, 480, sdl.SDL_WINDOW_OPENGL|sdl.SDL_WINDOW_RESIZABLE)
    11. if window == nil {
    12. fmt.Printf("Can't create window, err:%s", sdl.SDL_GetError())
    13. return
    14. }
    15. renderer := window.SDL_CreateRenderer(-1, 0)
    16. if renderer == nil {
    17. return
    18. }
    19. renderer.SDL_SetRenderDrawColor(255, 0, 0, 255)
    20. renderer.SDL_RenderClear()
    21. renderer.SDL_RenderPresent()
    22. var event sdl.SDL_Event
    23. for {
    24. event.SDL_WaitEvent()
    25. if event.Type == sdl.SDL_KEYDOWN {
    26. fmt.Println("键盘按下事件", event.ToSDL_KeyboardEvent().Keysym.Sym)
    27. } else if event.Type == sdl.SDL_MOUSEBUTTONDOWN {
    28. fmt.Println("鼠标按下事件")
    29. } else if event.Type == sdl.SDL_QUIT {
    30. fmt.Println("退出事件")
    31. break
    32. }
    33. }
    34. if renderer != nil {
    35. renderer.SDL_DestroyRenderer()
    36. }
    37. window.SDL_DestroyWindow()
    38. sdl.SDL_Quit()
    39. }

    执行结果如下:

    golang调用sdl2,键盘和鼠标事件 - 图1