Vstup textu v XNA
V LoadContent vytvoříme novou instanci třídy InputTextu:
//vyběr jazyka en=angliština cokoliv jiného čestina
//20=delka textu, 0=neomezený text
InputTextu = new InputTextu("en",20);
A Update se v proměnné text vratí psaný text:
InputTextu.Update();
text = InputTextu.vraceno();
Celá třída InputTextu je zde:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Input;
namespace vstupKlavesnice
{
class InputTextu
{
private string text;
private Keys[] keysToCheck = new Keys[] {
Keys.A, Keys.B, Keys.C, Keys.D, Keys.E,
Keys.F, Keys.G, Keys.H, Keys.I, Keys.J,
Keys.K, Keys.L, Keys.M, Keys.N, Keys.O,
Keys.P, Keys.Q, Keys.R, Keys.S, Keys.T,
Keys.U, Keys.V, Keys.W, Keys.X, Keys.Y,
Keys.Z, Keys.Back, Keys.Space,};
private Keys[] numLock = new Keys[]
{
Keys.NumPad0,Keys.NumPad1,Keys.NumPad2,Keys.NumPad3,Keys.NumPad4,Keys.NumPad5,
Keys.NumPad6,Keys.NumPad7,Keys.NumPad8,Keys.NumPad9
};
private Keys[] cisla = new Keys[]
{
Keys.D0,Keys.D1,Keys.D2,Keys.D3,Keys.D4,Keys.D5,Keys.D6,Keys.D7,Keys.D8,Keys.D9,Keys.OemSemicolon,Keys.OemOpenBrackets
};
private KeyboardState currentKeyboardState;
private KeyboardState lastKeyboardState;
private string jazyk;
private int delka;
public InputTextu(string jazyk,int delka)
{
text = "";
this.jazyk = jazyk;
this.delka = delka;
}
public void Update()
{
currentKeyboardState = Keyboard.GetState();
foreach (Keys key in keysToCheck)
{
if (CheckKey(key))
{
AddKeyToText(key);
break;
}
}
foreach (Keys key in numLock)
{
if (CheckKey(key))
{
numLockAddKeyToText(key);
break;
}
}
if (jazyk == "en")
{
foreach (Keys key in cisla)
{
if (CheckKey(key))
{
enAddKeyToText(key);
break;
}
}
}
else
{
foreach (Keys key in cisla)
{
if (CheckKey(key))
{
czAddKeyToText(key);
break;
}
}
}
lastKeyboardState = currentKeyboardState;
}
private void czAddKeyToText(Keys key)
{
string newChar = "";
//délka textu
if (delka != 0)
if (text.Length >= delka)
return;
switch (key)
{
case Keys.D2:
newChar += "ě";
break;
case Keys.D3:
newChar += "š";
break;
case Keys.D4:
newChar += "č";
break;
case Keys.D5:
newChar += "ř";
break;
case Keys.D6:
newChar += "ž";
break;
case Keys.D7:
newChar += "ý";
break;
case Keys.D8:
newChar += "á";
break;
case Keys.D9:
newChar += "í";
break;
case Keys.D0:
newChar += "é";
break;
case Keys.OemOpenBrackets:
newChar += "ú";
break;
case Keys.OemSemicolon:
newChar += "ů";
break;
}
if (currentKeyboardState.IsKeyDown(Keys.RightShift) ||
currentKeyboardState.IsKeyDown(Keys.LeftShift))
{
newChar = "";
//délka textu
if (delka != 0)
if (text.Length >= delka)
return;
switch (key)
{
case Keys.D1:
newChar += "1";
break;
case Keys.D2:
newChar += "2";
break;
case Keys.D3:
newChar += "3";
break;
case Keys.D4:
newChar += "4";
break;
case Keys.D5:
newChar += "5";
break;
case Keys.D6:
newChar += "6";
break;
case Keys.D7:
newChar += "7";
break;
case Keys.D8:
newChar += "8";
break;
case Keys.D9:
newChar += "9";
break;
case Keys.D0:
newChar += "0";
break;
case Keys.OemOpenBrackets:
newChar += "";
break;
case Keys.OemSemicolon:
newChar += "";
break;
}
}
text += newChar;
}
private void numLockAddKeyToText(Keys key)
{
string newChar = "";
//délka textu
if (delka != 0)
if (text.Length >= delka)
return;
switch (key)
{
case Keys.NumPad0:
newChar += "0";
break;
case Keys.NumPad1:
newChar += "1";
break;
case Keys.NumPad2:
newChar += "2";
break;
case Keys.NumPad3:
newChar += "3";
break;
case Keys.NumPad4:
newChar += "4";
break;
case Keys.NumPad5:
newChar += "5";
break;
case Keys.NumPad6:
newChar += "6";
break;
case Keys.NumPad7:
newChar += "7";
break;
case Keys.NumPad8:
newChar += "8";
break;
case Keys.NumPad9:
newChar += "9";
break;
}
text += newChar;
}
private void enAddKeyToText(Keys key)
{
string newChar = "";
//délka textu
if (delka != 0)
if (text.Length >= delka)
return;
switch (key)
{
case Keys.D1:
newChar += "1";
break;
case Keys.D2:
newChar += "2";
break;
case Keys.D3:
newChar += "3";
break;
case Keys.D4:
newChar += "4";
break;
case Keys.D5:
newChar += "5";
break;
case Keys.D6:
newChar += "6";
break;
case Keys.D7:
newChar += "7";
break;
case Keys.D8:
newChar += "8";
break;
case Keys.D9:
newChar += "9";
break;
case Keys.D0:
newChar += "0";
break;
}
text += newChar;
}
private void AddKeyToText(Keys key)
{
string newChar = "";
//délka textu
if(delka !=0)
if (text.Length >= delka && key != Keys.Back)
return;
switch (key)
{
case Keys.A:
newChar += "a";
break;
case Keys.B:
newChar += "b";
break;
case Keys.C:
newChar += "c";
break;
case Keys.D:
newChar += "d";
break;
case Keys.E:
newChar += "e";
break;
case Keys.F:
newChar += "f";
break;
case Keys.G:
newChar += "g";
break;
case Keys.H:
newChar += "h";
break;
case Keys.I:
newChar += "i";
break;
case Keys.J:
newChar += "j";
break;
case Keys.K:
newChar += "k";
break;
case Keys.L:
newChar += "l";
break;
case Keys.M:
newChar += "m";
break;
case Keys.N:
newChar += "n";
break;
case Keys.O:
newChar += "o";
break;
case Keys.P:
newChar += "p";
break;
case Keys.Q:
newChar += "q";
break;
case Keys.R:
newChar += "r";
break;
case Keys.S:
newChar += "s";
break;
case Keys.T:
newChar += "t";
break;
case Keys.U:
newChar += "u";
break;
case Keys.V:
newChar += "v";
break;
case Keys.W:
newChar += "w";
break;
case Keys.X:
newChar += "x";
break;
case Keys.Y:
newChar += "y";
break;
case Keys.Z:
newChar += "z";
break;
case Keys.Space:
newChar += " ";
break;
case Keys.D1:
newChar += "+";
break;
case Keys.D2:
newChar += "ě";
break;
case Keys.Back:
if (text.Length != 0)
text = text.Remove(text.Length - 1);
return;
}
if (currentKeyboardState.IsKeyDown(Keys.RightShift) ||
currentKeyboardState.IsKeyDown(Keys.LeftShift))
{
newChar = newChar.ToUpper();
}
text += newChar;
}
private bool CheckKey(Keys theKey)
{
return lastKeyboardState.IsKeyUp(theKey) && currentKeyboardState.IsKeyDown(theKey);
}
public string vraceno()
{
return text;
}
}
}