   Mouse coordinates changing during SDL_KEYDOWN/UP events
  Hey there. Was wondering if someone could help me out with an odd problem.I have part of a program I'm writing tracking the mouse coordinates in order to draw an elastic line from a static point to the mouse cursor. My problem is that while drawing this line, if any keys are pressed on the keyboard the line shoots off the side of the screen. I've isolated the code below and for the life of me I cannot figure out what's wrong.I've included string output to help track down the problem. I'm seeing that when an SDL_KEYDOWN event is detected, the value of event.motion.x jumps to 57 and event.motion.y jumps to zero. Then, when the key is released and an SDL_KEYUP event is triggered, event.motion.x jumps to 569 event.motion.y value jumps to 31889.Here's my code and below is a sample of the output. Any ideas?#include "SDL/SDL.h"#include <iostream>#include <cmath>using namespace std;SDL_Event event;SDL_Surface* screen;int main( int argc, char* args[] ) {bool quit;bool isKeyDown   = false;bool isMouseDown = false;//Start SDLSDL_Init(SDL_INIT_VIDEO);screen = SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE);SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 25, 25, 25));quit = false;while(!quit) {SDL_PollEvent(&event);if (event.type == SDL_QUIT)quit = true;if ((event.type == SDL_KEYDOWN) && (!isKeyDown)) {cout << "\n***Key down***\n";isKeyDown = true;} //endif SDL_KEYDOWNif (event.type == SDL_KEYUP) {cout << "\n***Key up***\n";isKeyDown = false;}if ((event.type == SDL_MOUSEBUTTONDOWN) && (!isMouseDown)) {cout << "\n***Mouse down***\n";isMouseDown = true;}if (event.type == SDL_MOUSEBUTTONUP) {cout << "\n***Mouse up***\n";isMouseDown = false;}cout << "Mouse Coordinates: " << endl << "x1: " << event.motion.x << " y1: " << event.motion.y << endl;SDL_UpdateRect(screen, 0, 0, 0, 0);SDL_Delay(10);} //end while !quitSDL_Quit();return 0;}/****OUTPUT******Key down***Mouse Coordinates: x1: 57 y1: 0Mouse Coordinates: x1: 297 y1: 279Mouse Coordinates: x1: 286 y1: 277Mouse Coordinates: x1: 281 y1: 276Mouse Coordinates: x1: 274 y1: 276***Key up***Mouse Coordinates: x1: 569 y1: 31889Mouse Coordinates: x1: 268 y1: 276Mouse Coordinates: x1: 265 y1: 274Mouse Coordinates: x1: 253 y1: 274***Key down***Mouse Coordinates: x1: 57 y1: 0Mouse Coordinates: x1: 228 y1: 238Mouse Coordinates: x1: 229 y1: 230***Key up***Mouse Coordinates: x1: 569 y1: 31889Mouse Coordinates: x1: 238 y1: 212Mouse Coordinates: x1: 242 y1: 202Mouse Coordinates: x1: 244 y1: 199***Key down***Mouse Coordinates: x1: 57 y1: 0Mouse Coordinates: x1: 303 y1: 157*/
SDL_Event is a union; it can only represent one type of event.  The reason why you're getting getting odd values is because you're trying to reinterpret_cast<SDL_MouseMotionEvent*>( &keyEvent )->x.  If you want to get the mouse coordinates, you need to have a case for SDL_MOUSEMOTION and use the motion member of the Event.   ;  Awesome, thanks! It makes perfect sense now.   