在 C# 中这样做没有意义。无论如何,此类代码必须在客户端执行,因此它必须是 Javascript(除非您想将每个客户端击键发送到服务器,这会非常慢)
无论如何,这是一个 C# 实现。它适用于 WPF,但将其适应其他技术并不困难。
private static readonly Key[] _konamiCode = new[] { Key.Up, Key.Up, Key.Down, Key.Down, Key.Left, Key.Right, Key.Left, Key.Right, Key.B, Key.A };
int _konamiCurrentIndex = 0;
protected override void OnPreviewKeyDown(KeyEventArgs e)
{
base.OnPreviewKeyDown(e);
if (e.Key == _konamiCode[_konamiCurrentIndex])
{
_konamiCurrentIndex++;
if (_konamiCurrentIndex == _konamiCode.Length)
{
_konamiCurrentIndex = 0;
KonamiEasterEgg();
}
}
else
{
_konamiCurrentIndex = 0;
}
}
void KonamiEasterEgg()
{
// whatever you want to do when the Konami code is entered...
}
Run Code Online (Sandbox Code Playgroud)