• Advertisement
Sign in to follow this  

Handling Input (C# Console)

This topic is 4487 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey Guys, I decided to finally check out C# for myself to see if it's everything people say it is. So far, seems like it is! Anyway, I'm writing a little Console game, and struggling with input handling. I'm using the Console object for all input, but it feels a little bit like I'm forcing it to handle real-time game inputs. So I'm curious if there is another approach I could take with this? Some sort of input specific object?
And just incase someone can maybe help me with my current system, here is my input manager right now. Basically the current problem is that holding down more than one key seems to cancel out other key presses. For example, I'm holding the right arrow key and that is registering, but if I then hit the left arrow as well, the right arrow is no longer being read.
using System;
using BOPOA;

namespace BOPOA
{
	// Enums
	//////////

	// This different actions to be mapped
	public enum ACTIONS : int { LEAVE_GAME = 0, RIGHT, NUM_ACTIONS };

	public static class InputManager
	{
		// Members
		////////////

		// This will store the current key information
		private static ConsoleKeyInfo m_sKeyInfo;

		// This will key presses related to our actions
		private static int[] m_siActions;

		// We need to store the state of each action
		private static bool[] m_sbActionStates;


		// Methods
		////////////

		// Maps all actions to default keys
		private static void _restoreDefaultMaps()
		{
			// Assign the default values
			m_siActions[(int)ACTIONS.LEAVE_GAME] = (int)ConsoleKey.Escape;
			m_siActions[(int)ACTIONS.RIGHT] = (int)ConsoleKey.RightArrow;
		}

		// Checks each action and stores the state
		private static void _updateActionStates()
		{
			// Loops through all the actions
			for (int i = 0; i < (int)ACTIONS.NUM_ACTIONS; i++)
			{
				if((int)m_sKeyInfo.Key == m_siActions)
					m_sbActionStates =  true;
			}
		}

		// Resets all the action states to false
		private static void _resetActionStates()
		{
			// Loops through all the actions
			for (int i = 0; i < (int)ACTIONS.NUM_ACTIONS; i++)
			{
				m_sbActionStates = false;
			}
		}

		// Initializes the manager
		public static void initialize()
		{
			// Create the key info object
			m_sKeyInfo	= new ConsoleKeyInfo();

			// Create an array for all our action
			m_siActions			= new int[(int)ACTIONS.NUM_ACTIONS];
			m_sbActionStates	= new bool[(int)ACTIONS.NUM_ACTIONS];

			// Start with default action maps
			_restoreDefaultMaps();
		}

		// Updates all the key presses
		public static void update()
		{
			// Make sure there is a key press to check before we go any further
			if (Console.KeyAvailable == false)
			{
				// Set all the states to false
				_resetActionStates();

				return;
			}

			while (Console.KeyAvailable)
			{
				// Get the key info without displaying it
				m_sKeyInfo = Console.ReadKey(true);

				// Update the action states
				_updateActionStates();
			}
		}

		// Shuts the class down
		public static void shutdown()
		{
		}

		// Assigns a key to an action (actions are defined above)
		public static void assignKeyToAction(ACTIONS a_eActionToMap, ConsoleKey a_eKeyToMap)
		{
			// Assign the key to the actions
			m_siActions[(int)a_eActionToMap] = (int)a_eKeyToMap;
		}

		// Checks if an action is happening
		// Returns true if the key corisponding with this action
		// was pressed
		public static bool checkAction(ACTIONS a_eAction)
		{
			// Return the state of the action
			return m_sbActionStates[(int)a_eAction];
		}
	}
}



Thanks guys! Matt Hughson

Share this post


Link to post
Share on other sites
Advertisement
Well, the quick answer is that the console isn't really meant to handle complex input. It's only a terminal, and you shouldn't need more than one [modifier]+key at the same time.

The only thing that comes to mind is to P/Invoke the .dll that provides GetAsyncKeyState. I'm still looking....

Share this post


Link to post
Share on other sites
Quote:
Original post by jfclavette
The only thing that comes to mind is to P/Invoke the .dll that provides GetAsyncKeyState. I'm still looking....


Would you be able to explain that bit?

Matt Hughson

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement