I have an interrupt that on keyboard keypress gives me the coordinating key. When the number lock is ON, the keys come up as Numpad1, Numpad2, etc. When the number lock is OFF, the numpad keys turn into End, Left, Right, Up, PageLock... I cant have this happening because I need to keep those keys (Left, Right..) as the actual arrow keys. Id rather have the input not be affected by the number lock at all. However, I can't seem to mitigate this as I am getting the input from XNA directly. Can someone else confirm this is happening in XNA 3.1 on VS 2008?
问题:
回答1:
XNA internally uses the win32 function GetKeyboardState
to determine the keyboard state. This function cannot differentiate between the number pad, the arrow keys, and the insert group.
Apparently you are able to differentiate between the keys by using the WM_KEYDOWN
message (and its friends).
I'll leave the interop required to get win32 messages in an XNA application, and how to check the data for the message, as an exercise.
(Personally I would recommend simply modifying your control scheme. Or just observing the user's num-lock state. Generally speaking it is a bad idea to take over the functionality of something like the num-lock key!)
回答2:
You can define 2 keys for each action in your game.
For example, instead of Keys.Left for moving left, have another key allocated for it (Numpad4).
In your input checking code, you would simply check if any of these 2 keys are pressed:
For example:
if (IsKeyPressed(Keys.Numpad4) || IsKeyPressed(Keys.Left)
{
// Do some action.
}
Off course you can also create a method that accepts 2 keys and performs this internally, without needing to write the code every time.
回答3:
I have not verified this yet, but Ryan Rubley of Morpheus Development has posted a solution here that is worth checking out:
http://xboxforums.create.msdn.com/forums/p/90944/545124.aspx#545124
Here is a fully working solution to use the numpad keys regardless of numlock's status, and without turning on the numlock
Created by Ryan Rubley 9-5-2011