console application multi key input

-1

I'm trying to get multi-key input to work on a console application. I have a separate thread running and updating a "keyboard reader" that currently doesn't work because I don't know the .Net Core framework works. here's what I've got so far. (I've included WindowsBase.dll and PresentationCore.dll from the same place as a WinForms application) I also want this to be multi-platform so using thing's like User32.dll wouldn't work

using System;
using System.Collections.Generic;
using System.Windows.Input;
using System.Windows;

namespace Handlers.Input
{
    class InputHandler : UIElement
    {

        public InputHandler()
        {
            Focusable = true;
            IsEnabled = true;
        }
    }

    static class InputSystem
    {
        private static List<Key> keysDown;
        private static List<Key> keysDownLast;
        private static List<MouseEventArgs> buttonsDown;
        private static List<MouseEventArgs> buttonsDownLast;
        private static InputHandler window;
        public static void Initialize()
        {
            keysDown        = new List<Key>();
            keysDownLast    = new List<Key>();
            buttonsDown     = new List<MouseEventArgs>();
            buttonsDownLast = new List<MouseEventArgs>();

            window = new InputHandler();
            Keyboard.Focus(window);
            window.CaptureMouse();

            window.KeyDown   += Game_KeyDown;
            window.KeyUp     += Game_KeyUp;     
            window.MouseDown += Game_MouseDown;
            window.MouseUp   += Game_MouseUp;

        }
        public static void Update()
        {
            keysDownLast    = new List<Key>(keysDown);
            buttonsDownLast = new List<MouseEventArgs>(buttonsDown);
        }                             
        private static void Game_KeyUp(object sender, KeyEventArgs e)
        {
            while (keysDown.Contains(e.Key))
                keysDown.Remove(e.Key);
        }

        private static void Game_KeyDown(object sender, KeyEventArgs e)
        {
            if (!keysDown.Contains(e.Key))
                keysDown.Add(e.Key);
        }

        private static void Game_MouseUp(object sender, MouseEventArgs e)
        {
            while (buttonsDown.Contains(e))
                buttonsDown.Remove(e);
        }

        private static void Game_MouseDown(object sender, MouseEventArgs e)
        {
            if (!buttonsDown.Contains(e))
                buttonsDown.Add(e);
        }

        public static bool KeyPress(Key key)
        {
            return (keysDown.Contains(key) && !keysDownLast.Contains(key));
        }
        public static bool KeyRelease(Key key)
        {
            return (!keysDown.Contains(key) && keysDownLast.Contains(key));
        }
        public static bool KeyDown(Key key)
        {
            return (keysDown.Contains(key));
        }

        public static bool MousePress(MouseEventArgs button)
        {
            return (buttonsDown.Contains(button) && !buttonsDownLast.Contains(button));
        }
        public static bool MouseRelease(MouseEventArgs button)
        {
            return (!buttonsDown.Contains(button) && buttonsDownLast.Contains(button));
        }
        public static bool MouseDown(MouseEventArgs button)
        {
            return (buttonsDown.Contains(button));
        }
    }
}

If I could set a Focus for my console application I think this could work but that's pretty deep into the WinForms documentation probably. Edit: so i got it to "run" but now there's a problem. it says that the dll's should be reference through reflection only: "System.BadImageFormatException HResult=0x80131058 Message=Could not load file or assembly 'WindowsBase, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. Reference assemblies should not be loaded for execution. They can only be loaded in the Reflection-only loader context. (0x80131058) Source= StackTrace:

Inner Exception 1: BadImageFormatException: Cannot load a reference assembly for execution. "

c#
winforms
.net-core
console-application
asked on Stack Overflow Mar 4, 2020 by jklw10 • edited Mar 5, 2020 by jklw10

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0