Skip to content

Instantly share code, notes, and snippets.

@thatcosmonaut
Created December 29, 2023 01:31
Show Gist options
  • Save thatcosmonaut/3249baace86de6aa16458365b0670f65 to your computer and use it in GitHub Desktop.
Save thatcosmonaut/3249baace86de6aa16458365b0670f65 to your computer and use it in GitHub Desktop.
KeyboardTest
using System;
using System.Runtime.InteropServices;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
namespace KeyboardTest
{
unsafe class KeyboardTestGame : Game
{
GraphicsDeviceManager graphics;
KeyboardState oldKeyboardState = new KeyboardState();
byte* oldSdlKeyboardState;
public KeyboardTestGame()
{
graphics = new GraphicsDeviceManager(this);
graphics.PreferredBackBufferWidth = 1280;
graphics.PreferredBackBufferHeight = 720;
Content.RootDirectory = "Content";
Window.AllowUserResizing = true;
IsMouseVisible = true;
SDL2.SDL.SDL_GetKeyboardState(out int numkeys);
oldSdlKeyboardState = (byte*) Marshal.AllocHGlobal(numkeys);
}
protected override void Update(GameTime gameTime)
{
var keyboardState = Keyboard.GetState();
foreach (var key in keyboardState.GetPressedKeys())
{
if (!oldKeyboardState.IsKeyDown(key))
{
Console.WriteLine("XNA key: " + key.ToString());
}
}
byte* sdlKeyboardState = (byte*) SDL2.SDL.SDL_GetKeyboardState(out int numkeys);
for (int scancode = 0; scancode < numkeys; scancode += 1)
{
if (sdlKeyboardState[scancode] != 0 && oldSdlKeyboardState[scancode] == 0)
{
Console.WriteLine("SDL key: " + (SDL2.SDL.SDL_Scancode) scancode);
}
}
oldKeyboardState = keyboardState;
Buffer.MemoryCopy(sdlKeyboardState, oldSdlKeyboardState, numkeys, numkeys);
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
base.Draw(gameTime);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment