Optimize the move function

Using XPending , XPeekEvent(It can block), XNextEvent to optimize it.
At the same time ,optimize the code.