跳转到编辑3:
我打算利用我在网上找到的这个。但是,我无法弄清楚适当的变量如何适用于Keys key
的参数。我无法弄清楚我必须向程序发送什么类型的变量。我尝试发送Keyboard kbState = Keyboard.GetState()
,但它没有用。我尝试发送kbState.GetPressedKeys()
,但它表示无效。我该怎么做?
public static string ConvertKeyToChar(Keys key, bool shift)
{
switch (key)
{
case Keys.Space: return " ";
// Escape Sequences
case Keys.Enter: return "\n"; // Create a new line
case Keys.Tab: return "\t"; // Tab to the right
// D-Numerics (strip above the alphabet)
case Keys.D0: return shift ? ")" : "0";
case Keys.D1: return shift ? "!" : "1";
case Keys.D2: return shift ? "@" : "2";
case Keys.D3: return shift ? "#" : "3";
case Keys.D4: return shift ? "$" : "4";
case Keys.D5: return shift ? "%" : "5";
case Keys.D6: return shift ? "^" : "6";
case Keys.D7: return shift ? "&" : "7";
case Keys.D8: return shift ? "*" : "8";
case Keys.D9: return shift ? "(" : "9";
// Numpad
case Keys.NumPad0: return "0";
case Keys.NumPad1: return "1";
case Keys.NumPad2: return "2";
case Keys.NumPad3: return "3";
case Keys.NumPad4: return "4";
case Keys.NumPad5: return "5";
case Keys.NumPad6: return "6";
case Keys.NumPad7: return "7";
case Keys.NumPad8: return "8";
case Keys.NumPad9: return "9";
case Keys.Add: return "+";
case Keys.Subtract: return "-";
case Keys.Multiply: return "*";
case Keys.Divide: return "/";
case Keys.Decimal: return ".";
// Alphabet
case Keys.A: return shift ? "A" : "a";
case Keys.B: return shift ? "B" : "b";
case Keys.C: return shift ? "C" : "c";
case Keys.D: return shift ? "D" : "d";
case Keys.E: return shift ? "E" : "e";
case Keys.F: return shift ? "F" : "f";
case Keys.G: return shift ? "G" : "g";
case Keys.H: return shift ? "H" : "h";
case Keys.I: return shift ? "I" : "i";
case Keys.J: return shift ? "J" : "j";
case Keys.K: return shift ? "K" : "k";
case Keys.L: return shift ? "L" : "l";
case Keys.M: return shift ? "M" : "m";
case Keys.N: return shift ? "N" : "n";
case Keys.O: return shift ? "O" : "o";
case Keys.P: return shift ? "P" : "p";
case Keys.Q: return shift ? "Q" : "q";
case Keys.R: return shift ? "R" : "r";
case Keys.S: return shift ? "S" : "s";
case Keys.T: return shift ? "T" : "t";
case Keys.U: return shift ? "U" : "u";
case Keys.V: return shift ? "V" : "v";
case Keys.W: return shift ? "W" : "w";
case Keys.X: return shift ? "X" : "x";
case Keys.Y: return shift ? "Y" : "y";
case Keys.Z: return shift ? "Z" : "z";
// Oem
case Keys.OemOpenBrackets: return shift ? "{" : "[";
case Keys.OemCloseBrackets: return shift ? "}" : "]";
case Keys.OemComma: return shift ? "<" : ",";
case Keys.OemPeriod: return shift ? ">" : ".";
case Keys.OemMinus: return shift ? "_" : "-";
case Keys.OemPlus: return shift ? "+" : "=";
case Keys.OemQuestion: return shift ? "?" : "/";
case Keys.OemSemicolon: return shift ? ":" : ";";
case Keys.OemQuotes: return shift ? "\"" : "'";
case Keys.OemPipe: return shift ? "|" : "\\";
case Keys.OemTilde: return shift ? "~" : "`";
}
return string.Empty;
}
编辑: 所以我在XNA的更新过程中试过这个。
var newkbState = Keyboard.GetState();
Keys[] pressedKeys = newkbState.GetPressedKeys();
newInput = null;
if (newkbState.IsKeyDown(Keys.LeftShift) || newkbState.IsKeyDown(Keys.RightShift))
{
shiftNew = true;
}
else
{
shiftNew = false;
}
for (int count = 0; count < pressedKeys.Length; count++)
{
newInput += ConvertKeyToChar(pressedKeys[count], shiftNew) ;
}
if (newInput != oldInput)
{
text2 += newInput;
}
oldInput = newInput;
现在我的问题是,如果我按住S并按下O,它将输入SOSS。无论如何我可以阻止这种情况发生吗?
EDIT2: var newkbState = Keyboard.GetState(); newkbState = GetKeysPressedBetween(oldkbState,newkbState); Keys [] pressedKeys; pressedKeys = newkbState.GetPressedKeys();
if (newkbState.IsKeyDown(Keys.LeftShift) || newkbState.IsKeyDown(Keys.RightShift))
{
shiftNew = true;
}
else
{
shiftNew = false;
}
newInput = "";
for (int count = 0; count < pressedKeys.Length; count++)
{
newInput += ConvertKeyToChar(pressedKeys[count], shiftNew);
}
if ((newInput != oldInput) && (newkbState != oldkbState))
{
text2 += newInput;
}
oldInput = newInput;
oldkbState = newkbState;
好吧,我尝试了回答者的代码,现在我遇到了问题。发生的事情是: 1.只需输出字母“a”一次输出3 a。 2.当按下两个按钮时,a和b,第一个问题发生,abababababab只是在我抓住它时不断重复。如果我解决了第一个问题,我不确定第二个问题是否会解决。
答案 0 :(得分:1)
这并没有完全解决主要问题,但它解决了代码的基本问题。
KeyboardState GetKeysPressedBetween(KeyboardState first, KeyboardState second)
{
KeyboardState pressed = new KeyboardState();
for (int i = 0; i < 8; i++)
{
FieldInfo currentStateI = typeof(KeyboardState).GetField("currentState" + i, BindingFlags.NonPublic | BindingFlags.Instance);
uint firstCurrentStateI = (uint)currentStateI.GetValue(first);
uint secondCurrentStateI = (uint)currentStateI.GetValue(second);
currentStateI.SetValueDirect(__makeref(pressed), ~firstCurrentStateI & secondCurrentStateI);
}
return pressed;
}
此功能将为您提供KeyboardState
,指示在两帧之间按下了哪些键。你需要这样的东西才能有任何明智的打字能力。
基本上你需要做的是存储最后一帧的KeyboardState
,并用它来与当前帧进行比较,所以你只需在按下它后的第一帧键入一个键。 / p>
您应该可以将其与编辑中的现有代码一起使用。