본문 바로가기

프로그램 경험/.Net

[C#] 프로그램에 치트키 넣기


스타크래프트에서 사용하는 치트키는 어떻게 만들까 하는 생각에 간단하게 이렇게 하면 되지 않을까 하는 생각으로 만들어 봤다.


using System;
using System.Drawing;
using System.Windows.Forms;

namespace Exams
{
    public partial class CheatKey : Form
    {

        private string _cheatKey;
        private bool _cheatStart = false;

        public CheatKey()
        {
            InitializeComponent();
        }

        private void CheatKey_KeyUp(object sender, KeyEventArgs e)
        {
            //엔터로 치트 시작
            if (e.KeyData == Keys.Enter)
            {
                //엔터 1번이면 시작 2번째면 중지
                _cheatStart = !_cheatStart;

                //엔터 2번째에 입력된 값을 비교해본다.
                if (!_cheatStart && _cheatKey.ToLower().Equals("abcd"))
                {
                    MessageBox.Show(_cheatKey);
                }

                //치트값 클리어
                _cheatKey = "";
            }

            //치트가 시작되어 있으면서 0부터 z까지의 값일때면 
            //입력데이터 누적 저장
            if (_cheatStart && (e.KeyValue >= 48 && e.KeyValue <= 122))
            {
                _cheatKey += e.KeyData.ToString();
            }
        }
    }
}