3 回答

TA貢獻1842條經驗 獲得超13個贊
最簡單的是,您幾乎可以從 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);
}

TA貢獻1829條經驗 獲得超9個贊
更新(因為您已將錯誤輸出添加到問題中):
預期類、結構或接口成員聲明類型中的標記“+=”無效
你不能只將 C# 代碼放在類的頂層,你必須遵循一個最小的結構。您可以將此類代碼放置在類的構造函數方法或其他方法或靜態初始化塊中(如果您僅在代碼中引用靜態內容)
下次更新(新錯誤之后):
使用泛型類型“Cheet”需要 1 個類型參數
您正在使用通用包而不是 Wpf 包:
public?class?Cheet?:?Cheet<Key>,?ICheet
很不幸我認為他們使用相同的類名

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>類,但它是抽象的??雌饋鞹是打算成為“鑰匙”類型的。Cheet.Wpf 庫有一個Cheet繼承自 的類Cheet<Key>,使用 WPFKey類型。
看來您需要創建自己的Cheet類(很可能)繼承自Cheet<System.Windows.Forms.Keys>.
我想現在的問題是哪個工作量更大:為 WinForms 實現 Cheet,還是在 WPF 中重新開始您的項目
- 3 回答
- 0 關注
- 160 瀏覽
添加回答
舉報