问题
Hi! I am trying to write a program where I need to report the position of every mouse motion. I have called the XSelectInput()
function with a PointerMotionMask
mask. Everything seems to work alright but the numbers after printing don't appear after every movement, they appear in blocks and also the numbers in event.xmotion.x
and event.xmotion.y
are very high, in the hundred thousands.
What is causing these large numbers?
Also is my program getting every number and reporting it immediately or is it being stored in a queue and sent in blocks to the terminal?
Thanks
Here's my event loop:
while(1)
{
XNextEvent(display, &event);
switch (event.type)
{
case Expose:
glClearColor( 1.0, 1.0, 0.0, 1.0 );
glClear( GL_COLOR_BUFFER_BIT );
glFlush();
glXSwapBuffers( display, glxwin );
break;
case MotionNotify:
printf("%d, %d", event.xmotion.x, event.xmotion.y);
break;
case ButtonPress: exit(1);
default: break;
}
}
回答1:
Besides printing a newline at the end, you could also do a '\r'
at the end it it will move the cursor to the beginning of the existing line, so it will just print over itself each time. To make this work better, change the digit formatting to be a fixed size, like:
printf("%4d, %4d \r", event.xmotion.x, event.xmotion.y);
fflush(stdout) ;
来源:https://stackoverflow.com/questions/21241311/pointer-motion-why-are-numbers-so-high-why-does-it-print-info-in-blocks-not-a