The SDL forums have moved to discourse.libsdl.org.
This is just a read-only archive of the previous forums, to keep old links working.
SDL
Simple Directmedia Layer Forums
Trying to use scancode to get key input.
Kelvin
Joined: 31 Mar 2016
Posts: 4
Posted: Thu Mar 31, 2016 5:59 pm
The wiki seems to be missing a ton of code samples, so I'm sorry if this seems like a stupid question. I'm trying to make a function that gets an integer to represent the scancode of a key then pass it to SDL from a Squirrel-scripted VM. I tried to use SDL_GetKeyFromScancode(), but it won't let me use an integer as input, so how to I turn an integer into an SDL_Scancode? I can't pass SDL_Scancode directly from Squirrel, or if i can, I don't know how.
Naith
Joined: 03 Jul 2014
Posts: 158
Posted: Fri Apr 01, 2016 12:10 am
You can use SDL_GetKeyboardState to accomplish what you want to do.
Example code on how to use it:
Code:
#include <stdio.h>
#include "SDL.h"
bool KeyPressed(const int Key)
{
// Get a snapshot of the current state of the keyboard
const Uint8* pCurrentKeyState = SDL_GetKeyboardState(NULL);
/**
* Make sure that 'Key' is a valid SDL_SCANCODE (don't wanna go out of bounds in the array)
* SDL_SCANCODE_A = 4
* SDL_NUM_SCANCODES = 512
*/
if(Key >= SDL_SCANCODE_A && Key < SDL_NUM_SCANCODES)
{
if(pCurrentKeyStates[Key])
return true;
}
return false;
}
int main(int argc, char* argv[])
{
// Initialize SDL and such