0

i have this problem, i want to create a small game, for an university project using c#, i need to create a small dll library that contains the game logic, but the core of logic is handle the keyboard events. I know that "System.Form.." allow to listen for keyboard events, but is there a method to handle single keyboard events without using the Form library? I need to create a event that is "launched" when a keyboard button is pressed, an event "like" this: public OnKeyPress(KeyCode c)

There is a method? Thanks in advance.

1 Answers1

0

You need something that loads and uses a DLL. The DLL by itself is not going to run. We need some more information regarding how this library will be used. You can create a library that has only processing classes/functions, but somewhere the keyboard will have to be monitored.

Here is a post that discusses global keyboard monitoring. The answer of that question links out to an MSDN article.

Back to what I initially mentioned, you do not need to have the actual events in your library. You can create a library containing only functional code. That code could then be called from a higher level in the overall application where the events are handled. Here is a very simplistic, and honestly hackish, example:

public static class MyKeyboardActions
{
    public void HandleKeyCharPress(char keyPressed)
    {
        switch(keyPressed)
        {
            case 'A':
            // do code for key: A
            break;
            case 'B':
            // do code for key: B
            break;
            // etc...
        }
    }
}

If you want something more specific than this, you would need to elaborate more about your project in your question, then I can expand my answer more if needed. As you can see though, there is no need for a System.Windows.Forms reference with that code.

Community
  • 1
  • 1
gmiley
  • 6,531
  • 1
  • 13
  • 25
  • Thanks for your reply, I have not expressed well my question, in general the dll contains functional code, the dll is used by form application, or console application, inside the dll i want to define the point for start the game, when game start each keyboard event need to go "inside" the dll that process in "realtime" the event, and change some variables of game, and finally the dll update the console or form. – andrea petreti Jan 14 '17 at 12:54
  • The game is based on a set of words to write correctly in the shortest possible time, when a whitespace character is pressed on the keyboard, to be launched an event that is caught, is updated what you're writing a character by character. I think your answer is good solution. – andrea petreti Jan 14 '17 at 12:54
  • The second part of my answer should be exactly what you need then. You want a simple library that contains only game logic code. You want to keep it decoupled from the user interface code. Unfortunately, without samples of your game code, or specifications regarding what the functional code needs to do, this is about as specific of an answer as we can do. You will need to do your analysis to determine what kind of library you will need and what signatures your class members should have. – gmiley Jan 14 '17 at 13:02