Jump to content

  • Log In with Google      Sign In   
  • Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

Sign up from the right-hand sidebar on our homepage and read Tuesday's newsletter for details!


We're also offering banner ads on our site from just $5! 1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Handling keyboard input for text purposes


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 KappaG3   Members   -  Reputation: 131

Like
0Likes
Like

Posted 01 July 2013 - 02:31 PM

Over the course of time I've often had to deal with Keyboard input - be it using XNA's abstractions or by directly P/Invoking GetKeyState.

But I've often found myself dealing with a different problem: having to transform that input into a string. And I've often lazily 'solved' it by enumerating through the standard ASCII keys and converting them to either letter or number since their KeyCodes directly translate to their ASCII counterparts.

 

However, I don't quite know how to handle symbols, such as periods, commas, colons, or any other special character that may or may not be on my keyboard. A while ago I found a way to handle modifiers (shift, ctrl+alt) too via P/Invoking VkKeyState and ToAscii (ex. to turn 1 into ! when shift is held), which also works with different keyboard layouts. Still, that leaves the problem that the ASCII code for a period (46) isn't the KeyCode for the period key on my keyboard (190).

 

And all this leads me to a question: Is the path of pain the only choice I've got? Winforms, games and even console applications handle text input flawlessly, so there is probably some API - be it managed or unmanaged - that I am not aware of and I should be using instead. Any clue would be appreciated.

 

Side note: My searches through the web often found threads from people (sometimes on this very forum) who wrapped XNA's input handling (KeyboardStates precisely), but not only that's what I've been doing for all this time, it also seems like a bad and heavily hardcoded way of doing things.


Edited by KappaG3, 01 July 2013 - 02:36 PM.


Sponsor:

#2 radioteeth   Prime Members   -  Reputation: 1103

Like
1Likes
Like

Posted 01 July 2013 - 07:07 PM

I set up my own value system, and create an array that takes a virtual key code, or a keyboard scancode value, and maps it to an internal key value. This internal key value then can be used to index a string table, to retrieve characters for things like typing.. along with a 2nd table of strings for when the shift key is held. This key value is also used for mapping binds, which is why I include all other button inputs into the system, mouse, joystick, etc... that way everything is handled by one input system, user control and string manipulation.



#3 KappaG3   Members   -  Reputation: 131

Like
0Likes
Like

Posted 02 July 2013 - 03:14 AM

I set up my own value system, and create an array that takes a virtual key code, or a keyboard scancode value, and maps it to an internal key value. This internal key value then can be used to index a string table, to retrieve characters for things like typing.. along with a 2nd table of strings for when the shift key is held. This key value is also used for mapping binds, which is why I include all other button inputs into the system, mouse, joystick, etc... that way everything is handled by one input system, user control and string manipulation.

Still, how do you handle keyboard layouts? (English, German, Italian, Japanese..)

One way is to simply ignore they exist, and force the user to use the one you mapped. That's not too bad, some games do it.

Another one is to manually map everything (rip 2013).

But they still seem like an awful lot of work considering there's so many programss out there that, I'll say it again, have no problems with different cultures. 


Edited by KappaG3, 02 July 2013 - 03:26 AM.


#4 KappaG3   Members   -  Reputation: 131

Like
1Likes
Like

Posted 02 July 2013 - 04:47 AM

I found an answer, after endlessly looking for it:

http://www.gamedev.net/topic/543581-keyboard-input-in-xna/

There's apparently no managed way to do it, so I'll need to hook the raw keyboard events myself.



#5 LorenzoGatti   Crossbones+   -  Reputation: 2735

Like
0Likes
Like

Posted 02 July 2013 - 05:19 AM

XNA has message boxes http://msdn.microsoft.com/en-us/library/ff827868.aspx and "software input panels" http://msdn.microsoft.com/en-us/library/ff827869.aspx
Produci, consuma, crepa

#6 KappaG3   Members   -  Reputation: 131

Like
0Likes
Like

Posted 02 July 2013 - 05:37 AM

However, the first one binds me to show message boxes for getting text input, and the second one is unsuitable for PC games.

I think I am fine with what I've found so far, it's the most flexible way.






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS