65.9K
CodeProject is changing. Read more.
Home

Cheat Code Keystrokes Handler

starIconstarIconstarIconstarIconstarIcon

5.00/5 (2 votes)

Oct 23, 2020

CPOL

1 min read

viewsIcon

6030

CheatCodeLite to trigger events when a cheat code is caught using keyboard

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:

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);
        }
    }
}

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.

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.

Same thing when a duplication is found.

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