How to define lowercase keys using key enumeration in C #?

for (Keys k = Keys.A; k <= Keys.Z; k++) { if (KeyPress(k)) Text += k; } 

This code identifies the key buttons that I pressed on the keyboard and prints out which key is pressed in the console. However, I pressed "a" on the keyboard, but it appeared as "A". How to fix it?

+4
source share
2 answers

After looking at your provided code, this should be a simple modification:

 [...] for (Keys k = Keys.A; k <= Keys.Z; k++) { if (KeyPress(k)) { if (keyboardState.IsKeyDown(Keys.LeftShift) || keyboardState.IsKeyDown(Keys.RightShift)) //if shift is held down Text += k.ToString().ToUpper(); //convert the Key enum member to uppercase string else Text += k.ToString().ToLower(); //convert the Key enum member to lowercase string } } [...] 
0
source

This is a common problem. The problem is that XNA is not designed to enter text input from the keyboard. It is designed to obtain the current status of each key on the keyboard.

For now, you can write your own code to check things like shift wrapping, etc., for keys that you don't use (e.g., another language, different format). What most people do is get windows to do this for you.

If you want the text to appear as expected, you basically need to recreate a few things and get windows to do it for you.

See the Promit post here: http://www.gamedev.net/topic/457783-xna-getting-text-from-keyboard/

Using:

Add your code to your solution. Call EventInput.Initialize in your game's Initialize` method and subscribe to its event:

 EventInput.CharEntered += HandleCharEntered; public void HandleCharEntered(object sender, CharacterEventArgs e) { var charEntered = e.Character; // Do something with charEntered } 

Note. If you allow you to enter any character (for example, foreign ones using umlauts, etc.), make sure that your SpriteFont supports them! (If not, maybe just check if your font supports it before adding it to a text string or getting spritefont to use a special "invalid" character).

+9
source

Source: https://habr.com/ru/post/1381721/


All Articles