C# XNA,我怎么知道按下了什么键?
跳到编辑3: 我计划利用我在网上找到的这个。但是,我无法找出适合其参数C# XNA,我怎么知道按下了什么键?,c#,xna,C#,Xna,跳到编辑3: 我计划利用我在网上找到的这个。但是,我无法找出适合其参数Keys-key的适当变量是如何工作的。我不知道我必须向过程发送哪种类型的变量。我尝试发送我的Keyboard kbState=Keyboard.GetState(),但没有成功。我尝试发送kbState.GetPressedKeys(),但它说它不起作用。我该怎么做 public static string ConvertKeyToChar(Keys key, bool shift) { switch (key)
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;
}
public静态字符串转换器keytochar(Keys-key,bool-shift)
{
开关(钥匙)
{
大小写键。空格:返回“”;
//逃逸序列
大小写键。输入:return“\n”//创建新行
大小写键.Tab:返回“\t”//Tab到右边
//D-数字(字母表上方的条带)
case Keys.D0:返回班次?”):“0”;
案例编号:D1:返回班次?!:“1”;
case Keys.D2:返回班次?@:“2”;
case Keys.D3:返回班次?#“:“3”;
case Keys.D4:返回班次?$:“4”;
case Keys.D5:返回班次?%:“5”;
case Keys.D6:返回班次?^:“6”;
case Keys.D7:返回换档?&“:“7”;
case Keys.D8:返回班次?*:“8”;
case Keys.D9:返回班次?”(“:“9”;
//努帕德
case Keys.NumPad0:返回“0”;
case Keys.NumPad1:返回“1”;
case Keys.NumPad2:返回“2”;
case Keys.NumPad3:返回“3”;
case Keys.numpa4:返回“4”;
case Keys.NumPad5:返回“5”;
case Keys.NumPad6:返回“6”;
case Keys.NumPad7:返回“7”;
case Keys.NumPad8:返回“8”;
case Keys.NumPad9:返回“9”;
大小写键。添加:返回“+”;
大小写键。减法:返回“-”;
大小写键。乘法:返回“*”;
大小写键。除:返回“/”;
大小写键。十进制:返回“”;
//字母表
案例键。A:返回班次?:“A”:“A”;
案例键。B:返回班次?“B”:“B”;
案例键。C:返回换档?“C”:“C”;
案例键。D:返回换档?“D”:“D”;
案例键。E:返回班次?“E”:“E”;
案例键。F:返回班次?“F”:“F”;
案例键.G:返回移位?“G”:“G”;
案例键。H:返回换档?“H”:“H”;
案例键。I:返回班次?“I”:“I”;
案例键。J:返回换档?“J”:“J”;
案例键。K:返回移位?“K”:“K”;
案例键。L:返回班次?“L”:“L”;
案例键。M:返回班次?“M”:“M”;
案例编号:N:返回班次?“N”:“N”;
案例键。O:返回班次?“O”:“O”;
案例键。P:返回班次?“P”:“P”;
案例键。Q:返回移位?“Q”:“Q”;
案例键。R:返回移位?“R”:“R”;
大小写键.S:返回移位?“S”:“S”;
案例键。T:返回移位?“T”:“T”;
案例键。U:返回移位?“U”:“U”;
案例键。V:返回移位?“V”:“V”;
案例键。W:返回换档?“W”:“W”;
大小写键。X:返回移位?“X”:“X”;
案例键。Y:返回移位?“Y”:“Y”;
大小写键。Z:返回移位?“Z”:“Z”;
//原始设备制造商
case Keys.OemOpenBrackets:返回班次?{“:”[”;
case Keys.oemclose括号:返回移位?}“:”];
case Keys.OemComma:返回班次?“:”;
case Keys.OemMinus:返回班次?\:“-”;
case Keys.OemPlus:返回移位?+:“=”;
case Keys.OemQuestion:返回移位?“:”/”;
case Keys.OemSemicolon:返回移位?”:“:;”;
case Keys.OemQuotes:返回移位?\:“;
case Keys.OemPipe:返回班次?”|“:“\\”;
case Keys.OemTilde:返回班次?~:“`”;
}
返回字符串。空;
}
编辑:
所以我在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;
var newkbState=Keyboard.GetState();
Keys[]pressedKeys=newkbState.GetPressedKeys();
newInput=null;
if(newkbState.IsKeyDown(Keys.LeftShift)| newkbState.IsKeyDown(Keys.righshift))
{
shiftNew=true;
}
其他的
{
shiftNew=false;
}
对于(int count=0;count
现在我的问题是,如果我按住S键,然后按O键,它将键入SOSS。我是否可以阻止这种情况发生
编辑2:
var newkbState=Keyboard.GetState();
newkbState=GetKeysPressedBetween(oldkbState,newkbState);
按键[]按按键;
pressedKeys=newkbState.GetPressedKeys()
if(newkbState.IsKeyDown(Keys.LeftShift)| newkbState.IsKeyDown(Keys.RightShift))
{
shiftNew=true;
}
其他的
{
shiftNew=false;
}
newInput=“”;
对于(int count=0;count
好吧,我试过回答者的代码,现在我遇到了问题。发生的是:
1.只需轻敲字母“a”一次,即可输出3个a。
2.当按下a和b两个按钮时,第一个问题发生,我抓住它时,阿巴巴不断重复。如果我解决了第一个问题,我不确定第二个问题是否会解决。这并不能完全解决主要问题,但它解决了代码的一个基本问题
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 GetKeyPressedBetween(键盘状态第一,键盘状态第二)
{
按下键盘状态=新键盘状态();
对于(int i=0;i<8;i++)
{
FieldInfo当前
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;
}