From acb8029a9354f140dc3accd6636e06e48b2eba06 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Mon, 28 May 2012 23:45:48 +0200 Subject: [PATCH] SDL2 context: keyboard handling for basic keys. --- src/Contexts/Sdl2Context.cpp | 6 ++++++ src/Contexts/Sdl2Context.h | 33 ++++++++++++++++++++++++++++++++- 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/src/Contexts/Sdl2Context.cpp b/src/Contexts/Sdl2Context.cpp index 0ca70b5c5..b772c609c 100644 --- a/src/Contexts/Sdl2Context.cpp +++ b/src/Contexts/Sdl2Context.cpp @@ -82,6 +82,12 @@ int Sdl2Context::exec() { _redraw = true; break; } break; + case SDL_KEYDOWN: + keyPressEvent(static_cast(event.key.keysym.sym), event.key.repeat); + break; + case SDL_KEYUP: + keyReleaseEvent(static_cast(event.key.keysym.sym)); + break; case SDL_MOUSEBUTTONDOWN: case SDL_MOUSEBUTTONUP: mouseEvent(static_cast(event.button.button), diff --git a/src/Contexts/Sdl2Context.h b/src/Contexts/Sdl2Context.h index bbb3227eb..b98b559e0 100644 --- a/src/Contexts/Sdl2Context.h +++ b/src/Contexts/Sdl2Context.h @@ -30,7 +30,7 @@ namespace Magnum { namespace Contexts { /** @nosubgrouping @brief SDL2 context -Currently doesn't have any mouse/keyboard handling. +Supports keyboard and mouse handling. You need to implement at least drawEvent() and viewportEvent() to be able to draw on the screen. @@ -75,6 +75,37 @@ class Sdl2Context: public AbstractContext { /*@}*/ + /** @{ @name Keyboard handling */ + public: + /** + * @brief Key + * @see keyPressEvent(), keyReleaseEvent() + */ + enum class Key: SDL_Keycode { + Up = SDLK_UP, /**< Up arrow */ + Down = SDLK_DOWN, /**< Down arrow */ + Left = SDLK_LEFT, /**< Left arrow */ + Right = SDLK_RIGHT, /**< Right arrow */ + Plus = SDLK_PLUS, /**< Plus */ + Minus = SDLK_MINUS /**< Minus */ + }; + + protected: + /** + * @brief Key press event + * @param key Key pressed + * @param repeat Non-zero if this is a key repeat + */ + inline virtual void keyPressEvent(Key key, Uint8 repeat) {} + + /** + * @brief Key release event + * @param key Key release + */ + inline virtual void keyReleaseEvent(Key key) {} + + /*@}*/ + /** @{ @name Mouse handling */ public: