This project is read-only.

Issue with keyboard input

Nov 27, 2012 at 8:55 AM


I'm using an Input event handler to detect LeftControl presses however the KeyboardEventArgs is passing through a value of 17 instead of Keys.LeftControl enum value. I have to cast the value to int and compare as an integer to get it working. I'm not sure why as it seems to work as expected for most other keys. Do you know what might be happening here?

The relevant code is:

            Components.Add(new InputComponent(Window.Handle));
            input = new Input();
            input.KeyDown += (sender, args) =>
                if (args.Key == Keys.LeftControl)
                    ctrlMod = true;

I have to use this to get it working:

input.KeyDown += (sender, args) =>
                if ((int)args.Key == 17)
                    ctrlMod = true;

It does the same thing with the Alt key too.


Nov 27, 2012 at 9:12 AM
Edited Nov 27, 2012 at 9:26 AM

The LeftControl in the enum is 162. But the input is 17, on my pc too. This can be region related with all that keyboard layouts. 

This is XNA related issue. More on it.


But I have to include in this post that it can be added to engine Nine to work using only the enum.

Nov 27, 2012 at 10:39 AM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.
Nov 27, 2012 at 10:44 AM

This is caused by am incorrect mapping between System.Windows.Forms.Keys and Microsoft.Xna.Framework.Input.Keys.

When you create an InputComponent with a handle to the game window, it internally uses Windows forms to handle keyboard inputs, this guarantees that keyboard events are captured even in low frame rate conditions.

A walkaround is to use "new InputComponent()" instead. The drawback is that key strokes might be discarded if the frame rate is too low.

I'm creating a issue to track this.

Nov 27, 2012 at 11:38 PM

Thanks for the info.

I'm fine using the workaround for now. Just thought I would raise it so you are aware of the issue.