Skip to content

Instantly share code, notes, and snippets.

@Mordo95
Created September 4, 2021 20:33
Show Gist options
  • Save Mordo95/52f69eb74a694739430f54cbf639fb4b to your computer and use it in GitHub Desktop.
Save Mordo95/52f69eb74a694739430f54cbf639fb4b to your computer and use it in GitHub Desktop.
Multi-player keyboard binder
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Linq;
using System.Threading;
namespace InputStuffaaaaa
{
public interface IKeyBinding
{
bool isDown();
}
public abstract class KeyBinding : IKeyBinding
{
public int KeyCode { get; set; }
public abstract bool isDown();
}
public class KeyboardKeyBinding : KeyBinding
{
[DllImport("User32.dll")]
public static extern short GetAsyncKeyState(int key);
public override bool isDown()
{
return (GetAsyncKeyState(KeyCode) & 0x8000) != 0;
}
}
public class KeyBinder
{
public List<Dictionary<string, IKeyBinding>> KeyBindings { get; set; } = new List<Dictionary<string, IKeyBinding>>();
public void LoadMap(Dictionary<string, IKeyBinding> map)
{
KeyBindings.Add(map);
}
public bool IsKeyDown(string key)
{
return KeyBindings.Find(x => x.First(y => y.Key == key).Value.isDown()) != null;
}
}
public class MultiplayerKeyBinder
{
public Dictionary<string, KeyBinder> Binders { get; set; } = new Dictionary<string, KeyBinder>();
public void AddBinderForPlayer(string player, KeyBinder binder)
{
Binders.Add(player, binder);
}
public bool IsKeyDownForPlayer(string player, string key)
{
return Binders[player].IsKeyDown(key);
}
}
class Program
{
static void Main(string[] args)
{
MultiplayerKeyBinder multiplayerKeyBinder = new MultiplayerKeyBinder();
Dictionary<string, IKeyBinding> keyboardMapP1 = new Dictionary<string, IKeyBinding>
{
{ "LEFT", new KeyboardKeyBinding() { KeyCode = 0x41 } },
{ "UP", new KeyboardKeyBinding() { KeyCode = 0x57 } },
{ "DOWN", new KeyboardKeyBinding() { KeyCode = 0x53 } },
{ "RIGHT", new KeyboardKeyBinding() { KeyCode = 0x44 } }
};
KeyBinder p1Binder = new KeyBinder();
p1Binder.LoadMap(keyboardMapP1);
multiplayerKeyBinder.AddBinderForPlayer("P1", p1Binder);
Dictionary<string, IKeyBinding> keyboardMapP2 = new Dictionary<string, IKeyBinding>
{
{ "LEFT", new KeyboardKeyBinding() { KeyCode = 0x25 } },
{ "UP", new KeyboardKeyBinding() { KeyCode = 0x26 } },
{ "DOWN", new KeyboardKeyBinding() { KeyCode = 0x28 } },
{ "RIGHT", new KeyboardKeyBinding() { KeyCode = 0x27 } }
};
KeyBinder p2Binder = new KeyBinder();
p2Binder.LoadMap(keyboardMapP2);
multiplayerKeyBinder.AddBinderForPlayer("P2", p2Binder);
while (true)
{
if (multiplayerKeyBinder.IsKeyDownForPlayer("P1", "LEFT")) Console.WriteLine("P1 LEFT");
if (multiplayerKeyBinder.IsKeyDownForPlayer("P1", "UP")) Console.WriteLine("P1 UP");
if (multiplayerKeyBinder.IsKeyDownForPlayer("P1", "DOWN")) Console.WriteLine("P1 DOWN");
if (multiplayerKeyBinder.IsKeyDownForPlayer("P1", "RIGHT")) Console.WriteLine("P1 RIGHT");
if (multiplayerKeyBinder.IsKeyDownForPlayer("P2", "LEFT")) Console.WriteLine("P2 LEFT");
if (multiplayerKeyBinder.IsKeyDownForPlayer("P2", "UP")) Console.WriteLine("P2 UP");
if (multiplayerKeyBinder.IsKeyDownForPlayer("P2", "DOWN")) Console.WriteLine("P2 DOWN");
if (multiplayerKeyBinder.IsKeyDownForPlayer("P2", "RIGHT")) Console.WriteLine("P2 RIGHT");
Thread.Sleep(100);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment