Problem Detecting Shift Key with Numpad

For whatever reason, the shift-numpad7 detection below never works. The shift-A works perfectly fine. Is this a bug? I feel like it’s ignoring shift when numlock is on and it’s a numpad key, but I looked at the code and I don’t see anything that would do anything like that. When numlock is off, shift-numpad7 seems to work fine.

    var state = Keyboard.GetState();

    if(state.IsKeyDown(Keys.NumPad7) && state.IsKeyDown(Keys.LeftShift))
    {
        System.Diagnostics.Trace.WriteLine("shift Num7");
    }

    if(state.IsKeyDown(Keys.A) && state.IsKeyDown(Keys.LeftShift))
    {
        System.Diagnostics.Trace.WriteLine("shift A");
    }

I must ask if numlock is on? And welcome, new user :slight_smile:

With numlock ON the trace does not write

With numlock OFF the trace does write

The shift-A writes a trace regardless. It’s like numlock disables the shift key for numpad keys or something.

You should be able to pop that into any game loop and observe the same behavior.

When I open notepad and shift-num7 with numlock ON, it activates the home functionality. It’s probably not a big deal if it doesn’t do that, but being a curious sort of fellow I’d like to know why.

I just coded up a thing to test this, and I actually get input from numpad with or without numlock on…

Maybe you are using a special keyboard, that has a switch on it, or needs specific drivers?

That’s interesting. I’m using a laptop keyboard. Nothing strange about it other than the fact that it’s a steel series keyboard. I’ll test it on my desktop tonight and see if I have the same results.

That’s probably it… Laptops tend to have unique buttons and non-standard features, so it’s probably something there… Just for laughs, plug a usb keyboard into the laptop and see what that does to your program… Let me know :slight_smile:

No go. I tried it on my desktop and the shift-num7 with numlock ON produces no results. Then i made a new program and made my update loop the following. It simply won’t detect leftshift and numpad7 at the same time. The output of this loop in my console is below. It detects left shift, but when I press numpad7, left shift goes away. Turning off numlock restores the left shift. It’s just weird.

numlock onLeftShift
numlock onNumPad7
numlock onLeftShift
numlock offNumLockLeftShift
numlock offLeftShift
numlock offNumPad7LeftShift
numlock offLeftShift

    string lastKeys = "";
    protected override void Update(GameTime gameTime)
    {
        if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
            Exit();

        var state = Keyboard.GetState();

        if (state.GetPressedKeyCount() > 0)
        {
            string keys = "";
            foreach (var k in state.GetPressedKeys())
            {
                keys += k.ToString();
            }

            if (keys != lastKeys)
            {
                System.Diagnostics.Trace.WriteLine((state.NumLock ? "numlock on" : "numlock off") + keys);
                
            }

            lastKeys = keys;
        }

        // TODO: Add your update logic here

        base.Update(gameTime);
    }

To thicken the plot slightly, pressing shift-numpad2(4, 6, 8) produce DOWN, LEFT, RIGHT, UP keycodes like the shifting is sending a modified keycode for those, but not for HOME, END, PGUP, PGDN.

ok, I found this, it’s pretty nuts…

I coded a thing to get input… For num pad and shift together, it registers if you press and hold the num pad key first, THEN shift, but not the other way around…

I tested to make sure, and this is NOT the case for other keys, where you get the functionality you would expect… I have no idea why any of this would be the case, but I guess find a different control scheme… I thought about it, and I have never seen a shift-num-pad control scheme for anything, maybe there is a reason for that ?

1 Like

Well, at least I’m not crazy. I’m not using it as a control scheme. I’m writing the text input for a gui system, and trying to get it to work 100% like windows works. I personally don’t press shift-num7 to go home, but there might be a couple of people in the world who do.

I am reminded of a problem I had in the past, which gave me the same feeling… which is no support for usb joysticks… Since windows can detect the input from them, I thought they would have worked in monogame. But no, you HAVE to use a certain brand of controller… Everyone who uses your game, just has to adhere to certain schemes/protocols… I still don’t get it, but I also cannot do better myself. There are some limits to a framework I guess, even if I don’t get why… But it’s open source, so maybe not hopeless if you really wanna get serious and make changes.

1 Like