Jump to content
  • Advertisement
Sign in to follow this  
csmith12

Issues with GetKeyboardState() in C#

This topic is 4336 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Experience members, I am trying to get the status of the keyboard in a game loop. I am after when the key is pressed down. I don't think I am havin a problem getting the state, but with the way I am checking for keydown vs. key toggled as in caps lock and such. Below is the code; [Test] public void TestKeyboardPolling() { Text = "TestKeyboardPolling"; byte[] keys = new byte[256]; do { // get keyboard state CTInput.GetKeyboardState(keys); // check Enter key if ((keys['a'] & 0x100) == 1) Text = ((int)Keys.Enter).ToString(); else Text = "TestKeyboardPolling"; Application.DoEvents(); } while (Visible); } My if statement never works as expected. Can someone point me in the right direction or a link. I have tried googling without much luck. Thanks,

Share this post


Link to post
Share on other sites
Advertisement
Quote:

if ((keys['a'] & 0x100) == 1)

I very much doubt that anything bitwise ANDed with 0x100 would ever have the lowest bit set.

Share this post


Link to post
Share on other sites
Changed the code to read like this:

[Test]
public void TestKeyboardPolling()
{
Text = "TestKeyboardPolling";
byte[] keys = new byte[256];

do
{
// get keyboard state
CTInput.GetKeyboardState(keys);

// check A key
if ((keys[(int) Keys.A]) == 1)
Text = ((int)Keys.A).ToString();
else
Text = "TestKeyboardPolling";

Application.DoEvents();
}
while (Visible);
}

And it acts like a toggle key now. Closer, but still not what I am after.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!