dpoon

The KeyboardState object is a C# structure - meaning that it's a value type not a reference type. Calling Keyboard.GetState() returns a copy of the KeyboardState structure. This KeyboardState structure appears to be about 32 bytes in size - determined via a call to System.Runtime.InteropServices.Mashal.SizeOf(). Is there a particular reason why KeyboardState is a structure rather than a class type Correct me if I'm wrong but I would have thought returning a class reference would be much more efficient than returning a 32 byte structure.



Re: XNA Framework Implications of Keyboard.GetState() returning a KeyboardState structure.

waruwaru

If it was a class, it may be easier for you to access individual parts. But a 32 bit byte block is probably easier/faster to deal with for unmanaged low level codes (bit operation for example). If you want to deal with a class instead, you can alway write your own class wrapper.




Re: XNA Framework Implications of Keyboard.GetState() returning a KeyboardState structure.

Alex-MyRpg

Another reason for using a structure is so that KeyboardStates can be saved. (i.e. old and new ones can be compared, which is very useful). Previously there was no way to do this as it was a class/reference type.



Re: XNA Framework Implications of Keyboard.GetState() returning a KeyboardState structure.

Mitch Walker - MSFT

The previous replies are correct. It's a struct so that you can easily do previous/current comparisons. At one point it was a class, always returning the same reference (to eliminate allocations), but was difficult to work with. (You'd have to cache the values you care about yourself) We then switched it to a struct, but it was allocating a 256-byte array each time to store the results. We finally packed the data and got it down to the current size, which seems ok.