KEYBOARDEVENTS DONE?????

This commit is contained in:
Ben
2021-09-17 17:22:54 +01:00
parent 7d5242126b
commit 163e0eeafd
3 changed files with 19 additions and 20 deletions

View File

@@ -20,8 +20,6 @@ Input::Input()
mKeyboardEventDispatcher.RegisterAsSource( "ENGINE_INPUT_KEYBOARD" );
mKbdState = static_cast<const uint8_t*>(SDL_GetKeyboardState( &mNumScancodes ));
mOldKbdState = static_cast<uint8_t*>(malloc( mNumScancodes / sizeof( uint8_t ) ));
memcpy( mOldKbdState, mKbdState, 242 / sizeof( uint8_t ) );
}
Input::~Input()
@@ -30,7 +28,7 @@ Input::~Input()
mMouseEventDispatcher.DeRegisterAsSource( "ENGINE_INPUT_MOUSE" );
mKeyboardEventDispatcher.DeRegisterAsSource( "ENGINE_INPUT_KEYBOARD" );
free( mOldKbdState );
// Do not free mKbdState as that is done by SDL
}
void Input::PollInput()
@@ -74,8 +72,6 @@ void Input::PollInput()
// keyboard processing
mPollScanKeyboard();
memcpy( mOldKbdState, mKbdState, mNumScancodes / sizeof( uint8_t ) );
}
void Input::mPollDisplay()
@@ -231,17 +227,19 @@ void Input::mPollKeyboard()
void Input::mPollScanKeyboard()
{
// first we want to check if any key has changed
// if a key has changed, then we want to check if any keys are pressed
// if a key is pressed we want to check the keys and dispatch events
// according to the scancode
// keydown and keyup will be done seperately
if ( !std::equal( mKbdState, mKbdState + mNumScancodes, mOldKbdState ) )
{
// this is naive, can be optimised with double buffering
for ( int i = 0; i < mNumScancodes; i++ )
{
if ( mKbdState[i] != 0 )
std::cout << KeyCodeFromSDL( mKbdState[i] ) << " ";
bool isKeyPressed = (bool)mKbdState[i];
if ( isKeyPressed )
{
EKeyCode whatKeyPressed = KeyCodeFromScanCode( (SDL_Scancode)i );
Aeon::Core::GenericEvent e;
e.keyCode = whatKeyPressed;
e.Type = "KEYBOARD_KEYPRESS";
mKeyboardEventDispatcher.Dispatch( e );
}
}
}

View File

@@ -1,6 +1,8 @@
#ifndef AEON_INPUT_INPUT_H_
#define AEON_INPUT_INPUT_H_
#include <vector>
#include <SDL.h>
#include "Aeon/Singleton.hpp"
@@ -29,7 +31,6 @@ private:
int mNumScancodes = 242;
const uint8_t* mKbdState;
uint8_t* mOldKbdState;
Aeon::Core::EventDispatcher mDisplayEventDispatcher;
Aeon::Core::EventDispatcher mKeyboardEventDispatcher;

View File

@@ -173,6 +173,11 @@ inline EKeyCode KeyCodeFromSDL( SDL_Keycode key )
return (EKeyCode)key;
}
inline EKeyCode KeyCodeFromScanCode( SDL_Scancode key )
{
return KeyCodeFromSDL( SDL_GetKeyFromScancode( key ) );
}
inline EKeyCode KeyCodeFromKeymod( SDL_Keymod key )
{
switch ( key )
@@ -221,11 +226,6 @@ inline EKeyCode KeyCodeFromKeymod( SDL_Keymod key )
}
}
inline EKeyCode KeyCodeFromSDL( SDL_Scancode key )
{
return KeyCodeFromSDL( SDL_SCANCODE_TO_KEYCODE( key ) );
}
}
#endif