为了账号安全,请及时绑定邮箱和手机立即绑定

如何调整 Cheet.NET 以绑定到我的 C# 表单?

如何调整 Cheet.NET 以绑定到我的 C# 表单?

C#
海绵宝宝撒 2023-08-20 09:49:39
所以我在设置一个按键绑定来在我的项目中执行某些操作时遇到问题(右 Alt + 左 Control)我尝试使用一个名为 Cheet.NET 的 API,它表示它可以轻松地调整项目并允许制作可以调用函数的自定义“konami”代码。因此,我使用 Visual Studio 中的 NuGet 包管理器安装了它,保存并重新加载了我的项目。回来后,尝试编写一个简单的按键绑定,我已经从初始化代码中得到了很多错误:// Initializationvar cheet = new Cheet();myUIElement.PreviewKeyDown += cheet.OnKeyDown;cheet.Map("↑ ↑ ↓ ↓", () => { Debug.WriteLine("Voilà!"); } );这是我放置代码的地方:namespace WindowsFormsApp1{    public partial class Form1 : Form    {        // Initialization        var cheet = new Cheet();        myUIElement.PreviewKeyDown += cheet.OnKeyDown;        cheet.Map("↑ ↑ ↓ ↓", () => { Debug.WriteLine("Voilà!"); });        public Form1()        {            InitializeComponent();        }        private void Form1_Load(object sender, EventArgs e)        {        }    }}它无法识别很多代码,并且也给了我很多计算错误:类、结构或接口成员声明中的标记“+=”无效预期元组必须包含至少两个元素 类、结构或接口成员声明中的标记“+=”无效我听说我应该将包“绑定”到我的项目,但我不知道该怎么做。感谢任何未来的帮助。
查看完整描述

3 回答

?
红颜莎娜

TA贡献1842条经验 获得超12个赞

最简单的是,您几乎可以从 github 复制/粘贴 Wpf 实现,并更改为使用适用于 Windows 表单的正确事件处理程序/枚举:


using CheetNET.Core;

using System;

using System.Text.RegularExpressions;

using System.Windows.Forms;


public class Cheet : Cheet<System.Windows.Forms.Keys>

{

    private static readonly Regex LetterKeysNamePattern = new Regex(@"^[a-z]$");

    private static readonly Regex NumberKeysNamePattern = new Regex(@"^[0-9]$");

    private static readonly Regex KeyspadNumberKeysNamePattern = new Regex(@"^kp_[0-9]$");

    private static readonly Regex FunctionKeysNamePattern = new Regex(@"^(?:f[1-9]|f1[0-2])$");


    private PreviewKeyDownEventArgs lastHandledEvent;


    public virtual void OnKeyDown(object sender, PreviewKeyDownEventArgs e)

    {

        if (e == lastHandledEvent)

        {

            return;

        }

        OnKeyDown(e.KeyCode);

        lastHandledEvent = e;

    }


    protected override Keys GetKey(string KeysName)

    {

        if (LetterKeysNamePattern.IsMatch(KeysName))

        {

            return ParseKey(KeysName.ToUpper());

        }

        if (NumberKeysNamePattern.IsMatch(KeysName))

        {

            return ParseKey("D" + KeysName);

        }

        if (KeyspadNumberKeysNamePattern.IsMatch(KeysName))

        {

            return ParseKey(KeysName.Replace("kp_", "NumPad"));

        }

        if (FunctionKeysNamePattern.IsMatch(KeysName))

        {

            return ParseKey(KeysName.ToUpper());

        }

        switch (KeysName)

        {

            case "left":

            case "L":

            case "←":

                return Keys.Left;

            case "up":

            case "U":

            case "↑":

                return Keys.Up;

            case "right":

            case "R":

            case "→":

                return Keys.Right;

            case "down":

            case "D":

            case "↓":

                return Keys.Down;

            case "backspace":

                return Keys.Back;

            case "tab":

                return Keys.Tab;

            case "enter":

                return Keys.Enter;

            case "return":

                return Keys.Return;

            case "shift":

            case "⇧":

                return Keys.LShiftKey;

            case "control":

            case "ctrl":

            case "^":

                return Keys.LControlKey;

            case "alt":

            case "option":

            case "⌥":

                return Keys.Alt;

            case "command":

            case "⌘":

                return Keys.LWin;

            case "pause":

                return Keys.Pause;

            case "capslock":

                return Keys.CapsLock;

            case "esc":

                return Keys.Escape;

            case "space":

                return Keys.Space;

            case "pageup":

                return Keys.PageUp;

            case "pagedown":

                return Keys.PageDown;

            case "end":

                return Keys.End;

            case "home":

                return Keys.Home;

            case "insert":

                return Keys.Insert;

            case "delete":

                return Keys.Delete;

            case "equal":

            case "=":

                return Keys.Oemplus;

            case "comma":

            case ",":

                return Keys.Oemcomma;

            case "minus":

            case "-":

                return Keys.OemMinus;

            case "period":

            case ".":

                return Keys.OemPeriod;

            case "kp_multiply":

                return Keys.Multiply;

            case "kp_plus":

                return Keys.Add;

            case "kp_minus":

                return Keys.Subtract;

            case "kp_decimal":

                return Keys.Decimal;

            case "kp_divide":

                return Keys.Divide;

        }

        throw new ArgumentException(String.Format("Could not map Keys named '{0}'.", KeysName));

    }


    private static Keys ParseKey(string KeysName)

    {

        return (Keys)Enum.Parse(typeof(Keys), KeysName);

    }

}

然后,只要将代码放入表单加载处理程序中,它就会按预期工作:


protected override void OnLoad(EventArgs e)

{

    var cheet = new Cheet();

    PreviewKeyDown += cheet.OnKeyDown;

    cheet.Map("c h e a t", () => { MessageBox.Show("Voilà!"); });


    base.OnLoad(e);

}


查看完整回答
反对 回复 2023-08-20
?
PIPIONE

TA贡献1829条经验 获得超9个赞

更新(因为您已将错误输出添加到问题中):

预期类、结构或接口成员声明类型中的标记“+=”无效

你不能只将 C# 代码放在类的顶层,你必须遵循一个最小的结构。您可以将此类代码放置在类的构造函数方法或其他方法或静态初始化块中(如果您仅在代码中引用静态内容)

下次更新(新错误之后):

使用泛型类型“Cheet”需要 1 个类型参数

您正在使用通用包而不是 Wpf 包:

public class Cheet : Cheet<Key>, ICheet

很不幸我认为他们使用相同的类名

查看完整回答
反对 回复 2023-08-20
?
繁星coding

TA贡献1797条经验 获得超4个赞

存储库中有一个 WPF 演示应用程序,您可以在 Window 构造函数中进行初始化:


public MainWindow()

{

    var cheet = new Cheet();

    PreviewKeyDown += cheet.OnKeyDown;


    cheet.Map("↑ ↑ ↓ ↓ ← → ← → b a", () => { WriteLine("Voilà!"); });


    cheet.Map("i d d q d", () => {

        WriteLine("god mode enabled");

    });

    [etc.]

因此,您可以使用 处理PreviewKeyDown事件cheet.OnKeyDown,并且OnKeyDownCheet 中可能会循环遍历其映射并寻找合适的映射。


我设置了一个测试 WinForms 项目并添加了 Cheet.NET,如果您想将它与 WinForms 一起使用,您似乎需要做一些工作。


Cheet.Core有一个Cheet<T>类,但它是抽象的。看起来T是打算成为“钥匙”类型的。Cheet.Wpf 库有一个Cheet继承自 的类Cheet<Key>,使用 WPFKey类型。


看来您需要创建自己的Cheet类(很可能)继承自Cheet<System.Windows.Forms.Keys>.


我想现在的问题是哪个工作量更大:为 WinForms 实现 Cheet,还是在 WPF 中重新开始您的项目


查看完整回答
反对 回复 2023-08-20
  • 3 回答
  • 0 关注
  • 82 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信