Click here to Skip to main content
14,693,599 members
Articles » General Programming » DLLs & Assemblies » General
Tip/Trick
Posted 23 Oct 2020

Tagged as

Stats

2.1K views
2 bookmarked

Cheat Code Keystrokes Handler

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
23 Oct 2020CPOL
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Mohssine EL HARFI
Software Developer (Junior)
Morocco Morocco
I am someone fascinated by gaming, new technologies and programming.
My goal is to advance in programming skills.

Comments and Discussions

 
QuestionThis is pretty cool. Pin
DumpsterJuice26-Oct-20 10:01
MemberDumpsterJuice26-Oct-20 10:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.