Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Cheat Code Keystrokes Handler

0.00/5 (No votes)
23 Oct 2020 1  
CheatCodeLite to trigger events when a cheat code is caught using keyboard
This is a library that allows you to trigger cheat codes events in your game/application using .NET. The keystrokes handler is not included in this library because it really depends on the context where this library is used, most of the time, games use a third party to catch keystrokes not the one in the .NET Framework.

Introduction

I just didn't find any library doing this, or at least not with the criteria I wanted.

Background

The library is coded using C# and therefore it works on .NET environment like C#, VB.NET, F#.

In the Winform, you still can use "KeyDown" event to catch keystrokes.

Using the Code

Just add a reference to the library in the release https://github.com/melharfi/CheatCodeLite/releases or using nuget:

Install-Package CheatCodeLite -Version 1.0.0

Once it's done, you can then use this code to start on a Winform:

Image 1

using System.Collections.Generic;
using System.Windows.Forms;
using CheatCodeLite;

namespace winform_test
{
    public partial class Form1 : Form
    {
        readonly CheatCodeLite.CheatCodeLite cch;
        public Form1()
        {
            InitializeComponent();
            int elapsedTime = 1000; //one second before cheat code will be reset
            cch = new CheatCodeLite.CheatCodeLite(elapsedTime);

            // first cheat code with an alia "DOOM" to identify the pattern 
            // when event will be triggered
            ChainePattern cp1 = new ChainePattern(new List<int> 
                  { (int)Keys.D, (int)Keys.O, (int)Keys.O, (int)Keys.M }, "DOOM");
            cch.AddChainePattern(cp1);

            // second cheat code
            ChainePattern cp2 = new ChainePattern(new List<int> 
              { (int)Keys.H, (int)Keys.E, (int)Keys.L, (int)Keys.L, (int)Keys.O }, "HELLO");
            cch.AddChainePattern(cp2);

            // add an event handler to be triggered automatically when cheat code is raised
            cch.AddedPattern += CheatCodeHandler_AddedPattern;

            // self handling of keystroke using System.Windows.Forms
            // in other scenarios you can use some other third party to 
            // handle keystrokes like as some game libs do
            this.KeyDown += Form1_KeyDown;
        }
        
        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            // send keystroke to CheatCodeLite
            cch.AddKeystroke(e.KeyValue);
        }
        
        private void CheatCodeHandler_AddedPattern(object sender, PatternEventHandler e)
        {
            // catching event
            MessageBox.Show(e.ChainePattern.Alias + 
                   " cheat code has been triggered", ":-)", MessageBoxButtons.OK, 
                   MessageBoxIcon.Information);
        }
    }
}

Image 2

Pay attention that the value passed to the CheatCodeLite.CheatCodeLite(int keystrokesInterval) is the time allowed to keep chained cheat code, if this time is overwhelmed, then the cheat code in progress is reset.
To disable this timer, just put a very long value like 100000000. :)

Pay attention to some scenarios that will raise some error. When a cheat code is registered like "HELLO" in the first hand, and another cheat code is registered like "HELL", in that case, the second cheat code will hide the first one because cheat code will be reset as soon as it trigger the "HELL" event, so to avoid that, an exception will be thrown to prevent and warn you about that.

Image 3

Same scenario when the reverse happens, when you first add a cheat code like "HELL" and you just add another one like "HELLO", then the second one will be hidden by the first one.

Image 4

Same thing when a duplication is found.

Image 5

Points of Interest

Simple to use, rare library shared to do that "at least I didn't find some".

History

  • 23rd October, 2020: First release

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here