Sign in to follow this  
FenixRoA

DirectInput Buffered Keyboard

Recommended Posts

OK, so I switched to a buffered keyboard. The codes that come in however are almost nonsensical garbage. The keys do not match up to any ASCII or Unicode chart but rather to whatever the hardware sends. So I have been mapping each letter, number, and control key (shift, enter, insert, etc.) into an array, and now I have a massive class that handles all things keyboard related. The struct for each key is as follows:
[source language = "c#"]
public struct OneKey
{

	public enum State
	{
		Norm = 0,
		Shift= 1
	}

	public bool hasChar;
	public byte KeyCode;
	public char [] KeyChar;

	public OneKey(byte kCode,char kChar, char ShiftChar)
	{
		hasChar = true;
		KeyCode = kCode;
		KeyChar = new Char [2];
		KeyChar[State.Norm] = kChar;
		KeyChar[State.Shift] = ShiftChar;
	}

	public OneKey(byte kCode, char kChar)
	{
		hasChar = true;
		KeyCode = kCode;
		KeyChar = new Char[1];
		KeyChar[State.Norm] = kChar;
	}
	public OneKey(byte kCode)
	{
		hasChar = false;
		KeyCode = kCode;
	}
}

Is this the best way to go about this? is there no function that will do this for me (give me the character (or string) represented by the key pressed or the keycode (as I require) that is affected by caps lock and shift as necessary)?

Share this post


Link to post
Share on other sites
To my knowledge there is no such dinput function or macro to handle this. You'll have to roll your own switch statement ala

DWORD keyPressed;
switch( keyPressed )
{
case DIK_A:
case DIK_B:
.
.
.
case DIK_Z:
};

I just double checked the dinput documentation and I didn't find any, regardless it shouldn't take you longer then 10 mins to write a function to do the conversion for you.

- Dave Neubelt

Share this post


Link to post
Share on other sites
DirectInput is not really designed to work with text input.

I suggest that you use the normal windows events for regular textinputs and directX for controlling the character.

Share this post


Link to post
Share on other sites
If you're going to go the winproc route of using text-input you will want to catch the WM_CHAR message. Remeber in your message loop, you need to call TranslateMessage to turn those WM_KEYDOWN message's into WM_CHAR's. I believe the wParam of WM_CHAR tell's you if shift is being held or capslock is on, but it could be the lword, you'll have to double check the docs.

But going against the previous posters advice I'd write a function that takes a direct input DWORD and converts it to a char, it's a quick copy paste job.

- Dave Neubelt

Share this post


Link to post
Share on other sites
take a look at
http://www.gamedev.net/reference/programming/features/scan2asc/
.
Yuck.
To do this in Managed land you need to pinvoke User32.dll. If you are interested in following this path browse through the Common directory of of the Managed samples in the oct 2004 update, they do a bunch of similar garbage.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this