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 Forum Index
SDL
Simple Directmedia Layer Forums
Trying to use scancode to get key input.
Kelvin


Joined: 31 Mar 2016
Posts: 4
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
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
   
   bool Running = true;

   while(Running)
   {
      if(KeyPressed(SDL_SCANCODE_ESCAPE))
         Running = false;

      if(KeyPressed(SDL_SCANCODE_SPACE))
         printf("Space key pressed\n");
   }
   
   return 0;
}