3
\$\begingroup\$

I'm attempting to write input capturing code using SDL2 on Windows. However, I'm running into a problem.

Whenever a key is held down, the SDL event queue has no mouse movement, nor will the mouse move on my screen. Instead, The SDL event queue continually returns the key I am holding down as an SDL_KEYDOWN event.

Here is a simple example program which reproduces the problem exactly:

#include <SDL2/SDL.h>
#include <iostream>

int main(int argc, char** argv) {
  SDL_Init(SDL_INIT_EVERYTHING);
  SDL_Window* window = SDL_CreateWindow("Moust Input Test", SDL_WINDOWPOS_UNDEFINED,
                                        SDL_WINDOWPOS_UNDEFINED, 640, 480,
                                        SDL_WINDOW_SHOWN);
  SDL_Event event;
  for(; ;) {
    while (SDL_PollEvent(&event)) {
      if (event.type == SDL_QUIT) {
        SDL_DestroyWindow(window);
        return 0;
      } else if (event.type == SDL_MOUSEMOTION) {
        int x, y;
        SDL_GetMouseState( &x, &y );
        std::cout << "Mouse moved to: " << x << " " << y << std::endl;
      } else if (event.type == SDL_KEYDOWN) {
        std::cout << "Code of key pressed: " << event.key.keysym.sym << std::endl;
      }
    }
  }
  return 0;
}

What I would expect to happen is that, when I hold down a key (for example, 'a'), it prints exactly once:

Code of key pressed: 97

And then, while they key is held down, if the mouse is moved, it prints every time the mouse is moved:

Mouse moved to: [wherever]

Instead, I'll keep seeing over and over again:

Code of key pressed: 97

And I will not get any mouse movement whatsoever on the screen, nor will I get my mouse movement log printed, until I release whatever keys I am holding down.

Anybody know how I can capture mouse movement while I have a key held down?

The only thing I can think of which could possibly be relevant is I am compiling with MinGW using:

g++ -Dmain=SDL_main test.cpp -o test.exe -L./lib/SDL2 -lmingw32 -lSDL2main -lSDL2 -mwindows -mconsole

Thanks.

Edit: I've also just noticed that the problem persists even when the window is minimized- if I am holding a key down in this text window while my program is minimized in another Window, the mouse stops working, though my program is not printing out the key code.

\$\endgroup\$
  • \$\begingroup\$ Note that if you're receiving mouse motion data via SDL_PollEvent, you can get the mouse's absolute position from event.motion.x and event.motion.y (and its motion from event.motion.xrel and event.motion.yrel) -- no need to call SDL_GetMouseState. (This doesn't answer your question -- it's just a side-note) \$\endgroup\$ – Trevor Powell Dec 18 '14 at 2:22
  • \$\begingroup\$ For what it's worth, the code looks legal, and it works for me as-is under Linux, using SDL2 v2.0.2. \$\endgroup\$ – Trevor Powell Dec 18 '14 at 2:30
  • \$\begingroup\$ Hmm, in that case I guess it could be a bug in the Windows SDL implementation. Do you also only get one SDL_KEYDOWN event if you hold a key continuously on Linux? \$\endgroup\$ – sol_var Dec 18 '14 at 2:39
  • 1
    \$\begingroup\$ No, in SDL2 you will receive SDL_KEYDOWN multiple times, at a frequency based on your OS's key repeat rate. (You can tell whether a SDL_KEYDOWN was generated by a true keydown or by the OS's key repeat by checking the value of event.key.repeat.) \$\endgroup\$ – Trevor Powell Dec 18 '14 at 7:46
  • \$\begingroup\$ @sol_var SDL2 delivers events as given by the OS. I'm only aware of laptop trackpads disabling themselves when some keys are held. So my suggestion is that you check other, non-SDL2 applications for simultaneous mouse+keyboard usage possibilities. \$\endgroup\$ – snake5 Dec 18 '14 at 8:05
2
\$\begingroup\$

The problem might not be in SDL. You can try going to your trackpad's settings, look for something called "palm sensitivity" or "palm-something" and reduce it to zero.

My trackpad momentarily turned off whenever I pressed a key, because of my PalmCheck settings. I reduced my PalmCheck sensitivity to zero and could freely register key presses and mouse motion events at the same time.

My solution was to go to Control Panel > Mouse > Device Setings > Settings > Pointing > PalmCheck, then move the slider to zero. Of course, this is specific to my laptop, but anyone having this problem might be because their "palm-something sensitivity" is too high, and the laptop disables the mouse to prevent movement while typing. Set it to zero, and it might help.

\$\endgroup\$
1
\$\begingroup\$

SDL2 is intended to deliver all these events, not to block one while another is active.

SDL2 does deliver keyboard repeats. I use almost the same code, and I see repeated keys, but also mouse movement events interspersed.

You could filter out keyboard repeats by doing

  } else if (event.type == SDL_KEYDOWN) {
    if(event.key.repeat == 0) {
      std::cout << "Code of key pressed: " << event.key.keysym.sym << std::endl;
    }
  }

Maybe this helps...

\$\endgroup\$
  • \$\begingroup\$ The big issue isn't the repeats, it's that there are no mouse movements whatsoever while any key is held down. Filtering repeated key events will not cause mouse movement events to appear. \$\endgroup\$ – sol_var Dec 18 '14 at 4:38
0
\$\begingroup\$

I use this code below to limit the amount of times I run my Mouse calculations...

You could use something like this, and When KEY IS PRESSED , Make a bool KEYPRESS = true if (keypress == false){ do the key down event } When KEY IS UP, KEYPRESS = false;

See where I am headed with this?

if (e.type == SDL_MOUSEMOTION){

            if (frameBuffer > 10){                      
                std::cout << "Mouse Relative to SCREEN CENTER RelativeX = " << Mouse1.getMousePosX(Engine.getScreenW()) << "\n" <<
                    "               RelativeY = " << Mouse1.getMousePosY(Engine.getScreenH()) << "\n";
                std::cout << "Current Mouse Angle Relative to Center is : " << mouseAngle << "\n\n";
                std::cout << "Mouse Handle Count : " << mouseHandleCount << "\n\n";
                mouseHandleCount++;
                frameBuffer = 0;                    
            }                                   
            frameBuffer++;
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.