I am in the process of making a C# WFA game engine, building it from a empty VS project / ground up. I have hit the point where i need to add input but have no idea of how to add a keyDown / keyUp event nor how to add or use a KeyEventHandler. Excuse my english.
I have tried using a partial class refering to the From class but it doesn't help. I'm basicaly stuck, and i have researched and have ben unable to find anything.
class Window
{
// privates needed to intialize the game screen
private Form screen;
private Size size;
private Bitmap bitmap;
private Graphics LoadGraphics;
private Graphics FinalGraphics;
// constructor for the window / game screen
public Window(GameContainer gc)
{
// initializes the game screen
bitmap = new Bitmap(gc.GetWidth(), gc.GetHeight());
size = new Size((int)(gc.GetWidth() * gc.GetScale()), (int)
(gc.GetHeight() * gc.GetScale()));
screen = new Form();
// makes the Game screen not resizeable
screen.GetPreferredSize(size);
screen.MinimumSize = size;
screen.MaximumSize = size;
screen.Size = size;
screen.FormBorderStyle = FormBorderStyle.FixedToolWindow;
screen.Text = gc.GetTitle();
// intializes the graphics
LoadGraphics = screen.CreateGraphics();
FinalGraphics = screen.CreateGraphics();
screen.BackColor = Color.Black;
}
// updater for the game screen
public void Update()
{
Application.DoEvents();
LoadGraphics = Graphics.FromImage(bitmap);
FinalGraphics.DrawImage(bitmap, 0, 0, screen.Width, screen.Height);
screen.Show();
}
// getter for the screen
public Form GetScreen()
{
return screen;
}
// getter for the bitmap
public Bitmap GetBitmap()
{
return bitmap;
}
public Color GetBackcolor()
{
return screen.BackColor;
}
}
class Input
{
public Input(Window screen)
{
screen.GetScreen().KeyPreview = true;
}
void Screen_KeyPress( object sender, KeyPressEventArgs e )
{
if (e.KeyChar == 65)
{
Console.WriteLine("you pressed A");
}
}
}