mirror of
https://github.com/love2d/love.git
synced 2026-08-18 11:44:15 +02:00
Changed the love.keypressed and love.keyreleased event callbacks to be love.keypressed(key, scancode, isrepeat) and love.keyreleased(key, scancode). Added love.keyboard.isScancodeDown. Resolves issue #993.
--HG-- branch : minor
This commit is contained in:
@@ -25,6 +25,9 @@
|
||||
#include "common/Module.h"
|
||||
#include "common/StringMap.h"
|
||||
|
||||
// C++
|
||||
#include <vector>
|
||||
|
||||
namespace love
|
||||
{
|
||||
namespace keyboard
|
||||
@@ -534,11 +537,18 @@ public:
|
||||
virtual bool hasKeyRepeat() const = 0;
|
||||
|
||||
/**
|
||||
* Checks whether certain keys are down or not.
|
||||
* @param keylist An array of key identifiers, terminated by KEY_MAX_ENUM.
|
||||
* @return boolean
|
||||
* Checks whether certain keys are pressed or not.
|
||||
* @param keylist A list of key identifiers.
|
||||
* @return Whether any of the specified keys are pressed.
|
||||
**/
|
||||
virtual bool isDown(Key *keylist) const = 0;
|
||||
virtual bool isDown(const std::vector<Key> &keylist) const = 0;
|
||||
|
||||
/**
|
||||
* Checks whether certain scancodes are pressed or not.
|
||||
* @param scancodelist A list of scancodes.
|
||||
* @return Whether any of the specified scancodes are pressed.
|
||||
**/
|
||||
virtual bool isScancodeDown(const std::vector<Scancode> &scancodelist) const = 0;
|
||||
|
||||
/**
|
||||
* Gets the key corresponding to the specified scancode according to the
|
||||
|
||||
@@ -47,14 +47,31 @@ bool Keyboard::hasKeyRepeat() const
|
||||
return key_repeat;
|
||||
}
|
||||
|
||||
bool Keyboard::isDown(Key *keylist) const
|
||||
bool Keyboard::isDown(const std::vector<Key> &keylist) const
|
||||
{
|
||||
const Uint8 *keystate = SDL_GetKeyboardState(nullptr);
|
||||
const Uint8 *state = SDL_GetKeyboardState(nullptr);
|
||||
|
||||
for (Key key = *keylist; key != KEY_MAX_ENUM; key = *(++keylist))
|
||||
for (Key key : keylist)
|
||||
{
|
||||
SDL_Scancode scancode = SDL_GetScancodeFromKey(keymap[key]);
|
||||
if (keystate[scancode])
|
||||
if (state[scancode])
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool Keyboard::isScancodeDown(const std::vector<Scancode> &scancodelist) const
|
||||
{
|
||||
const Uint8 *state = SDL_GetKeyboardState(nullptr);
|
||||
|
||||
for (Scancode scancode : scancodelist)
|
||||
{
|
||||
SDL_Scancode sdlscancode = SDL_SCANCODE_UNKNOWN;
|
||||
if (!scancodes.find(scancode, sdlscancode))
|
||||
continue;
|
||||
|
||||
if (state[sdlscancode])
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -105,6 +122,16 @@ bool Keyboard::hasTextInput() const
|
||||
return SDL_IsTextInputActive();
|
||||
}
|
||||
|
||||
bool Keyboard::getConstant(Scancode in, SDL_Scancode &out)
|
||||
{
|
||||
return scancodes.find(in, out);
|
||||
}
|
||||
|
||||
bool Keyboard::getConstant(SDL_Scancode in, Scancode &out)
|
||||
{
|
||||
return scancodes.find(in, out);
|
||||
}
|
||||
|
||||
const SDL_Keycode *Keyboard::createKeyMap()
|
||||
{
|
||||
// Array must be static so its lifetime continues once the function returns.
|
||||
|
||||
@@ -46,7 +46,8 @@ public:
|
||||
|
||||
void setKeyRepeat(bool enable);
|
||||
bool hasKeyRepeat() const;
|
||||
bool isDown(Key *keylist) const;
|
||||
bool isDown(const std::vector<Key> &keylist) const;
|
||||
bool isScancodeDown(const std::vector<Scancode> &scancodelist) const;
|
||||
|
||||
Key getKeyFromScancode(Scancode scancode) const;
|
||||
Scancode getScancodeFromKey(Key key) const;
|
||||
@@ -54,6 +55,9 @@ public:
|
||||
void setTextInput(bool enable);
|
||||
bool hasTextInput() const;
|
||||
|
||||
static bool getConstant(Scancode in, SDL_Scancode &out);
|
||||
static bool getConstant(SDL_Scancode in, Scancode &out);
|
||||
|
||||
private:
|
||||
|
||||
// Whether holding down a key triggers repeated key press events.
|
||||
|
||||
@@ -46,19 +46,34 @@ int w_hasKeyRepeat(lua_State *L)
|
||||
int w_isDown(lua_State *L)
|
||||
{
|
||||
Keyboard::Key k;
|
||||
unsigned int num = lua_gettop(L);
|
||||
Keyboard::Key *keylist = new Keyboard::Key[num+1];
|
||||
unsigned int counter = 0;
|
||||
int num = lua_gettop(L);
|
||||
std::vector<Keyboard::Key> keylist;
|
||||
keylist.reserve(num);
|
||||
|
||||
for (unsigned int i = 0; i < num; i++)
|
||||
for (int i = 0; i < num; i++)
|
||||
{
|
||||
if (Keyboard::getConstant(luaL_checkstring(L, i+1), k))
|
||||
keylist[counter++] = k;
|
||||
keylist.push_back(k);
|
||||
}
|
||||
keylist[counter] = Keyboard::KEY_MAX_ENUM;
|
||||
|
||||
luax_pushboolean(L, instance()->isDown(keylist));
|
||||
delete[] keylist;
|
||||
return 1;
|
||||
}
|
||||
|
||||
int w_isScancodeDown(lua_State *L)
|
||||
{
|
||||
Keyboard::Scancode scancode;
|
||||
int num = lua_gettop(L);
|
||||
std::vector<Keyboard::Scancode> scancodelist;
|
||||
scancodelist.reserve(num);
|
||||
|
||||
for (int i = 0; i < num; i++)
|
||||
{
|
||||
if (Keyboard::getConstant(luaL_checkstring(L, i+1), scancode))
|
||||
scancodelist.push_back(scancode);
|
||||
}
|
||||
|
||||
luax_pushboolean(L, instance()->isScancodeDown(scancodelist));
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -116,6 +131,7 @@ static const luaL_Reg functions[] =
|
||||
{ "setTextInput", w_setTextInput },
|
||||
{ "hasTextInput", w_hasTextInput },
|
||||
{ "isDown", w_isDown },
|
||||
{ "isScancodeDown", w_isScancodeDown },
|
||||
{ "getScancodeFromKey", w_getScancodeFromKey },
|
||||
{ "getKeyFromScancode", w_getKeyFromScancode },
|
||||
{ 0, 0 }
|
||||
|
||||
@@ -33,6 +33,7 @@ namespace keyboard
|
||||
int w_setKeyRepeat(lua_State *L);
|
||||
int w_hasKeyRepeat(lua_State *L);
|
||||
int w_isDown(lua_State *L);
|
||||
int w_isScancodeDown(lua_State *L);
|
||||
int w_getKeyFromScancode(lua_State *L);
|
||||
int w_getScancodeFromkey(lua_State *L);
|
||||
int w_setTextInput(lua_State *L);
|
||||
|
||||
Reference in New Issue
Block a user