3 回答

TA貢獻(xiàn)1842條經(jīng)驗(yàn) 獲得超13個(gè)贊
最簡(jiǎn)單的是,您幾乎可以從 github 復(fù)制/粘貼 Wpf 實(shí)現(xiàn),并更改為使用適用于 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);
}
}
然后,只要將代碼放入表單加載處理程序中,它就會(huì)按預(yù)期工作:
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貢獻(xiàn)1829條經(jīng)驗(yàn) 獲得超9個(gè)贊
更新(因?yàn)槟褜㈠e(cuò)誤輸出添加到問(wèn)題中):
預(yù)期類、結(jié)構(gòu)或接口成員聲明類型中的標(biāo)記“+=”無(wú)效
你不能只將 C# 代碼放在類的頂層,你必須遵循一個(gè)最小的結(jié)構(gòu)。您可以將此類代碼放置在類的構(gòu)造函數(shù)方法或其他方法或靜態(tài)初始化塊中(如果您僅在代碼中引用靜態(tài)內(nèi)容)
下次更新(新錯(cuò)誤之后):
使用泛型類型“Cheet”需要 1 個(gè)類型參數(shù)
您正在使用通用包而不是 Wpf 包:
public?class?Cheet?:?Cheet<Key>,?ICheet
很不幸我認(rèn)為他們使用相同的類名

TA貢獻(xiàn)1797條經(jīng)驗(yàn) 獲得超4個(gè)贊
存儲(chǔ)庫(kù)中有一個(gè) WPF 演示應(yīng)用程序,您可以在 Window 構(gòu)造函數(shù)中進(jìn)行初始化:
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 中可能會(huì)循環(huán)遍歷其映射并尋找合適的映射。
我設(shè)置了一個(gè)測(cè)試 WinForms 項(xiàng)目并添加了 Cheet.NET,如果您想將它與 WinForms 一起使用,您似乎需要做一些工作。
Cheet.Core有一個(gè)Cheet<T>類,但它是抽象的。看起來(lái)T是打算成為“鑰匙”類型的。Cheet.Wpf 庫(kù)有一個(gè)Cheet繼承自 的類Cheet<Key>,使用 WPFKey類型。
看來(lái)您需要?jiǎng)?chuàng)建自己的Cheet類(很可能)繼承自Cheet<System.Windows.Forms.Keys>.
我想現(xiàn)在的問(wèn)題是哪個(gè)工作量更大:為 WinForms 實(shí)現(xiàn) Cheet,還是在 WPF 中重新開(kāi)始您的項(xiàng)目
- 3 回答
- 0 關(guān)注
- 175 瀏覽
添加回答
舉報(bào)