XNA Creators Club Online
Page 1 of 1 (13 items)
Sort Posts: Previous Next

Checking All Keyboard Keys

Last post 3/29/2007 9:04 AM by John Sedlak. 12 replies.
  • 3/16/2007 5:02 PM

    Checking All Keyboard Keys

    I'm trying to write an event driven keyboard component, but it doesn't seem to check all the keys. I didn't want to type them all in, so I used this loop:

                for (int i = (int)Keys.A; i <= (int)Keys.Zoom; i++)
                    checkKey((Keys)i, newState);

    It works for most keys, but I noticed Escape, Space, and Enter are never caught. Any ideas? 

  • 3/16/2007 7:06 PM In reply to

    Re: Checking All Keyboard Keys

    Maybe because ESC and SPACE both comes before A (at least in ASCII codes)...
  • 3/16/2007 7:36 PM In reply to

    Re: Checking All Keyboard Keys

    Could be. I just based that alphabetically with how they're listed. If anyone figures this out, I'd love to know, but I have put in a place a list of keys to check so it's fewer keys to check.
  • 3/17/2007 1:30 AM In reply to

    Re: Checking All Keyboard Keys

    You can use reflection here to your advantage. For my EBIS (Event Based Input Service/System) I loop through the enum itself and add the necessary keys to a collection, leaving out Alt, Control and Shift since they are modifier keys and are checked and passed along as simple booleans.

    John Sedlak Xna/DirectX MVP
    XNA Articles, Tutorials and Videos | My Blog
  • 3/17/2007 3:17 AM In reply to

    Re: Checking All Keyboard Keys

    To loop through all the values of an enum do the following:

    for ( int i = 0; i < Enum.GetValues( typeof( Keys ) ).Length; i++ )
    {
           Keys key =  (Keys)Enum.GetValues( typeof( Keys ) ).GetValue(i); 

           checkKey( key, newState );
    }

     

    To explain, the Enum.GetValues( typeof( Keys ) ).Length,

    Enum.GetValues( ) returns an System.Array of the enum's members so wet give it a typeof(Keys) and then grab the length of that array to know how far to loop through.

    Keys key = ( (Keys[])Enum.GetValues( typeof( Keys ) ) )[i] is more complicated but what we're doing here is grabbing the System.Array and using GetValue() to grab the object at the index i. Then we cast the object to a Keys enum and we've got our key!

    You could also do:

    Keys key = ( (Keys[])Enum.GetValues( typeof( Keys ) ) )[i];
     

  • 3/17/2007 1:10 PM In reply to

    Re: Checking All Keyboard Keys

    Btw, Escape has always been key 13 I believe... at least it was in my MDX days...
    John Sedlak Xna/DirectX MVP
    XNA Articles, Tutorials and Videos | My Blog
  • 3/18/2007 11:17 PM In reply to

    Re: Checking All Keyboard Keys

    Keys key =  (Keys)Enum.GetValues( typeof( Keys ) ).GetValue(i);

     

    Now, this example is awsome, I got to say, this is exactly what I've been looking for too, for a keyboard handler/manager/thing I've been working on too. But, this line screams of inefficency to me.  Now, I know this is C# and I'm used to C/C++ so, really it might be a minor thing.  But, Would creating a new array to store the values and then using that, be a smarter method of doing this for speed/performance reasons?  Seems to me, a lot of work for every value in Keys is being done when it can be done once, heck once at the start up of the application, and used then instead.   Or does it not matter with C#. it's smart enough to optimize for this sort of thing.

     

      Tony
     

  • 3/19/2007 2:09 AM In reply to

    Re: Checking All Keyboard Keys

    I do it once, load it into a special class that wraps the key with functionality for timing, a boolean for whether or not it was pressed last update, etc. Then every update I loop through the special array of keys, works perfectly fine.
    John Sedlak Xna/DirectX MVP
    XNA Articles, Tutorials and Videos | My Blog
  • 3/19/2007 5:13 AM In reply to

    Re: Checking All Keyboard Keys

    Well, that was exactly what I was thinking even.  I too was looking to design a simple class that's purpose is to keep track of how long each key has been pressed and if specific conditions are meet, flag the key as 'pressed' again so that anything monitoring specific key presses or generally, looking to get the last key pressed, would generally work out smoothly.

     I figured there are a few things I need to keep track of.  The users Repeat Delay and Repeat Rate (System settings or perhaps just fudge with local values), the time a key was pressed, the length of time it's been pressed, and the last time it was 'used'.  This way, I can properly simulate a key press, allowing for a delay and then repeat action when appropriate.

     
    Functioning like this might be best for situations like, navigating a 2D map, or handling text input.  Say, the user entering a name or sending a text message to another player.

    Seems to me, functionality like this would be somewhat nice and not that difficult to duplicate. 

    Sentack 

  • 3/19/2007 4:54 PM In reply to

    Re: Checking All Keyboard Keys

    That is basically how I do it. I have a global delay for repeating keys, and holding two keys down repeats both keys. If you want I can paste up my code, as it handles GamePad, Mouse and Keyboard input.
    John Sedlak Xna/DirectX MVP
    XNA Articles, Tutorials and Videos | My Blog
  • 3/24/2007 12:21 AM In reply to

    Re: Checking All Keyboard Keys

    Oh wow, I'm seriously delayed in this response. Yah, I would love to see what you wrote. Throw it up, if you don't mind. Sentack
  • 3/28/2007 12:28 PM In reply to

    Re: Checking All Keyboard Keys

    Krisc... sharing code??? Never!
    Eric "TehOne" Lebetsamer
    http://tehone.com
  • 3/29/2007 9:04 AM In reply to

    Re: Checking All Keyboard Keys

    TehOneAndOnly:
    Krisc... sharing code??? Never!

    ha. I am releasing the next GUI video soon, and it includes most, if not all of this.

    Ebi Class: http://xna.multigan.com/pastebin/?page=view&id=1175173398

    Ebi Service Interface and Additional Stuff: http://xna.multigan.com/pastebin/?page=view&id=1175173434

    John Sedlak Xna/DirectX MVP
    XNA Articles, Tutorials and Videos | My Blog
Page 1 of 1 (13 items) Previous Next