3.4 Obtaining mouse information
mouse-x
, mouse-y
, and mouse-buttons
allow you to poll the mouse.mouse-x
mouse-y
mouse-buttons
Begin by creating a window to contain the drawing:
(setq *window* (make-window :inside-width 100 :inside-height 100 :title "Poll Window"))Define a function that draws circles in a window when the left button is down. The function will exit when the left button is released. Since the functions
mouse-x
andmouse-y
return bit locations that are relative to the root viewport and not to the window, the function has to adjust these values accordingly:(defun draw-some-circles (window) (format t "Hold down the left button to draw circles~%") ;; Wait until left button down. (loop until (eq (mouse-buttons) 4)) ;; Draw while left button down. (loop while (eq (mouse-buttons) 4) do (draw-circle window (make-position (- (mouse-x) (region-origin-x (window-inside-region window))) (- (mouse-y) (region-origin-y (window-inside-region window)))) 10))) ; Diameter of circle is 10.The next example shows how to use polling to draw connected lines on the screen. Every time you click left in the window, a line is drawn from the previous click, or from position (0,0) if there is no previous click.;; Invoke the function. (draw-some-circles *window*)
(defun draw-connected-lines (window) (format t "Click the left button to draw a line, middle button to exit~%") (loop with previous-position = (make-position 0 0) with current-position until (eq (mouse-buttons) 2) ; Exit on middle click. if (eq (mouse-buttons) 4) ; Draw line on left click. do (setq current-position (make-position (- (mouse-x) (region-origin-x (window-inside-region window))) (- (mouse-y) (region-origin-y (window-inside-region window))))) and do (draw-line window previous-position current-position) and do (setq previous-position current-position)))These examples demonstrate certain limitations imposed by polling the mouse to obtain mouse data. Polling returns the location or state of the mouse only when the polling functions are invoked. You cannot determine what the mouse was doing a moment ago, nor can you wait for a specific event to occur except by checking constantly to see if the current event is the one you want. If events occur more quickly than your function invokes the polling functions, only the current state of the mouse is returned, and you might miss changes in this state. The other two methods of obtaining mouse information do not have these limitations.;; Invoke the function. (draw-connected-lines *window*)
Generated with Harlequin WebMaker