XNA - Keyboard text input

Posted by Sekhat on Stack Overflow See other posts from Stack Overflow or by Sekhat
Published on 2008-12-17T17:24:53Z Indexed on 2010/05/03 2:18 UTC
Read the original article Hit count: 588

Filed under:
|
|

Okay, so basically I want to be able to retrieve keyboard text. Like entering text into a text field or something. I'm only writing my game for windows. I've disregarded using Guide.BeginShowKeyboardInput because it breaks the feel of a self contained game, and the fact that the Guide always shows XBOX buttons doesn't seem right to me either. Yes it's the easiest way, but I don't like it.

Next I tried using System.Windows.Forms.NativeWindow. I created a class that inherited from it, and passed it the Games window handle, implemented the WndProc function to catch WM_CHAR (or WM_KEYDOWN) though the WndProc got called for other messages, WM_CHAR and WM_KEYDOWN never did. So I had to abandon that idea, and besides, I was also referencing the whole of Windows forms, which meant unnecessary memory footprint bloat.

So my last idea was to create a Thread level, low level keyboard hook. This has been the most successful so far. I get WM_KEYDOWN message, (not tried WM_CHAR yet) translate the virtual keycode with Win32 funcation MapVirtualKey to a char. And I get my text! (I'm just printing with Debug.Write at the moment)

A couple problems though. It's as if I have caps lock on, and an unresponsive shift key. (Of course it's not however, it's just that there is only one Virtual Key Code per key, so translating it only has one output) and it adds overhead as it attaches itself to the Windows Hook List and isn't as fast as I'd like it to be, but the slowness could be more due to Debug.Write.

Has anyone else approached this and solved it, without having to resort to an on screen keyboard? or does anyone have further ideas for me to try?

thanks in advance.

note: This is cross posted from the XNA Creators Forums, so if I get an answer there I'll post it here and Vice-Versa

Question asked by Jimmy

Maybe I'm not understanding the question, but why can't you use the XNA Keyboard and KeyboardState classes?

My comment:

It's because though you can read keystates, you can't get access to typed text as and how it is typed by the user.

So let me further clarify. I want to implement being able to read text input from the user as if they are typing into textbox is windows. The keyboard and KeyboardState class get states of all keys, but I'd have to map each key and combination to it's character representation. This falls over when the user doesn't use the same keyboard language as I do especially with symbols (my double quotes is shift + 2, while american keyboards have theirs somewhere near the return key).

© Stack Overflow or respective owner

Related posts about XNA

Related posts about c#