-2

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");
        }
    }

}
K. Hansson
  • 21
  • 9
  • Is your key event handler only meant ot process keyboard input from a specific UI element (which would need to have the keyboard input focus), or should the key event handler process key events no matter what UI element might have the input focus? –  Feb 03 '19 at 18:15
  • https://learn.microsoft.com/en-us/dotnet/framework/winforms/using-keyboard-events – Bob Shaffer Feb 03 '19 at 18:17
  • If it's game engine you may want to actually hook the keyboard, anyway for key handlers you basically create a delegate, so OnKeyPressed += (double click tab to autocreate delegate in VS) – MaKiPL Feb 03 '19 at 18:17
  • i guess the most crucial part comes to my understanding of the english language when reading the "learn.microsoft.com" page for answers! – K. Hansson Feb 03 '19 at 18:18
  • Check the documentation; it describes how to do it at the form level: https://learn.microsoft.com/en-us/dotnet/framework/winforms/how-to-handle-keyboard-input-at-the-form-level –  Feb 03 '19 at 18:18
  • i have updated the code a little, am i on the right way or is it even worse? also i have a console running at the same time as the form for debugging – K. Hansson Feb 03 '19 at 18:42

1 Answers1

0

If it's game engine you may want to actually hook the keyboard, anyway for key handlers you basically create a delegate, so OnKeyPressed += (double click tab to autocreate delegate in VS)

After trial and error I got it to work

public Input(Window screen)
{
    screen.GetScreen().KeyPreview = true;
    screen.GetScreen().KeyDown += Input_KeyDown;
}

private void Input_KeyDown( object sender, KeyEventArgs e )
{
    if(e.KeyCode == Keys.A)
    {
        Console.WriteLine("you pressed 'A' !");
    }
}
Dale K
  • 25,246
  • 15
  • 42
  • 71
K. Hansson
  • 21
  • 9