Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: KeyPressed Event not firing in SFML.Net C# (with code)  (Read 1142 times)

0 Members and 1 Guest are viewing this topic.

Tigre Pablito

  • Full Member
  • ***
  • Posts: 225
    • View Profile
    • Email
KeyPressed Event not firing in SFML.Net C# (with code)
« on: August 18, 2017, 07:40:28 pm »
Hi Ladies and Guys

I did what eXpl0it3r told me in my last post, use Events instead of real time input. For my surprise the Keypressed Event is not working in my MenuBox class. I had never got this issue before. Here is the code. I pass the window reference from the main loop to the constructor.

using SFML.Graphics;
using SFML.Audio;
using SFML.Window;
using SFML.System;
using System;

namespace SpaceInvaders
{
    class MenuBox
    {
        private Text[] options;
        private int y;
        private IntRect board;
        private string title;
        private int chosen;
        private bool returnValue;
        private RenderWindow window;

        public MenuBox(string[] options, int y, RenderWindow window)
        {
            int a;
            this.options = new Text[options.Length];
            for (a = 0; a < options.Length; a++)
            {
                this.options[a] = new Text(options[a], new Font("data files\\fonts\\Arial.ttf"), 30);
            }            
            this.y = y;            
            this.window = window;
        }

        public MenuBox(string[] options, int y, IntRect board, string title, RenderWindow window) : this(options, y, window)
        {
            this.board = board;
            this.title = title;
        }

        public int DoMenuBox()
        {
            int a;            
            this.chosen = 0;
            this.returnValue = false;
            this.window.KeyPressed += new EventHandler<KeyEventArgs>(window_KeyPressed);
           
            if (this.board != null)
            {
                RectangleShape board = new RectangleShape(new Vector2f(this.board.Width, this.board.Height));
                board.OutlineThickness = 6;
                board.OutlineColor = new Color(128, 128, 128);
                board.FillColor = new Color(192, 192, 192);
                board.Position = new Vector2f(400, 300);
                board.Origin = new Vector2f(this.board.Width / 2, this.board.Height / 2);
                Text title = new Text(this.title, new Font("data files\\fonts\\Arial.ttf"), 30);
                title.Origin = new Vector2f(title.GetLocalBounds().Width / 2, title.GetLocalBounds().Height / 2);
                title.Position = new Vector2f(400, 200);
                title.Color = Color.White;
                this.window.Draw(board);
                this.window.Draw(title);
            }
           
            while (true)
            {
                for (a = 0; a < this.options.Length; a++)
                {
                    this.options[a].Position = new Vector2f(400, y + 40 * a);
                    this.options[a].Origin = new Vector2f(this.options[a].GetLocalBounds().Width / 2, this.options[a].GetLocalBounds().Top);
                    this.options[a].Color = new Color(255, 255, 0);
                    if (a == chosen)
                        this.options[a].Color = new Color(0, 128, 255);
                    this.window.Draw(this.options[a]);
                }
                if (returnValue)
                    break;
                this.window.DispatchEvents();
                this.window.Display();
            }
            this.window.KeyPressed -= window_KeyPressed;
            return chosen;
        }

        private void window_KeyPressed(object sender, KeyEventArgs e)
        {
            if (e.Code == Keyboard.Key.Up && this.chosen > 0)
                this.chosen--;
            else if (e.Code == Keyboard.Key.Down && this.chosen < this.options.Length - 1)
                this.chosen++;
            else if (e.Code == Keyboard.Key.Return)
                this.returnValue = true;
            else if (e.Code == Keyboard.Key.Escape)
            {
                this.chosen = -1;
                this.returnValue = true;
            }
        }
    }
}
 

Thanks for your help
If I solve this issue I'll be uploading my poor space shooter to the project forum soon

 

anything